Skip to content

Commit adcbe32

Browse files
committed
Use __new__ instead of __init__ in serializers
1 parent 728da9c commit adcbe32

File tree

1 file changed

+51
-7
lines changed

1 file changed

+51
-7
lines changed

Diff for: rest_framework-stubs/serializers.pyi

+51-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from collections.abc import Callable, Iterable, Iterator, Mapping, MutableMapping, Sequence
2-
from typing import Any, Generic, NoReturn, TypeVar
2+
from typing import Any, Generic, NoReturn, TypeVar, overload
33
from _typeshed import Self
44

55
from django.core.exceptions import ValidationError as DjangoValidationError
@@ -81,13 +81,57 @@ class BaseSerializer(Generic[_IN], Field[Any, Any, Any, _IN]):
8181
instance: _IN | None
8282
initial_data: Any
8383
_context: dict[str, Any]
84-
def __new__(cls: type[Self], *args: Any, **kwargs: Any) -> Self: ...
8584
def __class_getitem__(cls, *args, **kwargs): ...
86-
def __init__(
87-
self,
85+
# When both __init__ and __new__ are present, mypy will prefer __init__
86+
@overload
87+
def __new__(
88+
cls: type[Self],
89+
instance: Iterable[_IN] | None = ...,
90+
data: Any = ...,
91+
partial: bool = ...,
92+
many: Literal[True] = ...,
93+
allow_empty: bool = ...,
94+
context: dict[str, Any] = ...,
95+
read_only: bool = ...,
96+
write_only: bool = ...,
97+
required: bool = ...,
98+
default: Any = ...,
99+
initial: Any = ...,
100+
source: str = ...,
101+
label: str = ...,
102+
help_text: str = ...,
103+
style: dict[str, Any] = ...,
104+
error_messages: dict[str, str] = ...,
105+
validators: Sequence[Validator[Any]] | None = ...,
106+
allow_null: bool = ...,
107+
) -> ListSerializer[_IN]: ...
108+
@overload
109+
def __new__(
110+
cls: type[Self],
88111
instance: _IN | None = ...,
89112
data: Any = ...,
90113
partial: bool = ...,
114+
many: Literal[False] = ...,
115+
allow_empty: bool = ...,
116+
context: dict[str, Any] = ...,
117+
read_only: bool = ...,
118+
write_only: bool = ...,
119+
required: bool = ...,
120+
default: Any = ...,
121+
initial: Any = ...,
122+
source: str = ...,
123+
label: str = ...,
124+
help_text: str = ...,
125+
style: dict[str, Any] = ...,
126+
error_messages: dict[str, str] = ...,
127+
validators: Sequence[Validator[Any]] | None = ...,
128+
allow_null: bool = ...,
129+
) -> Self: ...
130+
def __new__(
131+
cls,
132+
instance: _IN | Iterable[_IN] | None = ...,
133+
data: Any = ...,
134+
partial: bool = ...,
91135
many: bool = ...,
92136
allow_empty: bool = ...,
93137
context: dict[str, Any] = ...,
@@ -103,7 +147,7 @@ class BaseSerializer(Generic[_IN], Field[Any, Any, Any, _IN]):
103147
error_messages: dict[str, str] = ...,
104148
validators: Sequence[Validator[Any]] | None = ...,
105149
allow_null: bool = ...,
106-
): ...
150+
) -> ListSerializer[_IN] | Self: ...
107151
@classmethod
108152
def many_init(cls, *args: Any, **kwargs: Any) -> BaseSerializer: ...
109153
def is_valid(self, raise_exception: bool = ...) -> bool: ...
@@ -159,7 +203,7 @@ class ListSerializer(
159203
allow_empty: bool | None
160204
def __init__(
161205
self,
162-
instance: _IN | None = ...,
206+
instance: Iterable[_IN] | None = ...,
163207
data: Any = ...,
164208
partial: bool = ...,
165209
context: dict[str, Any] = ...,
@@ -177,7 +221,7 @@ class ListSerializer(
177221
error_messages: dict[str, str] = ...,
178222
validators: Sequence[Validator[list[Any]]] | None = ...,
179223
allow_null: bool = ...,
180-
): ...
224+
) -> None: ...
181225
def get_initial(self) -> list[Mapping[Any, Any]]: ...
182226
def validate(self, attrs: Any) -> Any: ...
183227
@property

0 commit comments

Comments
 (0)