Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash involving undefined cyclic import * #11681

Merged
merged 5 commits into from Dec 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion mypy/semanal.py
Expand Up @@ -4612,7 +4612,11 @@ def add_symbol_table_node(self,
names = self.current_symbol_table(escape_comprehensions=escape_comprehensions)
existing = names.get(name)
if isinstance(symbol.node, PlaceholderNode) and can_defer:
self.defer(context)
if context is not None:
self.process_placeholder(name, 'name', context)
else:
# see note in docstring describing None contexts
self.defer()
if (existing is not None
and context is not None
and not is_valid_replacement(existing, symbol)):
Expand Down
41 changes: 40 additions & 1 deletion test-data/unit/check-modules.test
Expand Up @@ -3131,4 +3131,43 @@ main:2: error: Cannot find implementation or library stub for module named "blea
# flags: --ignore-missing-imports
import bleach.xyz
from bleach.abc import fgh
[file bleach/__init__.pyi]
[file bleach/__init__.pyi]

[case testCyclicUndefinedImportWithName]
import a
[file a.py]
from b import no_such_export
[file b.py]
from a import no_such_export # E: Module "a" has no attribute "no_such_export"

[case testCyclicUndefinedImportWithStar1]
import a
[file a.py]
from b import no_such_export
[file b.py]
from a import *
[out]
tmp/b.py:1: error: Cannot resolve name "no_such_export" (possible cyclic definition)
tmp/a.py:1: error: Module "b" has no attribute "no_such_export"

[case testCyclicUndefinedImportWithStar2]
import a
[file a.py]
from b import no_such_export
[file b.py]
from c import *
[file c.py]
from a import *
[out]
tmp/c.py:1: error: Cannot resolve name "no_such_export" (possible cyclic definition)
tmp/b.py:1: error: Cannot resolve name "no_such_export" (possible cyclic definition)
tmp/a.py:1: error: Module "b" has no attribute "no_such_export"

[case testCyclicUndefinedImportWithStar3]
import test1
[file test1.py]
from dir1 import *
[file dir1/__init__.py]
from .test2 import *
[file dir1/test2.py]
from test1 import aaaa # E: Module "test1" has no attribute "aaaa"