Skip to content

Commit

Permalink
Use __new__ instead of __init__ in serializers
Browse files Browse the repository at this point in the history
  • Loading branch information
Viicos committed Jan 8, 2023
1 parent 728da9c commit adcbe32
Showing 1 changed file with 51 additions and 7 deletions.
58 changes: 51 additions & 7 deletions rest_framework-stubs/serializers.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from collections.abc import Callable, Iterable, Iterator, Mapping, MutableMapping, Sequence
from typing import Any, Generic, NoReturn, TypeVar
from typing import Any, Generic, NoReturn, TypeVar, overload
from _typeshed import Self

from django.core.exceptions import ValidationError as DjangoValidationError
Expand Down Expand Up @@ -81,13 +81,57 @@ class BaseSerializer(Generic[_IN], Field[Any, Any, Any, _IN]):
instance: _IN | None
initial_data: Any
_context: dict[str, Any]
def __new__(cls: type[Self], *args: Any, **kwargs: Any) -> Self: ...
def __class_getitem__(cls, *args, **kwargs): ...
def __init__(
self,
# When both __init__ and __new__ are present, mypy will prefer __init__
@overload
def __new__(
cls: type[Self],
instance: Iterable[_IN] | None = ...,
data: Any = ...,
partial: bool = ...,
many: Literal[True] = ...,
allow_empty: bool = ...,
context: dict[str, Any] = ...,
read_only: bool = ...,
write_only: bool = ...,
required: bool = ...,
default: Any = ...,
initial: Any = ...,
source: str = ...,
label: str = ...,
help_text: str = ...,
style: dict[str, Any] = ...,
error_messages: dict[str, str] = ...,
validators: Sequence[Validator[Any]] | None = ...,
allow_null: bool = ...,
) -> ListSerializer[_IN]: ...
@overload
def __new__(
cls: type[Self],
instance: _IN | None = ...,
data: Any = ...,
partial: bool = ...,
many: Literal[False] = ...,
allow_empty: bool = ...,
context: dict[str, Any] = ...,
read_only: bool = ...,
write_only: bool = ...,
required: bool = ...,
default: Any = ...,
initial: Any = ...,
source: str = ...,
label: str = ...,
help_text: str = ...,
style: dict[str, Any] = ...,
error_messages: dict[str, str] = ...,
validators: Sequence[Validator[Any]] | None = ...,
allow_null: bool = ...,
) -> Self: ...
def __new__(
cls,
instance: _IN | Iterable[_IN] | None = ...,
data: Any = ...,
partial: bool = ...,
many: bool = ...,
allow_empty: bool = ...,
context: dict[str, Any] = ...,
Expand All @@ -103,7 +147,7 @@ class BaseSerializer(Generic[_IN], Field[Any, Any, Any, _IN]):
error_messages: dict[str, str] = ...,
validators: Sequence[Validator[Any]] | None = ...,
allow_null: bool = ...,
): ...
) -> ListSerializer[_IN] | Self: ...
@classmethod
def many_init(cls, *args: Any, **kwargs: Any) -> BaseSerializer: ...
def is_valid(self, raise_exception: bool = ...) -> bool: ...
Expand Down Expand Up @@ -159,7 +203,7 @@ class ListSerializer(
allow_empty: bool | None
def __init__(
self,
instance: _IN | None = ...,
instance: Iterable[_IN] | None = ...,
data: Any = ...,
partial: bool = ...,
context: dict[str, Any] = ...,
Expand All @@ -177,7 +221,7 @@ class ListSerializer(
error_messages: dict[str, str] = ...,
validators: Sequence[Validator[list[Any]]] | None = ...,
allow_null: bool = ...,
): ...
) -> None: ...
def get_initial(self) -> list[Mapping[Any, Any]]: ...
def validate(self, attrs: Any) -> Any: ...
@property
Expand Down

0 comments on commit adcbe32

Please sign in to comment.