Skip to content

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

Merged
merged 2 commits into from
Oct 22, 2019

Conversation

simonjayhawkins
Copy link
Member

@simonjayhawkins simonjayhawkins added the Typing type annotations, mypy/pyright type checking label Oct 22, 2019
Copy link
Member

@WillAyd WillAyd left a 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:
Copy link
Member

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

@@ -25,6 +35,7 @@
Scalar = Union[str, int, float, bool]
Axis = Union[str, int]
Ordered = Optional[bool]
Serializable = Union[Scalar, Sequence, Mapping]
Copy link
Member

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

Copy link
Member Author

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 and List for now

agreed.

OK with this move but can you change this to JSONSerializable

sure.

Copy link
Member

@WillAyd WillAyd left a 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

@simonjayhawkins simonjayhawkins added this to the 1.0 milestone Oct 22, 2019
@simonjayhawkins
Copy link
Member Author

lgtm merge on green.

shows green on Travis, github shows in progress. will merge.

Not sure if overkill but note in contributing guide may be useful as well for follow up

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.

@simonjayhawkins simonjayhawkins merged commit 2ca2161 into pandas-dev:master Oct 22, 2019
@WillAyd
Copy link
Member

WillAyd commented Oct 22, 2019

probably go with Iterable, and use Sequence where needed.

Both of these would allow strings then right? Might be less universally applicable to go from List to one of these

@simonjayhawkins
Copy link
Member Author

probably go with Iterable, and use Sequence where needed.

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.
(we should probably have disallow_any_generics in our config.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Typing type annotations, mypy/pyright type checking
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants