Skip to content

Commit 144c39b

Browse files
committed
[django-filter] Add __init__ params that are inherited from parent classes
Removed `__init__` methods from classes whose parameters are same as parent class.
1 parent 429a960 commit 144c39b

File tree

4 files changed

+167
-31
lines changed

4 files changed

+167
-31
lines changed

stubs/django-filter/django_filters/fields.pyi

Lines changed: 82 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
from collections.abc import Sequence
2-
from typing import Any, NamedTuple
2+
from typing import Any, NamedTuple, Mapping, Callable, Iterable, type_check_only, Protocol
3+
4+
from django.db.models import Choices
5+
from django.forms import Widget
6+
from django_stubs_ext import StrOrPromise
37
from typing_extensions import TypeAlias
48

59
from django import forms
@@ -15,38 +19,72 @@ DJANGO_50: bool
1519
# `widget = Select` will not typecheck.
1620
# `Any` gives too much freedom, but does not create false positives.
1721
_ClassLevelWidget: TypeAlias = Any
22+
_ValidatorCallable: TypeAlias = Callable[[Any], None]
23+
24+
# Based on django-stubs utils/choices.pyi
25+
_Choice: TypeAlias = tuple[Any, Any]
26+
_ChoiceNamedGroup: TypeAlias = tuple[str, Iterable[_Choice]]
27+
_Choices: TypeAlias = Iterable[_Choice | _ChoiceNamedGroup]
28+
_ChoicesMapping: TypeAlias = Mapping[Any, Any]
29+
_ChoicesInput: TypeAlias = _Choices | _ChoicesMapping | type[Choices] | Callable[[], _Choices | _ChoicesMapping]
1830

1931
class RangeField(forms.MultiValueField):
2032
widget: _ClassLevelWidget = ...
2133
def __init__(
22-
self, fields: tuple[forms.Field, forms.Field] | None = None, *args: Any, **kwargs: Any
34+
self,
35+
fields: tuple[forms.Field, forms.Field] | None = None,
36+
*,
37+
# Inherited from MultiValueField
38+
require_all_fields: bool = True,
39+
required: bool = ...,
40+
widget: Widget | type[Widget] | None = ...,
41+
label: StrOrPromise | None = ...,
42+
initial: Any | None = ...,
43+
help_text: StrOrPromise = ...,
44+
error_messages: Mapping[str, StrOrPromise] | None = ...,
45+
show_hidden_initial: bool = ...,
46+
validators: Sequence[_ValidatorCallable] = ...,
47+
localize: bool = ...,
48+
disabled: bool = ...,
49+
label_suffix: str | None = ...,
2350
) -> None: ... # Args/kwargs can be any field params, passes to parent
2451
def compress(self, data_list: list[Any] | None) -> slice | None: ... # Data list elements can be any field value type
2552

2653
class DateRangeField(RangeField):
2754
widget: _ClassLevelWidget = ...
28-
def __init__(self, *args: Any, **kwargs: Any) -> None: ... # Args/kwargs can be any field params for parent
2955
def compress(self, data_list: list[Any] | None) -> slice | None: ... # Date values in list can be any date type
3056

3157
class DateTimeRangeField(RangeField):
3258
widget: _ClassLevelWidget = ...
33-
def __init__(self, *args: Any, **kwargs: Any) -> None: ... # Args/kwargs can be any field params for parent
3459

3560
class IsoDateTimeRangeField(RangeField):
3661
widget: _ClassLevelWidget = ...
37-
def __init__(self, *args: Any, **kwargs: Any) -> None: ... # Args/kwargs can be any field params for parent
3862

3963
class TimeRangeField(RangeField):
4064
widget: _ClassLevelWidget = ...
41-
def __init__(self, *args: Any, **kwargs: Any) -> None: ... # Args/kwargs can be any field params for parent
4265

4366
class Lookup(NamedTuple):
4467
value: Any # Lookup values can be any filterable type
4568
lookup_expr: str
4669

4770
class LookupChoiceField(forms.MultiValueField):
4871
def __init__(
49-
self, field: forms.Field, lookup_choices: Sequence[tuple[str, str]], *args: Any, **kwargs: Any
72+
self,
73+
field: forms.Field,
74+
lookup_choices: Sequence[tuple[str, str]],
75+
*,
76+
require_all_fields: bool = True,
77+
required: bool = ...,
78+
widget: Widget | type[Widget] | None = ...,
79+
label: StrOrPromise | None = ...,
80+
initial: Any | None = ...,
81+
help_text: StrOrPromise = ...,
82+
error_messages: Mapping[str, StrOrPromise] | None = ...,
83+
show_hidden_initial: bool = ...,
84+
validators: Sequence[_ValidatorCallable] = ...,
85+
localize: bool = ...,
86+
disabled: bool = ...,
87+
label_suffix: str | None = ...,
5088
) -> None: ... # Args/kwargs can be any field params, uses kwargs for empty_label
5189
def compress(self, data_list: list[Any] | None) -> Lookup | None: ... # Data list can contain any lookup components
5290

