|
| 1 | +from collections.abc import Sequence |
| 2 | +from typing import Any, NamedTuple |
| 3 | +from typing_extensions import TypeAlias |
| 4 | + |
| 5 | +from django import forms |
| 6 | + |
| 7 | +DJANGO_50: bool |
| 8 | + |
| 9 | +# Ref: django-stubs/forms/fields.pyi |
| 10 | +# Problem: attribute `widget` is always of type `Widget` after field instantiation. |
| 11 | +# However, on class level it can be set to `Type[Widget]` too. |
| 12 | +# If we annotate it as `Union[Widget, Type[Widget]]`, every code that uses field |
| 13 | +# instances will not typecheck. |
| 14 | +# If we annotate it as `Widget`, any widget subclasses that do e.g. |
| 15 | +# `widget = Select` will not typecheck. |
| 16 | +# `Any` gives too much freedom, but does not create false positives. |
| 17 | +_ClassLevelWidget: TypeAlias = Any |
| 18 | + |
| 19 | +class RangeField(forms.MultiValueField): |
| 20 | + widget: _ClassLevelWidget = ... |
| 21 | + def __init__( |
| 22 | + self, fields: tuple[forms.Field, forms.Field] | None = None, *args: Any, **kwargs: Any |
| 23 | + ) -> None: ... # Args/kwargs can be any field params, passes to parent |
| 24 | + def compress(self, data_list: list[Any] | None) -> slice | None: ... # Data list elements can be any field value type |
| 25 | + |
| 26 | +class DateRangeField(RangeField): |
| 27 | + widget: _ClassLevelWidget = ... |
| 28 | + def __init__(self, *args: Any, **kwargs: Any) -> None: ... # Args/kwargs can be any field params for parent |
| 29 | + def compress(self, data_list: list[Any] | None) -> slice | None: ... # Date values in list can be any date type |
| 30 | + |
| 31 | +class DateTimeRangeField(RangeField): |
| 32 | + widget: _ClassLevelWidget = ... |
| 33 | + def __init__(self, *args: Any, **kwargs: Any) -> None: ... # Args/kwargs can be any field params for parent |
| 34 | + |
| 35 | +class IsoDateTimeRangeField(RangeField): |
| 36 | + widget: _ClassLevelWidget = ... |
| 37 | + def __init__(self, *args: Any, **kwargs: Any) -> None: ... # Args/kwargs can be any field params for parent |
| 38 | + |
| 39 | +class TimeRangeField(RangeField): |
| 40 | + widget: _ClassLevelWidget = ... |
| 41 | + def __init__(self, *args: Any, **kwargs: Any) -> None: ... # Args/kwargs can be any field params for parent |
| 42 | + |
| 43 | +class Lookup(NamedTuple): |
| 44 | + value: Any # Lookup values can be any filterable type |
| 45 | + lookup_expr: str |
| 46 | + |
| 47 | +class LookupChoiceField(forms.MultiValueField): |
| 48 | + def __init__( |
| 49 | + self, field: forms.Field, lookup_choices: Sequence[tuple[str, str]], *args: Any, **kwargs: Any |
| 50 | + ) -> None: ... # Args/kwargs can be any field params, uses kwargs for empty_label |
| 51 | + def compress(self, data_list: list[Any] | None) -> Lookup | None: ... # Data list can contain any lookup components |
| 52 | + |
| 53 | +class IsoDateTimeField(forms.DateTimeField): |
| 54 | + ISO_8601: str |
| 55 | + input_formats: list[str] |
| 56 | + def strptime(self, value: str, format: str) -> Any: ... # Returns datetime objects or parsing results |
| 57 | + |
| 58 | +class BaseCSVField(forms.Field): |
| 59 | + base_widget_class: _ClassLevelWidget = ... |
| 60 | + def __init__(self, *args: Any, **kwargs: Any) -> None: ... # Args/kwargs can be any field params for widget config |
| 61 | + def clean(self, value: Any) -> Any: ... # Cleaned values can be any valid field type |
| 62 | + |
| 63 | +class BaseRangeField(BaseCSVField): |
| 64 | + widget: _ClassLevelWidget = ... |
| 65 | + def clean(self, value: Any) -> Any: ... # Input and output values can be any range type |
| 66 | + |
| 67 | +class ChoiceIterator: |
| 68 | + field: ChoiceField |
| 69 | + choices: Sequence[tuple[Any, str]] # Choice values can be any type (int, str, Model, etc.) |
| 70 | + def __init__( |
| 71 | + self, field: ChoiceField, choices: Sequence[tuple[Any, str]] |
| 72 | + ) -> None: ... # Choice values can be any selectable type |
| 73 | + def __iter__(self) -> Any: ... # Iterator yields choice tuples with any value types |
| 74 | + def __len__(self) -> int: ... |
| 75 | + |
| 76 | +class ModelChoiceIterator(forms.models.ModelChoiceIterator): |
| 77 | + def __iter__(self) -> Any: ... # Iterator yields choice tuples with any value types |
| 78 | + def __len__(self) -> int: ... |
| 79 | + |
| 80 | +class ChoiceIteratorMixin: |
| 81 | + null_label: str | None |
| 82 | + null_value: Any # Null choice values can be any type (None, empty string, etc.) |
| 83 | + def __init__(self, *args: Any, **kwargs: Any) -> None: ... # Args/kwargs can be any field params for null config |
| 84 | + |
| 85 | +class ChoiceField(ChoiceIteratorMixin, forms.ChoiceField): |
| 86 | + iterator = ChoiceIterator |
| 87 | + empty_label: str | None |
| 88 | + def __init__(self, *args: Any, **kwargs: Any) -> None: ... # Args/kwargs can be any field params for label config |
| 89 | + |
| 90 | +class MultipleChoiceField(ChoiceIteratorMixin, forms.MultipleChoiceField): |
| 91 | + iterator = ChoiceIterator |
| 92 | + empty_label: str | None |
| 93 | + def __init__(self, *args: Any, **kwargs: Any) -> None: ... # Args/kwargs can be any field params, sets empty_label |
| 94 | + |
| 95 | +class ModelChoiceField(ChoiceIteratorMixin, forms.ModelChoiceField[Any]): |
| 96 | + iterator = ModelChoiceIterator |
| 97 | + def to_python(self, value: Any) -> Any: ... # Converts any input to Python model objects or values |
| 98 | + |
| 99 | +class ModelMultipleChoiceField(ChoiceIteratorMixin, forms.ModelMultipleChoiceField[Any]): |
| 100 | + iterator = ModelChoiceIterator |
0 commit comments