Skip to content

Commit b640914

Browse files
Fix crash involving undefined cyclic import * (#11681)
Fixes #11064. Co-authored by @hi-ogawa Pushes #11346 over the finish line. Implements the suggestion in #11346 (review) Co-authored-by: Hiroshi Ogawa <[email protected]> Co-authored-by: hauntsaninja <>
1 parent 1970029 commit b640914

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

mypy/semanal.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4611,7 +4611,11 @@ def add_symbol_table_node(self,
46114611
names = self.current_symbol_table(escape_comprehensions=escape_comprehensions)
46124612
existing = names.get(name)
46134613
if isinstance(symbol.node, PlaceholderNode) and can_defer:
4614-
self.defer(context)
4614+
if context is not None:
4615+
self.process_placeholder(name, 'name', context)
4616+
else:
4617+
# see note in docstring describing None contexts
4618+
self.defer()
46154619
if (existing is not None
46164620
and context is not None
46174621
and not is_valid_replacement(existing, symbol)):

test-data/unit/check-modules.test

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3131,4 +3131,43 @@ main:2: error: Cannot find implementation or library stub for module named "blea
31313131
# flags: --ignore-missing-imports
31323132
import bleach.xyz
31333133
from bleach.abc import fgh
3134-
[file bleach/__init__.pyi]
3134+
[file bleach/__init__.pyi]
3135+
3136+
[case testCyclicUndefinedImportWithName]
3137+
import a
3138+
[file a.py]
3139+
from b import no_such_export
3140+
[file b.py]
3141+
from a import no_such_export # E: Module "a" has no attribute "no_such_export"
3142+
3143+
[case testCyclicUndefinedImportWithStar1]
3144+
import a
3145+
[file a.py]
3146+
from b import no_such_export
3147+
[file b.py]
3148+
from a import *
3149+
[out]
3150+
tmp/b.py:1: error: Cannot resolve name "no_such_export" (possible cyclic definition)
3151+
tmp/a.py:1: error: Module "b" has no attribute "no_such_export"
3152+
3153+
[case testCyclicUndefinedImportWithStar2]
3154+
import a
3155+
[file a.py]
3156+
from b import no_such_export
3157+
[file b.py]
3158+
from c import *
3159+
[file c.py]
3160+
from a import *
3161+
[out]
3162+
tmp/c.py:1: error: Cannot resolve name "no_such_export" (possible cyclic definition)
3163+
tmp/b.py:1: error: Cannot resolve name "no_such_export" (possible cyclic definition)
3164+
tmp/a.py:1: error: Module "b" has no attribute "no_such_export"
3165+
3166+
[case testCyclicUndefinedImportWithStar3]
3167+
import test1
3168+
[file test1.py]
3169+
from dir1 import *
3170+
[file dir1/__init__.py]
3171+
from .test2 import *
3172+
[file dir1/test2.py]
3173+
from test1 import aaaa # E: Module "test1" has no attribute "aaaa"

0 commit comments

Comments
 (0)