-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
CLN: replace Dict with Mapping to annotate arguments #29155
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
CLN: replace Dict with Mapping to annotate arguments #29155
Conversation
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.
Cool thanks for taking this on as a follow up. Looks good just one comment on the JSON stuff, which is unfortunately a little wonky with the extension
@@ -351,7 +351,7 @@ def _finalize(self, categories, ordered: Ordered, fastpath: bool = False) -> Non | |||
self._ordered = ordered if ordered is not ordered_sentinel else None | |||
self._ordered_from_sentinel = ordered is ordered_sentinel | |||
|
|||
def __setstate__(self, state: Dict[str_type, Any]) -> None: | |||
def __setstate__(self, state: MutableMapping[str_type, Any]) -> 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.
This is really nice; gives indication that the argument is mutated
pandas/_typing.py
Outdated
@@ -25,6 +35,7 @@ | |||
Scalar = Union[str, int, float, bool] | |||
Axis = Union[str, int] | |||
Ordered = Optional[bool] | |||
Serializable = Union[Scalar, Sequence, Mapping] |
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.
Sorry comment might have gotten lost? OK with this move but can you change this to JSONSerializable
? I think specific that that piece of IO (others may impose their own requirements)
Also I think this should stay with Dict
and List
for now; the C extension isn't as flexible as you would hope. Using other types will surface buggy behavior that MyPy can't detect
>>> import pandas._libs.json as json
>>> json.dumps(range(3))
'{"start":0,"stop":3}' # Probably shouldn't output as a dictionary
>>> json.dumps(UserDict({"a": "b"}))
'{"data":{"a":"b"}}' # Adds a "data" key the user probably didn't want
FWIW the standard module doesn't allow those to be serialized:
>>> import json
>>> json.dumps(range(3))
TypeError: Object of type range is not JSON serializable
>>> json.dumps(UserDict({"a": "b"}))
TypeError: Object of type UserDict is not JSON serializable
So maybe something can be borrowed from stdlib or typeshed for this alias
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.
So maybe something can be borrowed from stdlib or typeshed for this alias
typeshed just uses Any
Also I think this should stay with
Dict
andList
for now
agreed.
OK with this move but can you change this to
JSONSerializable
sure.
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.
lgtm merge on green.
Not sure if overkill but note in contributing guide may be useful as well for follow up
shows green on Travis, github shows in progress. will merge.
i'll do similar changes for List first, xref https://docs.python.org/3/library/typing.html#typing.List probably go with Iterable, and use Sequence where needed. see how that goes and then update guide accordingly. |
Both of these would allow strings then right? Might be less universally applicable to go from List to one of these |
if these are typed as generic, then maybe so, but not if being precise. |
xref https://docs.python.org/3/library/typing.html#typing.Dict
cc @WillAyd