Mypy has no problem (at least not in 0.501). But the problem is with Python 3.6.0. Consider the following:
from collections import OrderedDict from typing import Dict def foo() -> Dict[str, int]: result: OrderedDict[str, int] = OrderedDict() result['two'] = 2 return result
This code will satisfy the requirements of mypy (0.501) and Python (3.6.0). However, if you replace Dict with OrderedDict , then mypy will still be happy, but doing it will die with TypeError: 'type' object is not subscriptable .
Interestingly, the Python interpreter dies when viewing the signature of the OrderedDict in the function signature, but gladly accepts it in the annotation of a type variable.
Anyway, my workaround for this is to use Dict instead of OrderedDict in the function signature (and adding a comment that this should be fixed if / when the Python interpreter learns to accept the correct signature).
Oren Ben-Kiki
source share