What to do about this state of affairs?
? python -V && python -c "from typing import OrderedDict"
Python 3.7.3
? mypy -V && mypy -c "from typing import OrderedDict"
mypy 0.710+dev.5f08ccf029aa3046b15e1afc60743ba692e4758d
<string>:1: error: Module 'typing' has no attribute 'OrderedDict'
x: int = 'hi' also doesn't give any errors at runtime, so this is not an argument, import OrderedDict from collections, not from other places where it was imported for internal use.
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.
x: int = 'hi'also doesn't give any errors at runtime, so this is not an argument, importOrderedDictfromcollections, not from other places where it was imported for internal use.
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 typing.OrderedDict with mypy 0.740.
# 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)
collections.OrderedDict works, however.
# 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
VS Code:

Most helpful comment
It is still not possible to use
typing.OrderedDictwith mypy 0.740.collections.OrderedDictworks, however.