File: fields.pyi

package info (click to toggle)
python-django-stubs 5.2.9-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,832 kB
  • sloc: python: 5,185; makefile: 15; sh: 8
file content (590 lines) | stat: -rw-r--r-- 21,274 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
import datetime
from collections.abc import Collection, Iterator, Sequence
from decimal import Decimal
from re import Pattern
from typing import Any, ClassVar, Protocol, TypeAlias, type_check_only
from uuid import UUID

from django.core.files import File
from django.core.validators import _ValidatorCallable
from django.db.models.fields import _ErrorMessagesDict, _ErrorMessagesMapping
from django.forms.boundfield import BoundField
from django.forms.forms import BaseForm
from django.forms.widgets import Widget
from django.utils.choices import CallableChoiceIterator, _ChoicesCallable, _ChoicesInput
from django.utils.datastructures import _PropertyDescriptor
from django.utils.functional import _StrOrPromise

# Problem: attribute `widget` is always of type `Widget` after field instantiation.
# However, on class level it can be set to `Type[Widget]` too.
# If we annotate it as `Union[Widget, Type[Widget]]`, every code that uses field
# instances will not typecheck.
# If we annotate it as `Widget`, any widget subclasses that do e.g.
# `widget = Select` will not typecheck.
# `Any` gives too much freedom, but does not create false positives.
_ClassLevelWidgetT: TypeAlias = Any

class Field:
    initial: Any
    label: _StrOrPromise | None
    required: bool
    widget: _ClassLevelWidgetT
    hidden_widget: type[Widget]
    default_validators: list[_ValidatorCallable]
    default_error_messages: ClassVar[_ErrorMessagesDict]
    empty_values: Sequence[Any]
    show_hidden_initial: bool
    help_text: _StrOrPromise
    disabled: bool
    label_suffix: str | None
    localize: bool
    error_messages: _ErrorMessagesDict
    validators: list[_ValidatorCallable]
    bound_field_class: type[BoundField] | None
    def __init__(
        self,
        *,
        required: bool = True,
        widget: Widget | type[Widget] | None = None,
        label: _StrOrPromise | None = None,
        initial: Any | None = None,
        help_text: _StrOrPromise = "",
        error_messages: _ErrorMessagesMapping | None = None,
        show_hidden_initial: bool = False,
        validators: Sequence[_ValidatorCallable] = (),
        localize: bool = False,
        disabled: bool = False,
        label_suffix: str | None = None,
        template_name: str | None = None,
        bound_field_class: type[BoundField] | None = None,
    ) -> None: ...
    def prepare_value(self, value: Any) -> Any: ...
    def to_python(self, value: Any | None) -> Any | None: ...
    def validate(self, value: Any) -> None: ...
    def run_validators(self, value: Any) -> None: ...
    def clean(self, value: Any) -> Any: ...
    def bound_data(self, data: Any, initial: Any) -> Any: ...
    def widget_attrs(self, widget: Widget) -> dict[str, Any]: ...
    def has_changed(self, initial: Any | None, data: Any | None) -> bool: ...
    def get_bound_field(self, form: BaseForm, field_name: str) -> BoundField: ...

class CharField(Field):
    max_length: int | None
    min_length: int | None
    strip: bool
    empty_value: str | None
    def __init__(
        self,
        *,
        max_length: int | None = None,
        min_length: int | None = None,
        strip: bool = True,
        empty_value: str | None = "",
        required: bool = ...,
        widget: Widget | type[Widget] | None = ...,
        label: _StrOrPromise | None = ...,
        initial: Any | None = ...,
        help_text: _StrOrPromise = ...,
        error_messages: _ErrorMessagesMapping | None = ...,
        show_hidden_initial: bool = ...,
        validators: Sequence[_ValidatorCallable] = ...,
        localize: bool = ...,
        disabled: bool = ...,
        label_suffix: str | None = ...,
    ) -> None: ...
    def to_python(self, value: Any | None) -> str | None: ...
    def widget_attrs(self, widget: Widget) -> dict[str, Any]: ...

