-
-
Notifications
You must be signed in to change notification settings - Fork 121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix ReturnDict and ReturnList __init__ signatures #452
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,51 @@ | ||
from collections.abc import Iterator, MutableMapping | ||
from typing import Any | ||
from collections.abc import Iterable, Iterator, MutableMapping | ||
from typing import Any, TypeVar, overload | ||
|
||
from _typeshed import SupportsKeysAndGetItem | ||
from rest_framework.exceptions import ErrorDetail | ||
from rest_framework.fields import Field | ||
from rest_framework.serializers import BaseSerializer | ||
|
||
_T = TypeVar("_T") | ||
_VT = TypeVar("_VT") | ||
_KT = TypeVar("_KT") | ||
|
||
class ReturnDict(dict): | ||
serializer: BaseSerializer | ||
def __init__(self, serializer: BaseSerializer = ..., *args, **kwargs): ... | ||
# def __init__(self, serializer: BaseSerializer, *args, **kw @overload | ||
intgr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@overload | ||
def __init__(self, *, serializer: BaseSerializer) -> None: ... | ||
@overload | ||
def __init__(self: dict[str, _VT], *, serializer: BaseSerializer, **kwargs: _VT) -> None: ... | ||
@overload | ||
def __init__(self, __map: SupportsKeysAndGetItem[_KT, _VT], *, serializer: BaseSerializer) -> None: ... | ||
@overload | ||
def __init__( | ||
self: dict[str, _VT], __map: SupportsKeysAndGetItem[str, _VT], *, serializer: BaseSerializer, **kwargs: _VT | ||
) -> None: ... | ||
@overload | ||
def __init__(self, __iterable: Iterable[tuple[_KT, _VT]], *, serializer: BaseSerializer) -> None: ... | ||
@overload | ||
def __init__( | ||
self: dict[str, _VT], __iterable: Iterable[tuple[str, _VT]], *, serializer: BaseSerializer, **kwargs: _VT | ||
) -> None: ... | ||
# Next two overloads are for dict(string.split(sep) for string in iterable) | ||
# Cannot be Iterable[Sequence[_T]] or otherwise dict(["foo", "bar", "baz"]) is not an error | ||
@overload | ||
def __init__(self: dict[str, str], __iterable: Iterable[list[str]], *, serializer: BaseSerializer) -> None: ... | ||
@overload | ||
def __init__( | ||
self: dict[bytes, bytes], __iterable: Iterable[list[bytes]], *, serializer: BaseSerializer | ||
) -> None: ... | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wow these overloads are a lot more complicated than I imagined. I'm not sure if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From what I have seen so far, drf uses |
||
def copy(self) -> ReturnDict: ... | ||
def __reduce__(self) -> tuple[dict, tuple[dict]]: ... | ||
|
||
class ReturnList(list): | ||
serializer: BaseSerializer | ||
def __init__(self, serializer: BaseSerializer = ..., *args, **kwargs): ... | ||
@overload | ||
def __init__(self, *, serializer: BaseSerializer) -> None: ... | ||
@overload | ||
def __init__(self, __iterable: Iterable[_T], *, serializer: BaseSerializer) -> None: ... | ||
def __reduce__(self) -> tuple[dict, tuple[dict]]: ... | ||
|
||
class BoundField: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't it be generic?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can make it generic, but I am unsure about the wider impact this change has.
From what i have seen so far, only the new tests would break with that change.
Would you make the serializer argument/instance variable generic as well? (with a lower bound of
BaseSerializer[Any]
)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it makes sense to make key/value generic, like dict, since it subclasses dict. Don't bother with generic serializer, unless there's a particular reason to do so.