Skip to content

Commit ef0b0df

Browse files
authored
Use fully qualified names for ambiguous class names resembling builtins. (#8425)
1 parent d128158 commit ef0b0df

File tree

3 files changed

+41
-16
lines changed

3 files changed

+41
-16
lines changed

mypy/messages.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,21 @@
4242
from mypy.errorcodes import ErrorCode
4343
from mypy import message_registry, errorcodes as codes
4444

45+
TYPES_FOR_UNIMPORTED_HINTS = {
46+
'typing.Any',
47+
'typing.Callable',
48+
'typing.Dict',
49+
'typing.Iterable',
50+
'typing.Iterator',
51+
'typing.List',
52+
'typing.Optional',
53+
'typing.Set',
54+
'typing.Tuple',
55+
'typing.TypeVar',
56+
'typing.Union',
57+
'typing.cast',
58+
} # type: Final
59+
4560

4661
ARG_CONSTRUCTOR_NAMES = {
4762
ARG_POS: "Arg",
@@ -1720,6 +1735,9 @@ def find_type_overlaps(*types: Type) -> Set[str]:
17201735
for type in types:
17211736
for inst in collect_all_instances(type):
17221737
d.setdefault(inst.type.name, set()).add(inst.type.fullname)
1738+
for shortname in d.keys():
1739+
if 'typing.{}'.format(shortname) in TYPES_FOR_UNIMPORTED_HINTS:
1740+
d[shortname].add('typing.{}'.format(shortname))
17231741

17241742
overlaps = set() # type: Set[str]
17251743
for fullnames in d.values():

mypy/semanal.py

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@
8181
from mypy.typevars import fill_typevars
8282
from mypy.visitor import NodeVisitor
8383
from mypy.errors import Errors, report_internal_error
84-
from mypy.messages import best_matches, MessageBuilder, pretty_seq, SUGGESTED_TEST_FIXTURES
84+
from mypy.messages import (
85+
best_matches, MessageBuilder, pretty_seq, SUGGESTED_TEST_FIXTURES, TYPES_FOR_UNIMPORTED_HINTS
86+
)
8587
from mypy.errorcodes import ErrorCode
8688
from mypy import message_registry, errorcodes as codes
8789
from mypy.types import (
@@ -120,21 +122,6 @@
120122

121123
T = TypeVar('T')
122124

123-
TYPES_FOR_UNIMPORTED_HINTS = {
124-
'typing.Any',
125-
'typing.Callable',
126-
'typing.Dict',
127-
'typing.Iterable',
128-
'typing.Iterator',
129-
'typing.List',
130-
'typing.Optional',
131-
'typing.Set',
132-
'typing.Tuple',
133-
'typing.TypeVar',
134-
'typing.Union',
135-
'typing.cast',
136-
} # type: Final
137-
138125

139126
# Special cased built-in classes that are needed for basic functionality and need to be
140127
# available very early on.

test-data/unit/check-basic.test

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,26 @@ if int():
7575
x = 1
7676

7777

78+
[case testIncompatibleAssignmentAmbiguousShortnames]
79+
80+
class Any: pass
81+
class List: pass
82+
class Dict: pass
83+
class Iterator: pass
84+
85+
x = Any()
86+
x = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "__main__.Any")
87+
88+
y = List()
89+
y = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "__main__.List")
90+
91+
z = Dict()
92+
z = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "__main__.Dict")
93+
94+
w = Iterator()
95+
w = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "__main__.Iterator")
96+
97+
7898
-- Simple functions and calling
7999
-- ----------------------------
80100

0 commit comments

Comments
 (0)