Skip to content

Commit e7f4619

Browse files
authored
Fix crash on circular star import in incremental mode (#20511)
This allows to re-enable two more tests in parallel mode. The fix is kind of simple, replace any remaining placeholders with a `Var` on final iteration (similar to what we do for regular `from ... import x`). This has one unfortunate consequence: now each error is only reported once (which is good), _but_ it will be always reported in a module with a star import (because of how SCCs are ordered). I don't see any simple way to fix this, but IMO an error in a somewhat confusing place is better than a crash.
1 parent 30260ca commit e7f4619

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

mypy/semanal.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3224,6 +3224,12 @@ def visit_import_all(self, i: ImportAll) -> None:
32243224
self.add_imported_symbol(
32253225
name, node, context=i, module_public=True, module_hidden=False
32263226
)
3227+
# This is a (minimalist) copy of the logic in visit_import_from(), we need
3228+
# to clean-up any remaining placeholders by replacing them with Var(Any).
3229+
if isinstance(node.node, PlaceholderNode) and self.final_iteration:
3230+
self.add_unknown_imported_symbol(
3231+
name, i, target_name=None, module_public=True, module_hidden=False
3232+
)
32273233

32283234
else:
32293235
# Don't add any dummy symbols for 'from x import *' if 'x' is unknown.

test-data/unit/check-incremental.test

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7683,3 +7683,20 @@ main:4: error: Too many arguments for "C"
76837683
[out2]
76847684
main:2: error: Module "m1" has no attribute "D"
76857685
main:4: error: Too many arguments for "C"
7686+
7687+
[case testCyclicUndefinedImportWithStarIncremental]
7688+
import m
7689+
[file m.py]
7690+
import a
7691+
[file m.py.2]
7692+
import a
7693+
reveal_type(a.no_such_export)
7694+
[file a.py]
7695+
from b import no_such_export
7696+
[file b.py]
7697+
from a import *
7698+
[out]
7699+
tmp/b.py:1: error: Cannot resolve name "no_such_export" (possible cyclic definition)
7700+
[out2]
7701+
tmp/b.py:1: error: Cannot resolve name "no_such_export" (possible cyclic definition)
7702+
tmp/m.py:2: note: Revealed type is "Any"

test-data/unit/check-modules.test

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3184,17 +3184,16 @@ from b import no_such_export
31843184
[file b.py]
31853185
from a import no_such_export # E: Module "a" has no attribute "no_such_export"
31863186

3187-
[case testCyclicUndefinedImportWithStar1_no_parallel]
3187+
[case testCyclicUndefinedImportWithStar1]
31883188
import a
31893189
[file a.py]
31903190
from b import no_such_export
31913191
[file b.py]
31923192
from a import *
31933193
[out]
31943194
tmp/b.py:1: error: Cannot resolve name "no_such_export" (possible cyclic definition)
3195-
tmp/a.py:1: error: Module "b" has no attribute "no_such_export"
31963195

3197-
[case testCyclicUndefinedImportWithStar2_no_parallel]
3196+
[case testCyclicUndefinedImportWithStar2]
31983197
import a
31993198
[file a.py]
32003199
from b import no_such_export
@@ -3204,8 +3203,6 @@ from c import *
32043203
from a import *
32053204
[out]
32063205
tmp/c.py:1: error: Cannot resolve name "no_such_export" (possible cyclic definition)
3207-
tmp/b.py:1: error: Cannot resolve name "no_such_export" (possible cyclic definition)
3208-
tmp/a.py:1: error: Module "b" has no attribute "no_such_export"
32093206

32103207
[case testCyclicUndefinedImportWithStar3]
32113208
import test1

0 commit comments

Comments
 (0)