1
1
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
3
3
from _typeshed import Self
4
4
5
5
from django .core .exceptions import ValidationError as DjangoValidationError
@@ -81,13 +81,57 @@ class BaseSerializer(Generic[_IN], Field[Any, Any, Any, _IN]):
81
81
instance : _IN | None
82
82
initial_data : Any
83
83
_context : dict [str , Any ]
84
- def __new__ (cls : type [Self ], * args : Any , ** kwargs : Any ) -> Self : ...
85
84
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 ],
88
111
instance : _IN | None = ...,
89
112
data : Any = ...,
90
113
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 = ...,
91
135
many : bool = ...,
92
136
allow_empty : bool = ...,
93
137
context : dict [str , Any ] = ...,
@@ -103,7 +147,7 @@ class BaseSerializer(Generic[_IN], Field[Any, Any, Any, _IN]):
103
147
error_messages : dict [str , str ] = ...,
104
148
validators : Sequence [Validator [Any ]] | None = ...,
105
149
allow_null : bool = ...,
106
- ): ...
150
+ ) -> ListSerializer [ _IN ] | Self : ...
107
151
@classmethod
108
152
def many_init (cls , * args : Any , ** kwargs : Any ) -> BaseSerializer : ...
109
153
def is_valid (self , raise_exception : bool = ...) -> bool : ...
@@ -159,7 +203,7 @@ class ListSerializer(
159
203
allow_empty : bool | None
160
204
def __init__ (
161
205
self ,
162
- instance : _IN | None = ...,
206
+ instance : Iterable [ _IN ] | None = ...,
163
207
data : Any = ...,
164
208
partial : bool = ...,
165
209
context : dict [str , Any ] = ...,
@@ -177,7 +221,7 @@ class ListSerializer(
177
221
error_messages : dict [str , str ] = ...,
178
222
validators : Sequence [Validator [list [Any ]]] | None = ...,
179
223
allow_null : bool = ...,
180
- ): ...
224
+ ) -> None : ...
181
225
def get_initial (self ) -> list [Mapping [Any , Any ]]: ...
182
226
def validate (self , attrs : Any ) -> Any : ...
183
227
@property
0 commit comments