-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
mypy doesn't support typing.OrderedDict #6904
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
Comments
|
How can we provide type annotations for OrderedDict keys/values? |
You have two options: Either use: from __future__ import annotations
from collections import OrderedDict
def order(x) -> OrderedDict[str, int]: ... but it works only on Python 3.7+ Or use: from collections import OrderedDict
def order(x) -> 'OrderedDict[str, int]': ... it works on all Python 3 versions but a bit ugly. Also it is all in the docs, read them. For example https://mypy.readthedocs.io/en/latest/common_issues.html#using-classes-that-are-generic-in-stubs-but-not-at-runtime |
Thank you. |
I don't think this is right. As of Python 3.7.2, typing includes a copy of collections.OrderedDict that is listed in the docs as "A generic version of collections.OrderedDict". This behaves the same as collections.OrderedDict at runtime, but additionally can be subscripted so type annotations do work. In other words, it is a class that is generic both in stubs and at runtime. So the fact that mypy doesn't work correctly with it for Python 3.7.2+ is an issue. |
More specifically, the issue is that mypy isn't using the latest version of typeshed which does have a stub for OrderedDict. |
The next release of mypy will include more recent changes to typeshed. |
It is still not possible to use # mypy -c 'from typing import OrderedDict; o: OrderedDict[str, str] = OrderedDict()'
<string>:1: error: Variable "typing.OrderedDict" is not valid as a type
<string>:1: error: "TypeAlias" not callable
Found 2 errors in 1 file (checked 1 source file)
# mypy -c 'from collections import OrderedDict; o: OrderedDict[str, str] = OrderedDict()'
Success: no issues found in 1 source file |
Because this is still not fixed (as of master, even) and I had to dive into a really ugly workaround, figured I'd share it for fellow Googlers. Context: I wanted to subclass OrderedDict with a specific key type and a to-be-defined-later value type so as to share common methods between two classes with different value types. I also wanted it to actually work in VS Code properly. This is how to make that work for the current version of mypy: from typing import TYPE_CHECKING, TypeVar
T_co = TypeVar("T_co", covariant=True)
# Workaround
from collections import OrderedDict
if not TYPE_CHECKING:
class OrderedDict(OrderedDict):
def __class_getitem__(cls, types):
return cls
# /Workaround
class Struct(OrderedDict[str, T_co]):
pass
test = Struct["str"]()
tt = test.get("a", "b")
print(tt) $ mypy test.py
Success: no issues found in 1 source file
$ python test.py
b |
Python 3.6 does not support `typing.OrderedDict`. See python/mypy#6904.
Python 3.6 does not support `typing.OrderedDict`. See python/mypy#6904.
Python 3.6 does not support `typing.OrderedDict`. See python/mypy#6904.
Followup after #72211 - as python/mypy#6904 (comment) in Python-3.7+ world OrderedDict from collections can be used directly for type annotations
Followup after #72211 - as python/mypy#6904 (comment) in Python-3.7+ world OrderedDict from collections can be used directly for type annotations
What to do about this state of affairs?
The text was updated successfully, but these errors were encountered: