-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Bring _collections_abc
closer to runtime definition
#6312
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
Conversation
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
1 similar comment
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Diff from mypy_primer, showing the effect of this PR on open source code: dedupe (https://github.com/dedupeio/dedupe.git)
- dedupe/training.py:80: error: Argument 1 to "union" of "frozenset" has incompatible type "*_dict_values[Predicate, FrozenSet[int]]"; expected "FrozenSet[<nothing>]"
+ dedupe/training.py:80: error: Argument 1 to "union" of "frozenset" has incompatible type "*dict_values[Predicate, FrozenSet[int]]"; expected "FrozenSet[<nothing>]"
- dedupe/training.py:80: error: Argument 1 to "union" of "frozenset" has incompatible type "*_dict_values[Predicate, FrozenSet[int]]"; expected "FrozenSet[Any]"
+ dedupe/training.py:80: error: Argument 1 to "union" of "frozenset" has incompatible type "*dict_values[Predicate, FrozenSet[int]]"; expected "FrozenSet[Any]"
- dedupe/training.py:297: error: Argument 1 to "union" of "frozenset" has incompatible type "*_dict_values[Predicate, FrozenSet[int]]"; expected "FrozenSet[<nothing>]"
+ dedupe/training.py:297: error: Argument 1 to "union" of "frozenset" has incompatible type "*dict_values[Predicate, FrozenSet[int]]"; expected "FrozenSet[<nothing>]"
SinbadCogs (https://github.com/mikeshardmind/SinbadCogs.git)
- relays/relays.py:114: error: Argument 1 to <tuple> has incompatible type "*_dict_values[str, OnewayRelay]"; expected "Union[NwayRelay, OnewayRelay]"
+ relays/relays.py:114: error: Argument 1 to <tuple> has incompatible type "*dict_values[str, OnewayRelay]"; expected "Union[NwayRelay, OnewayRelay]"
- relays/relays.py:114: error: Argument 2 to <tuple> has incompatible type "*_dict_values[str, NwayRelay]"; expected "Union[NwayRelay, OnewayRelay]"
+ relays/relays.py:114: error: Argument 2 to <tuple> has incompatible type "*dict_values[str, NwayRelay]"; expected "Union[NwayRelay, OnewayRelay]"
|
from _collections_abc import dict_keys
dict_keys[int, int] Will type check fine, but raise errors at runtime. I think this is true for a lot of the |
I agree that this is unfortunate, but I'm not entirely sure what your point is. Yes it's not great that the stub differs from the runtime implementation in this case, and yes the stub should generally be as close to the runtime implementation as possible. But having these classes be generic in the stub file allows type checkers to perform far more sophisticated type inferences. If you have a valid use case for wanting to parameterize |
Previously this definition was |
Currently, the following code succeeds at runtime, but fails mypy:
This PR proposes bringing the
_collections_abc
stub closer to its runtime definition by moving the definitions ofdict_keys
,dict_values
anddict_items
frombuiltins
to_collections_abc
.I have also added
@final
to these classes, as they cannot be subclassed at runtime, and added# type: ignore
comments to_OrderedDictKeysView
/_OrderedDictValuesView
/_OrderedDictItemsView
. (These are indeed subclasses ofdict_keys
/dict_values
/dict_items
, even though the three classes cannot be subclassed at runtime; see #6299.)