class IntegerField(Field):
    max_value: int | None
    min_value: int | None
    step_size: int | None
    re_decimal: Any
    def __init__(
        self,
        *,
        max_value: int | None = None,
        min_value: int | None = None,
        step_size: int | None = None,
        required: bool = ...,
        widget: Widget | type[Widget] | None = ...,
        label: _StrOrPromise | None = ...,
        initial: Any | None = ...,
        help_text: _StrOrPromise = ...,
        error_messages: _ErrorMessagesMapping | None = ...,
        show_hidden_initial: bool = ...,
        validators: Sequence[_ValidatorCallable] = ...,
        localize: bool = ...,
        disabled: bool = ...,
        label_suffix: str | None = ...,
    ) -> None: ...
    def to_python(self, value: Any | None) -> int | None: ...
    def widget_attrs(self, widget: Widget) -> dict[str, Any]: ...

class FloatField(IntegerField):
    def __init__(
        self,
        *,
        max_value: int | float | None = None,
        min_value: int | float | None = None,
        step_size: int | float | None = None,
        required: bool = ...,
        widget: Widget | type[Widget] | None = ...,
        label: _StrOrPromise | None = ...,
        initial: Any | None = ...,
        help_text: _StrOrPromise = ...,
        error_messages: _ErrorMessagesMapping | None = ...,
        show_hidden_initial: bool = ...,
        validators: Sequence[_ValidatorCallable] = ...,
        localize: bool = ...,
        disabled: bool = ...,
        label_suffix: str | None = ...,
    ) -> None: ...
    def to_python(self, value: Any | None) -> float | None: ...  # type: ignore[override]
    def validate(self, value: float) -> None: ...
    def widget_attrs(self, widget: Widget) -> dict[str, Any]: ...

class DecimalField(IntegerField):
    decimal_places: int | None
    max_digits: int | None
    def __init__(
        self,
        *,
        max_value: Decimal | int | float | None = None,
        min_value: Decimal | int | float | None = None,
        max_digits: int | None = None,
        decimal_places: int | None = None,
        step_size: Decimal | int | float | None = ...,
        required: bool = ...,
        widget: Widget | type[Widget] | None = ...,
        label: _StrOrPromise | None = ...,
        initial: Any | None = ...,
        help_text: _StrOrPromise = ...,
        error_messages: _ErrorMessagesMapping | None = ...,
        show_hidden_initial: bool = ...,
        validators: Sequence[_ValidatorCallable] = ...,
        localize: bool = ...,
        disabled: bool = ...,
        label_suffix: str | None = ...,
    ) -> None: ...
    def to_python(self, value: Any | None) -> Decimal | None: ...  # type: ignore[override]
    def validate(self, value: Decimal) -> None: ...
    def widget_attrs(self, widget: Widget) -> dict[str, Any]: ...

class BaseTemporalField(Field):
    input_formats: Any
    def __init__(
        self,
        *,
        input_formats: Any | None = None,
        required: bool = ...,
        widget: Widget | type[Widget] | None = ...,
        label: _StrOrPromise | None = ...,
        initial: Any | None = ...,
        help_text: _StrOrPromise = ...,
        error_messages: _ErrorMessagesMapping | None = ...,
        show_hidden_initial: bool = ...,
        validators: Sequence[_ValidatorCallable] = ...,
        localize: bool = ...,
        disabled: bool = ...,
        label_suffix: str | None = ...,
    ) -> None: ...
    def to_python(self, value: str | None) -> Any | None: ...
    def strptime(self, value: str, format: str) -> Any: ...

class DateField(BaseTemporalField):
    def to_python(self, value: None | str | datetime.datetime | datetime.date) -> datetime.date | None: ...
    def strptime(self, value: str, format: str) -> datetime.date: ...

class TimeField(BaseTemporalField):
    def to_python(self, value: None | str | datetime.time) -> datetime.time | None: ...
    def strptime(self, value: str, format: str) -> datetime.time: ...

class DateTimeFormatsIterator:
    def __iter__(self) -> Iterator[str]: ...

class DateTimeField(BaseTemporalField):
    def to_python(self, value: None | str | datetime.datetime | datetime.date) -> datetime.datetime | None: ...
    def strptime(self, value: str, format: str) -> datetime.datetime: ...

class DurationField(Field):
    def prepare_value(self, value: datetime.timedelta | str | None) -> str | None: ...
    def to_python(self, value: Any | None) -> datetime.timedelta | None: ...

class RegexField(CharField):
    regex: _PropertyDescriptor[str | Pattern[str], Pattern[str]]
    def __init__(
        self,
        regex: str | Pattern[str],
        *,
        max_length: int | None = ...,
        min_length: int | None = ...,
        strip: bool = ...,
        empty_value: str | None = ...,
        required: bool = ...,
        widget: Widget | type[Widget] | None = ...,
        label: _StrOrPromise | None = ...,
        initial: Any | None = ...,
        help_text: _StrOrPromise = ...,
        error_messages: _ErrorMessagesMapping | None = ...,
        show_hidden_initial: bool = ...,
        validators: Sequence[_ValidatorCallable] = ...,
        localize: bool = ...,
        disabled: bool = ...,
        label_suffix: str | None = ...,
    ) -> None: ...

class EmailField(CharField):
    def __init__(
        self,
        *,
        max_length: int | None = ...,
        min_length: int | None = ...,
        strip: bool = ...,
        empty_value: str | None = ...,
        required: bool = ...,
        widget: Widget | type[Widget] | None = ...,
        label: _StrOrPromise | None = ...,
        initial: Any | None = ...,
        help_text: _StrOrPromise = ...,
        error_messages: _ErrorMessagesMapping | None = ...,
        show_hidden_initial: bool = ...,
        validators: Sequence[_ValidatorCallable] = ...,
        localize: bool = ...,
        disabled: bool = ...,
        label_suffix: str | None = ...,
    ) -> None: ...

class FileField(Field):
    allow_empty_file: bool
    max_length: int | None
    def __init__(
        self,
        *,
        max_length: int | None = None,
        allow_empty_file: bool = False,
        required: bool = ...,
        widget: Widget | type[Widget] | None = ...,
        label: _StrOrPromise | None = ...,
        initial: Any | None = ...,
        help_text: _StrOrPromise = ...,
        error_messages: _ErrorMessagesMapping | None = ...,
        show_hidden_initial: bool = ...,
        validators: Sequence[_ValidatorCallable] = ...,
        localize: bool = ...,
        disabled: bool = ...,
        label_suffix: str | None = ...,
    ) -> None: ...
    def clean(self, data: Any, initial: Any | None = None) -> Any: ...
    def to_python(self, data: File | None) -> File | None: ...
    def bound_data(self, data: Any | None, initial: Any) -> Any: ...
    def has_changed(self, initial: Any | None, data: Any | None) -> bool: ...

class ImageField(FileField):
    def to_python(self, data: File | None) -> File | None: ...
    def widget_attrs(self, widget: Widget) -> dict[str, Any]: ...

class URLField(CharField):
    def __init__(
        self,
        *,
        max_length: int | None = ...,
        min_length: int | None = ...,
        strip: bool = ...,
        empty_value: str | None = ...,
        required: bool = ...,
        widget: Widget | type[Widget] | None = ...,
        label: _StrOrPromise | None = ...,
        initial: Any | None = ...,
        help_text: _StrOrPromise = ...,
        error_messages: _ErrorMessagesMapping | None = ...,
        show_hidden_initial: bool = ...,
        validators: Sequence[_ValidatorCallable] = ...,
        localize: bool = ...,
        disabled: bool = ...,
        label_suffix: str | None = ...,
        assume_scheme: str | None = None,
    ) -> None: ...
    def to_python(self, value: Any | None) -> str | None: ...

class BooleanField(Field):
    def to_python(self, value: Any | None) -> bool: ...
    def validate(self, value: Any) -> None: ...
    def has_changed(self, initial: Any | None, data: Any | None) -> bool: ...

class NullBooleanField(BooleanField):
    def to_python(self, value: Any | None) -> bool | None: ...  # type: ignore[override]
    def validate(self, value: Any) -> None: ...

class ChoiceField(Field):
    choices: _PropertyDescriptor[
        _ChoicesInput | _ChoicesCallable | CallableChoiceIterator,
        _ChoicesInput | CallableChoiceIterator,
    ]
    widget: _ClassLevelWidgetT
    def __init__(
        self,
        *,
        choices: _ChoicesInput | _ChoicesCallable = (),
        required: bool = ...,
        widget: Widget | type[Widget] | None = ...,
        label: _StrOrPromise | None = ...,
        initial: Any | None = ...,
        help_text: _StrOrPromise = ...,
        error_messages: _ErrorMessagesMapping | None = ...,
        show_hidden_initial: bool = ...,
        validators: Sequence[_ValidatorCallable] = ...,
        localize: bool = ...,
        disabled: bool = ...,
        label_suffix: str | None = ...,
    ) -> None: ...
    # Real return type of `to_python` is `str`, but it results in errors when
    # subclassing `ModelChoiceField`: `# type: ignore[override]` is not inherited
    def to_python(self, value: Any | None) -> Any: ...
    def validate(self, value: Any) -> None: ...
    def valid_value(self, value: Any) -> bool: ...

@type_check_only
class _CoerceCallable(Protocol):
    def __call__(self, value: Any, /) -> Any: ...

class TypedChoiceField(ChoiceField):
    coerce: _CoerceCallable
    empty_value: str | None
    def __init__(
        self,
        *,
        coerce: _CoerceCallable = ...,
        empty_value: str | None = "",
        choices: _ChoicesInput | _ChoicesCallable = ...,
        required: bool = ...,
        widget: Widget | type[Widget] | None = ...,
        label: _StrOrPromise | None = ...,
        initial: Any | None = ...,
        help_text: _StrOrPromise = ...,
        error_messages: _ErrorMessagesMapping | None = ...,
        show_hidden_initial: bool = ...,
        validators: Sequence[_ValidatorCallable] = ...,
        localize: bool = ...,
        disabled: bool = ...,
        label_suffix: str | None = ...,
    ) -> None: ...
    def clean(self, value: Any) -> Any: ...

class MultipleChoiceField(ChoiceField):
    def to_python(self, value: Any | None) -> list[str]: ...
    def validate(self, value: Any) -> None: ...
    def has_changed(self, initial: Collection[Any] | None, data: Collection[Any] | None) -> bool: ...

class TypedMultipleChoiceField(MultipleChoiceField):
    coerce: _CoerceCallable
    empty_value: list[Any] | None
    def __init__(
        self,
        *,
        coerce: _CoerceCallable = ...,
        empty_value: list[Any] | None = ...,
        choices: _ChoicesInput | _ChoicesCallable = ...,
        required: bool = ...,
        widget: Widget | type[Widget] | None = ...,
        label: _StrOrPromise | None = ...,
        initial: Any | None = ...,
        help_text: _StrOrPromise = ...,
        error_messages: _ErrorMessagesMapping | None = ...,
        show_hidden_initial: bool = ...,
        validators: Sequence[_ValidatorCallable] = ...,
        localize: bool = ...,
        disabled: bool = ...,
        label_suffix: str | None = ...,
    ) -> None: ...
    def clean(self, value: Any) -> Any: ...
    def validate(self, value: Any) -> None: ...

class ComboField(Field):
    fields: Sequence[Field]
    def __init__(
        self,
        fields: Sequence[Field],
        *,
        required: bool = ...,
        widget: Widget | type[Widget] | None = ...,
        label: _StrOrPromise | None = ...,
        initial: Any | None = ...,
        help_text: _StrOrPromise = ...,
        error_messages: _ErrorMessagesMapping | None = ...,
        show_hidden_initial: bool = ...,
        validators: Sequence[_ValidatorCallable] = ...,
        localize: bool = ...,
        disabled: bool = ...,
        label_suffix: str | None = ...,
    ) -> None: ...
    def clean(self, value: Any) -> Any: ...

class MultiValueField(Field):
    require_all_fields: bool
    fields: Sequence[Field]
    def __init__(
        self,
        fields: Sequence[Field],
        *,
        require_all_fields: bool = True,
        required: bool = ...,
        widget: Widget | type[Widget] | None = ...,
        label: _StrOrPromise | None = ...,
        initial: Any | None = ...,
        help_text: _StrOrPromise = ...,
        error_messages: _ErrorMessagesMapping | None = ...,
        show_hidden_initial: bool = ...,
        validators: Sequence[_ValidatorCallable] = ...,
        localize: bool = ...,
        disabled: bool = ...,
        label_suffix: str | None = ...,
    ) -> None: ...
    def compress(self, data_list: Any) -> Any: ...
    def has_changed(self, initial: Any | None, data: Any | None) -> bool: ...
    def clean(self, value: Any) -> Any: ...
    def validate(self, value: Any) -> None: ...

