From 21f56d51cf51bc31f645d8ed2c3be5bfc087ff53 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Sun, 17 Oct 2021 17:33:25 +0900 Subject: [PATCH 1/5] fix(11064): fix crash due to undefined cyclic `import *` --- mypy/semanal.py | 2 +- test-data/unit/check-modules.test | 34 ++++++++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/mypy/semanal.py b/mypy/semanal.py index 21741f90a528e..85ef51ac19378 100644 --- a/mypy/semanal.py +++ b/mypy/semanal.py @@ -4707,7 +4707,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 1ae31d99c25b6..20c4a3b81f70f 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" From 9ec76a17cdd244fee906f2ac2af1e5b9e0470b5c Mon Sep 17 00:00:00 2001 From: hauntsaninja <> Date: Wed, 8 Dec 2021 00:19:08 -0800 Subject: [PATCH 2/5] revert --- mypy/semanal.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mypy/semanal.py b/mypy/semanal.py index 85ef51ac19378..21741f90a528e 100644 --- a/mypy/semanal.py +++ b/mypy/semanal.py @@ -4707,7 +4707,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, can_defer=not self.final_iteration) + self.add_symbol_table_node(name, symbol, context) def add_unknown_imported_symbol(self, name: str, From dd7c0a33621026485c9bd0fedb772ab5cb52d0e0 Mon Sep 17 00:00:00 2001 From: hauntsaninja <> Date: Wed, 8 Dec 2021 00:23:45 -0800 Subject: [PATCH 3/5] fix tests --- test-data/unit/check-modules.test | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/test-data/unit/check-modules.test b/test-data/unit/check-modules.test index 20c4a3b81f70f..fc915185b23a2 100644 --- a/test-data/unit/check-modules.test +++ b/test-data/unit/check-modules.test @@ -3143,18 +3143,25 @@ 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" +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 # E: Module "b" has no attribute "no_such_export" +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 From 7174772e97810232f3e69d3c45fc8ecbc2936898 Mon Sep 17 00:00:00 2001 From: hauntsaninja <> Date: Wed, 8 Dec 2021 00:23:51 -0800 Subject: [PATCH 4/5] fix deferral --- mypy/semanal.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/mypy/semanal.py b/mypy/semanal.py index 21741f90a528e..f18bbd95ce668 100644 --- a/mypy/semanal.py +++ b/mypy/semanal.py @@ -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(context) if (existing is not None and context is not None and not is_valid_replacement(existing, symbol)): From ee586ba36705c995d3572b631f70ae5078f6288c Mon Sep 17 00:00:00 2001 From: hauntsaninja <> Date: Wed, 8 Dec 2021 00:37:40 -0800 Subject: [PATCH 5/5] please mypyc --- mypy/semanal.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mypy/semanal.py b/mypy/semanal.py index f18bbd95ce668..275a42bb6c154 100644 --- a/mypy/semanal.py +++ b/mypy/semanal.py @@ -4616,7 +4616,7 @@ def add_symbol_table_node(self, self.process_placeholder(name, 'name', context) else: # see note in docstring describing None contexts - self.defer(context) + self.defer() if (existing is not None and context is not None and not is_valid_replacement(existing, symbol)):