@@ -57,7 +95,6 @@ class IsoDateTimeField(forms.DateTimeField):
5795

5896
class BaseCSVField(forms.Field):
5997
base_widget_class: _ClassLevelWidget = ...
60-
def __init__(self, *args: Any, **kwargs: Any) -> None: ... # Args/kwargs can be any field params for widget config
6198
def clean(self, value: Any) -> Any: ... # Cleaned values can be any valid field type
6299

63100
class BaseRangeField(BaseCSVField):
@@ -80,17 +117,51 @@ class ModelChoiceIterator(forms.models.ModelChoiceIterator):
80117
class ChoiceIteratorMixin:
81118
null_label: str | None
82119
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
120+
def __init__(self, *, null_label: str | None, null_value: Any) -> None: ...
84121

85122
class ChoiceField(ChoiceIteratorMixin, forms.ChoiceField):
86123
iterator = ChoiceIterator
87124
empty_label: str | None
88-
def __init__(self, *args: Any, **kwargs: Any) -> None: ... # Args/kwargs can be any field params for label config
125+
def __init__(
126+
self,
127+
*,
128+
choices: _ChoicesInput = (),
129+
required: bool = ...,
130+
widget: Widget | type[Widget] | None = ...,
131+
label: StrOrPromise | None = ...,
132+
initial: Any | None = ...,
133+
help_text: StrOrPromise = ...,
134+
error_messages: Mapping[str, StrOrPromise] | None = ...,
135+
show_hidden_initial: bool = ...,
136+
validators: Sequence[_ValidatorCallable] = ...,
137+
localize: bool = ...,
138+
disabled: bool = ...,
139+
label_suffix: str | None = ...,
140+
null_label: str | None,
141+
null_value: Any,
142+
) -> None: ...
89143

90144
class MultipleChoiceField(ChoiceIteratorMixin, forms.MultipleChoiceField):
91145
iterator = ChoiceIterator
92146
empty_label: str | None
93-
def __init__(self, *args: Any, **kwargs: Any) -> None: ... # Args/kwargs can be any field params, sets empty_label
147+
def __init__(
148+
self,
149+
*,
150+
choices: _ChoicesInput = (),
151+
required: bool = ...,
152+
widget: Widget | type[Widget] | None = ...,
153+
label: StrOrPromise | None = ...,
154+
initial: Any | None = ...,
155+
help_text: StrOrPromise = ...,
156+
error_messages: Mapping[str, StrOrPromise] | None = ...,
157+
show_hidden_initial: bool = ...,
158+
validators: Sequence[_ValidatorCallable] = ...,
159+
localize: bool = ...,
160+
disabled: bool = ...,
161+
label_suffix: str | None = ...,
162+
null_label: str | None,
163+
null_value: Any,
164+
) -> None: ...
94165

95166
class ModelChoiceField(ChoiceIteratorMixin, forms.ModelChoiceField[Any]):
96167
iterator = ModelChoiceIterator

stubs/django-filter/django_filters/filters.pyi

Lines changed: 82 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,19 @@ class BooleanFilter(Filter):
8787
class ChoiceFilter(Filter):
8888
field_class: type[Any] # Base class for choice-based filters
8989
null_value: Any # Null value can be any type (None, empty string, etc.)
90-
def __init__(self, *args: Any, **kwargs: Any) -> None: ... # Uses kwargs for null_value config
90+
def __init__(
91+
self,
92+
field_name: str | None = None,
93+
lookup_expr: str | None = None,
94+
*,
95+
null_value: Any = None,
96+
# Inherited from Filter
97+
label: str | None = None,
98+
method: Callable[..., Any] | str | None = None, # Filter methods can return various types
99+
distinct: bool = False,
100+
exclude: bool = False,
101+
**kwargs: Any, # Field kwargs stored as extra (required, help_text, etc.)
102+
) -> None: ...
91103
def filter(self, qs: QuerySet[Any], value: Any) -> QuerySet[Any]: ...
92104

