Repository navigation
Add collections import in typing for type aliases to always work - #1456
Conversation
JelleZijlstra
left a comment
There was a problem hiding this comment.
Is it easy to fix this in mypy instead? It's a bit unfortunate to have to add this unused import to work around a mypy quirk.
|
|
||
| from abc import abstractmethod, ABCMeta | ||
| from types import CodeType, FrameType, TracebackType | ||
| import collections # Needed by aliases like DefaultDict, Deque, etc. |
There was a problem hiding this comment.
Could you add a reference to the mypy issue that makes clear why mypy needs this import? It's not really obvious from this comment why the import is necessary.
There was a problem hiding this comment.
OK, added the issue number.
I think no.
It's not a mypy quirk. All other type checkers also need to manually load |
|
Thanks for the explanation! |
Fixes python/mypy#2986
This is a simplest fix, and all other fixes will require complicated manual loading of
collections(since it contains the actual class definitions forDefaultDict,Dequeetc).The problem is that if
collectionsare not in checked modules, thenDefaultDict,Dequeetc. are automatically translated toAnythus potentially hiding some errors, see python/mypy#2986 for an example.