Skip to content

Add collections import in typing for type aliases to always work - #1456

Merged
JelleZijlstra merged 2 commits into
python:masterfrom
ilevkivskyi:add-collections-import-for-aliases
Jul 3, 2017
Merged

JelleZijlstra merged 2 commits into
python:masterfrom
ilevkivskyi:add-collections-import-for-aliases

Conversation

@ilevkivskyi

Copy link
Copy Markdown
Member

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 for DefaultDict, Deque etc).

The problem is that if collections are not in checked modules, then DefaultDict, Deque etc. are automatically translated to Any thus potentially hiding some errors, see python/mypy#2986 for an example.

@JelleZijlstra JelleZijlstra left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread stdlib/2/typing.pyi Outdated

from abc import abstractmethod, ABCMeta
from types import CodeType, FrameType, TracebackType
import collections # Needed by aliases like DefaultDict, Deque, etc.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, added the issue number.

@ilevkivskyi

Copy link
Copy Markdown
Member Author

Is it easy to fix this in mypy instead?

I think no.

It's a bit unfortunate to have to add this unused import to work around a mypy quirk.

It's not a mypy quirk. All other type checkers also need to manually load collections (or do something similar). There is no way to know from typeshed how e.g. DefaultDict is defined (it is just TypeAlias(object)).

@JelleZijlstra
JelleZijlstra merged commit 97737ce into python:master Jul 3, 2017
@JelleZijlstra

Copy link
Copy Markdown
Member

Thanks for the explanation!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incorrect (or absent) error message for DefaultDict

2 participants