93105
class TypedChoiceFilter(Filter):
@@ -101,7 +113,20 @@ class MultipleChoiceFilter(Filter):
101113
always_filter: bool
102114
conjoined: bool
103115
null_value: Any # Multiple choice null values vary by implementation
104-
def __init__(self, *args: Any, **kwargs: Any) -> None: ... # Uses kwargs for distinct, conjoined, null_value config
116+
def __init__(
117+
self,
118+
field_name: str | None = None,
119+
lookup_expr: str | None = None,
120+
*,
121+
distinct: bool = True, # Overrides distinct default
122+
conjoined: bool = False,
123+
null_value: Any = None,
124+
# Inherited from Filter
125+
label: str | None = None,
126+
method: Callable[..., Any] | str | None = None, # Filter methods can return various types
127+
exclude: bool = False,
128+
**kwargs: Any, # Field kwargs stored as extra (required, help_text, etc.)
129+
) -> None: ...
105130
def is_noop(self, qs: QuerySet[Any], value: Any) -> bool: ... # Value can be any filter input
106131
def filter(self, qs: QuerySet[Any], value: Any) -> QuerySet[Any]: ...
107132
def get_filter_predicate(self, v: Any) -> Q: ... # Predicate value can be any filter input type
@@ -126,15 +151,29 @@ class DurationFilter(Filter):
126151

127152
class QuerySetRequestMixin:
128153
queryset: QuerySet[Any] | None
129-
def __init__(self, *args: Any, **kwargs: Any) -> None: ... # Uses kwargs for queryset config
154+
def __init__(self, *, queryset: QuerySet[Any] | None) -> None: ...
130155
def get_request(self) -> Any: ... # Request can be HttpRequest or other request types
131156
def get_queryset(self, request: Any) -> QuerySet[Any]: ... # Request parameter accepts various request types
132157
@property
133158
def field(self) -> Field: ...
134159

135160
class ModelChoiceFilter(QuerySetRequestMixin, ChoiceFilter):
136161
field_class: type[ModelChoiceField] # More specific than parent ChoiceField
137-
def __init__(self, *args: Any, **kwargs: Any) -> None: ... # Uses kwargs for empty_label config
162+
def __init__(
163+
self,
164+
field_name: str | None = None,
165+
lookup_expr: str | None = None,
166+
*,
167+
queryset: QuerySet[Any] | None,
168+
# Inherited from ChoiceFilter
169+
null_value: Any = None,
170+
# Inherited from Filter
171+
label: str | None = None,
172+
method: Callable[..., Any] | str | None = None, # Filter methods can return various types
173+
distinct: bool = False,
174+
exclude: bool = False,
175+
**kwargs: Any, # Field kwargs stored as extra (required, help_text, etc.)
176+
) -> None: ...
138177

139178
class ModelMultipleChoiceFilter(QuerySetRequestMixin, MultipleChoiceFilter):
140179
field_class: type[ModelMultipleChoiceField] # More specific than parent MultipleChoiceField
@@ -159,7 +198,20 @@ class DateRangeFilter(ChoiceFilter):
159198
choices: list[tuple[str, str]] | None
160199
filters: dict[str, Filter] | None
161200
def __init__(
162-
self, choices: list[tuple[str, str]] | None = None, filters: dict[str, Filter] | None = None, *args: Any, **kwargs: Any
201+
self,
202+
choices: list[tuple[str, str]] | None = None,
203+
filters: dict[str, Filter] | None = None,
204+
field_name: str | None = None,
205+
lookup_expr: str | None = None,
206+
*,
207+
# Inherited from ChoiceFilter
208+
null_value: Any = None,
209+
# Inherited from Filter
210+
label: str | None = None,
211+
method: Callable[..., Any] | str | None = None, # Filter methods can return various types
212+
distinct: bool = False,
213+
exclude: bool = False,
214+
**kwargs: Any, # Field kwargs stored as extra (required, help_text, etc.)
163215
) -> None: ... # Uses args/kwargs for choice and filter configuration
164216
def filter(self, qs: QuerySet[Any], value: Any) -> QuerySet[Any]: ...
165217

@@ -186,14 +238,11 @@ class AllValuesMultipleFilter(MultipleChoiceFilter):
186238
class BaseCSVFilter(Filter):
187239
base_field_class: type[BaseCSVField] = ...
188240
field_class: type[Any] # Base class for CSV-based filters
189-
def __init__(self, *args: Any, **kwargs: Any) -> None: ... # Uses kwargs for help_text and widget config
190241

191-
class BaseInFilter(BaseCSVFilter):
192-
def __init__(self, *args: Any, **kwargs: Any) -> None: ... # Sets lookup_expr and passes through
242+
class BaseInFilter(BaseCSVFilter): ...
193243

