Skip to content

Commit e698eb1

Browse files
committed
Add test for ambiguous short names in error messages
1 parent 6076943 commit e698eb1

File tree

5 files changed

+39
-26
lines changed

5 files changed

+39
-26
lines changed

mypy/messages.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
from textwrap import dedent
1616
import builtins
1717

18-
import typing
1918
from typing import cast, List, Dict, Any, Sequence, Iterable, Tuple, Set, Optional, Union
2019
from typing_extensions import Final
2120

@@ -44,6 +43,21 @@
4443
from mypy.errorcodes import ErrorCode
4544
from mypy import message_registry, errorcodes as codes
4645

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

4862
ARG_CONSTRUCTOR_NAMES = {
4963
ARG_POS: "Arg",
@@ -1720,14 +1734,14 @@ def find_type_overlaps(*types: Type) -> Set[str]:
17201734
"""
17211735
d = {} # type: Dict[str, Set[str]]
17221736
builtin_types = set(dir(builtins))
1723-
typing_types = set(dir(typing))
17241737
for type in types:
17251738
for inst in collect_all_instances(type):
17261739
d.setdefault(inst.type.name, set()).add(inst.type.fullname)
1727-
if inst.type.name in builtin_types:
1728-
d[inst.type.name].add('builtins.{}'.format(inst.type.name))
1729-
if inst.type.name in typing_types:
1730-
d[inst.type.name].add('typing.{}'.format(inst.type.name))
1740+
for shortname in d.keys():
1741+
if shortname in builtin_types:
1742+
d[shortname].add('builtins.{}'.format(shortname))
1743+
if 'typing.{}'.format(shortname) in TYPES_FOR_UNIMPORTED_HINTS:
1744+
d[shortname].add('typing.{}'.format(shortname))
17311745

17321746
overlaps = set() # type: Set[str]
17331747
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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,18 @@ if int():
7575
x = 1
7676

7777

78+
[case testIncompatibleAssignmentAmbiguousShortnames]
79+
80+
class Any: pass
81+
class float: pass
82+
83+
x = Any()
84+
x = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "__main__.Any")
85+
86+
y = float()
87+
y = 1.0 # E: Incompatible types in assignment (expression has type "builtins.float", variable has type "__main__.float")
88+
89+
7890
-- Simple functions and calling
7991
-- ----------------------------
8092

test-data/unit/fine-grained.test

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5137,8 +5137,8 @@ def g(x: T) -> T:
51375137
pass
51385138
[out]
51395139
==
5140-
a.py:2: error: Incompatible types in assignment (expression has type "c.T", variable has type "int")
5141-
a.py:2: error: Argument "x" has incompatible type "int"; expected "c.T"
5140+
a.py:2: error: Incompatible types in assignment (expression has type "T", variable has type "int")
5141+
a.py:2: error: Argument "x" has incompatible type "int"; expected "T"
51425142

51435143
[case testGenericFineCallableToGenericClass]
51445144
import a
@@ -7055,7 +7055,7 @@ class T: pass
70557055
a.py:2: error: No overload variant of "f" matches argument type "int"
70567056
a.py:2: note: Possible overload variants:
70577057
a.py:2: note: def f(x: C) -> None
7058-
a.py:2: note: def f(x: c.T) -> c.T
7058+
a.py:2: note: def f(x: T) -> T
70597059

70607060
[case testOverloadsToNonOverloaded]
70617061
import a

test-data/unit/pythoneval.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -904,7 +904,7 @@ reveal_type(o6)
904904

905905
[out]
906906
_testCollectionsAliases.py:5: note: Revealed type is 'collections.Counter[builtins.int]'
907-
_testCollectionsAliases.py:6: error: Invalid index type "str" for "collections.Counter[int]"; expected type "int"
907+
_testCollectionsAliases.py:6: error: Invalid index type "str" for "Counter[int]"; expected type "int"
908908
_testCollectionsAliases.py:9: note: Revealed type is 'collections.ChainMap[builtins.int, builtins.str]'
909909
_testCollectionsAliases.py:12: note: Revealed type is 'collections.deque[builtins.int]'
910910
_testCollectionsAliases.py:15: note: Revealed type is 'collections.Counter[builtins.int*]'

0 commit comments

Comments
 (0)