class FilePathField(ChoiceField):
    allow_files: bool
    allow_folders: bool
    match: str | None
    path: str
    recursive: bool
    match_re: Pattern[str] | None
    def __init__(
        self,
        path: str,
        *,
        match: str | None = None,
        recursive: bool = False,
        allow_files: bool = True,
        allow_folders: bool = False,
        choices: _ChoicesInput | _ChoicesCallable = ...,
        required: bool = ...,
        widget: Widget | type[Widget] | None = ...,
        label: _StrOrPromise | None = ...,
        initial: Any | None = ...,
        help_text: _StrOrPromise = ...,
        error_messages: _ErrorMessagesMapping | None = ...,
        show_hidden_initial: bool = ...,
        validators: Sequence[_ValidatorCallable] = ...,
        localize: bool = ...,
        disabled: bool = ...,
        label_suffix: str | None = ...,
    ) -> None: ...

class SplitDateTimeField(MultiValueField):
    def __init__(
        self,
        *,
        input_date_formats: Any | None = None,
        input_time_formats: Any | None = None,
        fields: Sequence[Field] = ...,
        require_all_fields: bool = ...,
        required: bool = ...,
        widget: Widget | type[Widget] | None = ...,
        label: _StrOrPromise | None = ...,
        initial: Any | None = ...,
        help_text: _StrOrPromise = ...,
        error_messages: _ErrorMessagesMapping | None = ...,
        show_hidden_initial: bool = ...,
        validators: Sequence[_ValidatorCallable] = ...,
        localize: bool = ...,
        disabled: bool = ...,
        label_suffix: str | None = ...,
    ) -> None: ...
    def compress(self, data_list: tuple[datetime.date, datetime.time] | None) -> datetime.datetime | None: ...

class GenericIPAddressField(CharField):
    unpack_ipv4: bool
    def __init__(
        self,
        *,
        protocol: str = "both",
        unpack_ipv4: bool = False,
        required: bool = ...,
        widget: Widget | type[Widget] | None = ...,
        label: _StrOrPromise | None = ...,
        initial: Any | None = ...,
        help_text: _StrOrPromise = ...,
        error_messages: _ErrorMessagesMapping | None = ...,
        show_hidden_initial: bool = ...,
        validators: Sequence[_ValidatorCallable] = ...,
        localize: bool = ...,
        disabled: bool = ...,
        label_suffix: str | None = ...,
    ) -> None: ...
    def to_python(self, value: Any) -> str: ...

class SlugField(CharField):
    allow_unicode: bool
    def __init__(
        self,
        *,
        allow_unicode: bool = False,
        max_length: Any | None = ...,
        min_length: Any | None = ...,
        strip: bool = ...,
        empty_value: str | None = ...,
        required: bool = ...,
        widget: Widget | type[Widget] | None = ...,
        label: _StrOrPromise | None = ...,
        initial: Any | None = ...,
        help_text: _StrOrPromise = ...,
        error_messages: _ErrorMessagesMapping | None = ...,
        show_hidden_initial: bool = ...,
        validators: Sequence[_ValidatorCallable] = ...,
        localize: bool = ...,
        disabled: bool = ...,
        label_suffix: str | None = ...,
    ) -> None: ...

class UUIDField(CharField):
    def prepare_value(self, value: Any | None) -> Any | None: ...
    def to_python(self, value: Any) -> UUID | None: ...  # type: ignore[override]

class InvalidJSONInput(str): ...
class JSONString(str): ...

class JSONField(CharField):
    default_error_messages: ClassVar[_ErrorMessagesDict]
    widget: _ClassLevelWidgetT
    encoder: Any
    decoder: Any
    def __init__(self, encoder: Any | None = None, decoder: Any | None = None, **kwargs: Any) -> None: ...
    def to_python(self, value: Any) -> Any: ...
    def bound_data(self, data: Any, initial: Any) -> Any: ...
    def prepare_value(self, value: Any) -> str: ...
    def has_changed(self, initial: Any | None, data: Any | None) -> bool: ...

__all__ = (
    "BooleanField",
    "CharField",
    "ChoiceField",
    "ComboField",
    "DateField",
    "DateTimeField",
    "DecimalField",
    "DurationField",
    "EmailField",
    "Field",
    "FileField",
    "FilePathField",
    "FloatField",
    "GenericIPAddressField",
    "ImageField",
    "IntegerField",
    "JSONField",
    "MultiValueField",
    "MultipleChoiceField",
    "NullBooleanField",
    "RegexField",
    "SlugField",
    "SplitDateTimeField",
    "TimeField",
    "TypedChoiceField",
    "TypedMultipleChoiceField",
    "URLField",
    "UUIDField",
)