194244
class BaseRangeFilter(BaseCSVFilter):
195245
base_field_class: type[BaseRangeField] = ...
196-
def __init__(self, *args: Any, **kwargs: Any) -> None: ... # Sets lookup_expr and passes through
197246

198247
class LookupChoiceFilter(Filter):
199248
field_class: type[forms.CharField]
@@ -205,7 +254,15 @@ class LookupChoiceFilter(Filter):
205254
field_name: str | None = None,
206255
lookup_choices: list[tuple[str, str]] | None = None,
207256
field_class: type[Field] | None = None,
208-
**kwargs: Any, # Handles empty_label and other field config
257+
*,
258+
# Inherited from ChoiceFilter
259+
null_value: Any = None,
260+
# Inherited from Filter
261+
label: str | None = None,
262+
method: Callable[..., Any] | str | None = None, # Filter methods can return various types
263+
distinct: bool = False,
264+
exclude: bool = False,
265+
**kwargs: Any, # Field kwargs stored as extra (required, help_text, etc.)
209266
) -> None: ...
210267
@classmethod
211268
def normalize_lookup(cls, lookup: Any) -> tuple[Any, str]: ...
@@ -219,7 +276,21 @@ class OrderingFilter(BaseCSVFilter, ChoiceFilter):
219276
field_class: type[BaseCSVField] # Inherits CSV field behavior for comma-separated ordering
220277
descending_fmt: str
221278
param_map: dict[str, str] | None
222-
def __init__(self, *args: Any, **kwargs: Any) -> None: ... # Uses kwargs for fields and field_labels config
279+
def __init__(
280+
self,
281+
field_name: str | None = None,
282+
lookup_expr: str | None = None,
283+
*,
284+
# Inherited from ChoiceFilter
285+
null_value: Any = None,
286+
# Inherited from Filter
287+
field_labels: dict[Any, Any] | None = None,
288+
label: str | None = None,
289+
method: Callable[..., Any] | str | None = None, # Filter methods can return various types
290+
distinct: bool = False,
291+
exclude: bool = False,
292+
**kwargs: Any, # Field kwargs stored as extra (required, help_text, etc.)
293+
) -> None: ...
223294
def get_ordering_value(self, param: str) -> str: ...
224295
def filter(self, qs: QuerySet[Any], value: Any) -> QuerySet[Any]: ...
225296
@classmethod

stubs/django-filter/django_filters/rest_framework/filters.pyi

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Any
1+
from typing import Any, reveal_type
22

33
from ..filters import (
44
AllValuesFilter,
@@ -67,5 +67,4 @@ __all__ = [
6767
]
6868

6969
# REST framework specific BooleanFilter that uses BooleanWidget by default
70-
class BooleanFilter(_BaseBooleanFilter):
71-
def __init__(self, *args: Any, **kwargs: Any) -> None: ... # Accepts any filter initialization params
70+
class BooleanFilter(_BaseBooleanFilter): ...

stubs/django-filter/django_filters/widgets.pyi

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ class LinkWidget(forms.Widget):
3030

3131
class SuffixedMultiWidget(forms.MultiWidget):
3232
suffixes: list[str]
33-
def __init__(self, *args: Any, **kwargs: Any) -> None: ... # Args/kwargs can be any widget params for MultiWidget
3433
def suffixed(self, name: str, suffix: str) -> str: ...
3534
# Widget value and context can contain any data types
3635
def get_context(self, name: str, value: Any, attrs: dict[str, Any] | None) -> dict[str, Any]: ...
@@ -70,16 +69,12 @@ class BaseCSVWidget(forms.Widget):
7069
# Can be widget class or instance - __init__ converts to instance via instantiation or deepcopy
7170
surrogate: type[Any] = ...
7271

73-
# Args/kwargs can be any widget params for surrogate init
74-
def __init__(self, *args: Any, **kwargs: Any) -> None: ...
7572
# CSV widget data can contain any types
7673
def value_from_datadict(self, data: Mapping[str, Any], files: Mapping[str, Any], name: str) -> list[str]: ...
7774
# Widget value and renderer can be any type
7875
def render(self, name: str, value: Any, attrs: dict[str, Any] | None = None, renderer: Any | None = None) -> SafeString: ...
7976

80-
class CSVWidget(BaseCSVWidget, forms.TextInput):
81-
# Args/kwargs can be any widget params, attrs for styling
82-
def __init__(self, *args: Any, attrs: dict[str, Any] | None = None, **kwargs: Any) -> None: ...
77+
class CSVWidget(BaseCSVWidget, forms.TextInput): ...
8378

8479
class QueryArrayWidget(BaseCSVWidget, forms.TextInput):
8580
# Query array widget data can contain any types

0 commit comments

Comments
 (0)