Skip to content

Commit 19629d5

Browse files
ilevkivskyigvanrossum
authored andcommitted
Fix crashes with import * (#2445)
Fixes #2315 Fixes #2172 There were reported some crashes of mypy when using import * and type aliases (only two above issues seem to be still open, others are closed as duplicates). It looks like this is because visit_import_all in semanal.py does not copy type_override for imported nodes (visit_import_from does, so that the crashes reported happen only with import *). I added few tests based on (extended) examples discussed in bug reports. Since the reason is clear and the fix is straightforward (one line) I think more sophisticated tests are not required.
1 parent f178a6c commit 19629d5

File tree

2 files changed

+78
-1
lines changed

2 files changed

+78
-1
lines changed

mypy/semanal.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1090,7 +1090,8 @@ def visit_import_all(self, i: ImportAll) -> None:
10901090
name, existing_symbol, node, i):
10911091
continue
10921092
self.add_symbol(name, SymbolTableNode(node.kind, node.node,
1093-
self.cur_mod_id), i)
1093+
self.cur_mod_id,
1094+
node.type_override), i)
10941095
else:
10951096
# Don't add any dummy symbols for 'from x import *' if 'x' is unknown.
10961097
pass

test-data/unit/check-modules.test

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1345,3 +1345,79 @@ def part4_thing(a: int) -> str: pass
13451345
[builtins fixtures/bool.pyi]
13461346
[out]
13471347
tmp/part3.py:2: error: Revealed type is 'def (a: builtins.int) -> builtins.str'
1348+
1349+
[case testImportStarAliasAnyList]
1350+
import bar
1351+
1352+
[file bar.py]
1353+
from foo import *
1354+
def bar(y: AnyAlias) -> None: pass
1355+
1356+
l = None # type: ListAlias[int]
1357+
reveal_type(l)
1358+
1359+
[file foo.py]
1360+
from typing import Any, List
1361+
AnyAlias = Any
1362+
ListAlias = List
1363+
[builtins fixtures/list.pyi]
1364+
[out]
1365+
main:1: note: In module imported here:
1366+
tmp/bar.py:5: error: Revealed type is 'builtins.list[builtins.int]'
1367+
1368+
[case testImportStarAliasSimpleGeneric]
1369+
from ex2a import *
1370+
1371+
def do_something(dic: Row) -> None:
1372+
pass
1373+
1374+
def do_another() -> Row:
1375+
return {}
1376+
1377+
do_something({'good': 'bad'}) # E: List item 0 has incompatible type "Tuple[str, str]"
1378+
reveal_type(do_another()) # E: Revealed type is 'builtins.dict[builtins.str, builtins.int]'
1379+
1380+
[file ex2a.py]
1381+
from typing import Dict
1382+
Row = Dict[str, int]
1383+
[builtins fixtures/dict.pyi]
1384+
[out]
1385+
1386+
[case testImportStarAliasGeneric]
1387+
from y import *
1388+
notes = None # type: G[X]
1389+
another = G[X]()
1390+
second = XT[str]()
1391+
last = XT[G]()
1392+
1393+
reveal_type(notes) # E: Revealed type is 'y.G[y.G[builtins.int]]'
1394+
reveal_type(another) # E: Revealed type is 'y.G[y.G*[builtins.int]]'
1395+
reveal_type(second) # E: Revealed type is 'y.G[builtins.str*]'
1396+
reveal_type(last) # E: Revealed type is 'y.G[y.G*]'
1397+
1398+
[file y.py]
1399+
from typing import Generic, TypeVar
1400+
1401+
T = TypeVar('T')
1402+
1403+
class G(Generic[T]):
1404+
pass
1405+
1406+
X = G[int]
1407+
XT = G[T]
1408+
[out]
1409+
1410+
[case testImportStarAliasCallable]
1411+
from foo import *
1412+
from typing import Any
1413+
1414+
def bar(x: Any, y: AnyCallable) -> Any:
1415+
return 'foo'
1416+
1417+
cb = None # type: AnyCallable
1418+
reveal_type(cb) # E: Revealed type is 'def (*Any, **Any) -> Any'
1419+
1420+
[file foo.py]
1421+
from typing import Callable, Any
1422+
AnyCallable = Callable[..., Any]
1423+
[out]

0 commit comments

Comments
 (0)