-
-
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
Conversation
Copied and integrated init type stubs from typeshed for ReturnList and ReturnDict.
_T = TypeVar("_T") | ||
_VT = TypeVar("_VT") | ||
_KT = TypeVar("_KT") | ||
|
||
class ReturnDict(dict): |
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.
@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 comment
The 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 ReturnDict
makes sense with non-string keys. Only string keys are JSON-serializable. But I'm OK with keeping this as is.
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.
From what I have seen so far, drf uses json.dumps
or json.dump
at some point, and the typeshed definition accepts Any
as argument. At runtime, the key types are restricted to str, int, float, bool or None
, from what I have seen in a brief investigation. I could limit the generics to those types, or even just to str
, but I am unsure whether it is worth it to deviate from the typeshed definition, since they are complex.
Added source reference of copied __init__ definitions. Added tests for copy and __reduce__ methods.
__init__
signatures of ReturnDict and ReturnListThere 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.
This is great, thanks!
Finally released in version 3.14.3 |
Fix
__init__
signatures of ReturnDict and ReturnListPR makes
serializer
argument in__init__
method ofReturnDict
andReturnList
position only and otherwise includeslist
anddict
type annotations from typeshed.Related issues