Skip to content

Commit

Permalink
Use fully qualified names for ambiguous class names resembling builti…
Browse files Browse the repository at this point in the history
…ns. (#8425)
  • Loading branch information
Muks14x committed Feb 27, 2020
1 parent d128158 commit ef0b0df
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 16 deletions.
18 changes: 18 additions & 0 deletions mypy/messages.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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")

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

0 comments on commit ef0b0df

Please sign in to comment.