-
-
Notifications
You must be signed in to change notification settings - Fork 31.3k
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
Add generic version of OrderedDict to typing module #79522
Comments
The other collections from the collections module (namedtuple, deque, ChainMap, Counter, defaultdict) have generic versions in the typing module for use in type annotations. The problem is currently the following: from __future__ import annotations
import typing
from collections import OrderedDict
# Understood by mypy
def f(d: OrderedDict[str, str]) -> None:
pass
typing.get_type_hints(f) gives: Traceback (most recent call last):
File "foo.py", line 9, in <module>
typing.get_type_hints(f)
File "/usr/lib/python3.7/typing.py", line 1001, in get_type_hints
value = _eval_type(value, globalns, localns)
File "/usr/lib/python3.7/typing.py", line 260, in _eval_type
return t._evaluate(globalns, localns)
File "/usr/lib/python3.7/typing.py", line 464, in _evaluate
eval(self.__forward_code__, globalns, localns),
File "<string>", line 1, in <module>
TypeError: 'type' object is not subscriptable To fix this, a line like the following could be added to Lib/typing.py near line 1250: OrderedDict = _alias(collections.OrderedDict, (KT, VT)) There might be a reasoning for why this has not been done yet, but I have not been able to find any. If this is acceptable, I could prepare a PR. |
Given that this is in collections, I don't object. Ivan, what do you say? |
Yes, since we already have |
Also typing is technically still provisional, we can backport this to previous versions (at least to 3.7). |
Now that regular dicts are ordered, my expectation is that OrderedDict() is going to mostly fall into disuse (much like UserDict, UserList, UserString). That said, it would be nice if all of the collections classes had generic counterparts in the typing module. |
My reason for using OrderedDict was that the normal dict is not reversible. I needed to get the last element added, so I ended up doing something like next(reversed(ordered_dict)). Perhaps this would be something to add to the normal dict (for 3.8?). Then I could migrate away from OrderedDict. Shall I open a new issue for this? |
@itoijala Please see https://bugs.python.org/issue33462. It's on master. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: