Skip to content
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

Use fully qualified names for ambiguous class names resembling builtins. #8425

Merged
merged 5 commits into from Feb 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions mypy/messages.py
Expand Up @@ -42,6 +42,21 @@
from mypy.errorcodes import ErrorCode
from mypy import message_registry, errorcodes as codes

TYPES_FOR_UNIMPORTED_HINTS = {
'typing.Any',
'typing.Callable',
'typing.Dict',
'typing.Iterable',
'typing.Iterator',
'typing.List',
'typing.Optional',
'typing.Set',
'typing.Tuple',
'typing.TypeVar',
'typing.Union',
'typing.cast',
} # type: Final


ARG_CONSTRUCTOR_NAMES = {
ARG_POS: "Arg",
Expand Down Expand Up @@ -1720,6 +1735,9 @@ def find_type_overlaps(*types: Type) -> Set[str]:
for type in types:
for inst in collect_all_instances(type):
d.setdefault(inst.type.name, set()).add(inst.type.fullname)
for shortname in d.keys():
if 'typing.{}'.format(shortname) in TYPES_FOR_UNIMPORTED_HINTS:
d[shortname].add('typing.{}'.format(shortname))

overlaps = set() # type: Set[str]
for fullnames in d.values():
Expand Down
19 changes: 3 additions & 16 deletions mypy/semanal.py
Expand Up @@ -81,7 +81,9 @@
from mypy.typevars import fill_typevars
from mypy.visitor import NodeVisitor
from mypy.errors import Errors, report_internal_error
from mypy.messages import best_matches, MessageBuilder, pretty_seq, SUGGESTED_TEST_FIXTURES
from mypy.messages import (
best_matches, MessageBuilder, pretty_seq, SUGGESTED_TEST_FIXTURES, TYPES_FOR_UNIMPORTED_HINTS
)
from mypy.errorcodes import ErrorCode
from mypy import message_registry, errorcodes as codes
from mypy.types import (
Expand Down Expand Up @@ -120,21 +122,6 @@

T = TypeVar('T')

TYPES_FOR_UNIMPORTED_HINTS = {
'typing.Any',
'typing.Callable',
'typing.Dict',
'typing.Iterable',
'typing.Iterator',
'typing.List',
'typing.Optional',
'typing.Set',
'typing.Tuple',
'typing.TypeVar',
'typing.Union',
'typing.cast',
} # type: Final


# Special cased built-in classes that are needed for basic functionality and need to be
# available very early on.
Expand Down
20 changes: 20 additions & 0 deletions test-data/unit/check-basic.test
Expand Up @@ -75,6 +75,26 @@ if int():
x = 1


[case testIncompatibleAssignmentAmbiguousShortnames]

class Any: pass
class List: pass
class Dict: pass
class Iterator: pass

x = Any()
x = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "__main__.Any")
Copy link
Collaborator

Choose a reason for hiding this comment

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

It would be good to test a few other type names as well, such as list / List. (Lower-case list might not work, since it's not in the TYPES_FOR_UNIMPORTED_HINTS set.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've added a few more types from TYPES_FOR_UNIMPORTED_HINTS.


y = List()
y = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "__main__.List")

z = Dict()
z = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "__main__.Dict")

w = Iterator()
w = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "__main__.Iterator")


-- Simple functions and calling
-- ----------------------------

Expand Down