diff --git a/mypy/semanal.py b/mypy/semanal.py index f58f533a268f..03110f708827 100644 --- a/mypy/semanal.py +++ b/mypy/semanal.py @@ -4670,7 +4670,7 @@ def add_imported_symbol(self, symbol = SymbolTableNode(node.kind, node.node, module_public=module_public, module_hidden=module_hidden) - self.add_symbol_table_node(name, symbol, context) + self.add_symbol_table_node(name, symbol, context, can_defer=not self.final_iteration) def add_unknown_imported_symbol(self, name: str, diff --git a/test-data/unit/check-modules.test b/test-data/unit/check-modules.test index 1ae31d99c25b..20c4a3b81f70 100644 --- a/test-data/unit/check-modules.test +++ b/test-data/unit/check-modules.test @@ -3131,4 +3131,36 @@ 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] \ No newline at end of file +[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 # E: Module "b" has no attribute "no_such_export" +[file b.py] +from a import * + +[case testCyclicUndefinedImportWithStar2] +import a +[file a.py] +from b import no_such_export # E: Module "b" has no attribute "no_such_export" +[file b.py] +from c import * +[file c.py] +from a import * + +[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"