From b4c5b804025680e26ab9e1c50990a3f24067a036 Mon Sep 17 00:00:00 2001 From: Shantanu <12621235+hauntsaninja@users.noreply.github.com> Date: Sun, 6 Sep 2020 15:45:08 -0700 Subject: [PATCH] semanal: populate module_public even for missing modules (#8661) Fixes #8649 Co-authored-by: hauntsaninja <> --- mypy/semanal.py | 18 ++++++++++++++---- mypy/test/teststubtest.py | 2 ++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/mypy/semanal.py b/mypy/semanal.py index 005a0670dad3..ddd847d1ca52 100644 --- a/mypy/semanal.py +++ b/mypy/semanal.py @@ -1762,8 +1762,15 @@ def visit_import_from(self, imp: ImportFrom) -> None: # Target module exists but the imported name is missing or hidden. self.report_missing_module_attribute(module_id, id, imported_id, imp) else: + module_public = ( + not self.is_stub_file + and self.options.implicit_reexport + or as_id is not None + ) # Import of a missing (sub)module. - self.add_unknown_imported_symbol(imported_id, imp, target_name=fullname) + self.add_unknown_imported_symbol( + imported_id, imp, target_name=fullname, module_public=module_public + ) def process_imported_symbol(self, node: SymbolTableNode, @@ -4415,7 +4422,9 @@ def add_module_symbol(self, module_public=module_public, module_hidden=module_hidden) else: - self.add_unknown_imported_symbol(as_id, context, target_name=id) + self.add_unknown_imported_symbol( + as_id, context, target_name=id, module_public=module_public + ) def add_local(self, node: Union[Var, FuncDef, OverloadedFuncDef], context: Context) -> None: """Add local variable or function.""" @@ -4440,7 +4449,8 @@ def add_imported_symbol(self, def add_unknown_imported_symbol(self, name: str, context: Context, - target_name: Optional[str] = None) -> None: + target_name: Optional[str] = None, + module_public: bool = True) -> None: """Add symbol that we don't know what it points to because resolving an import failed. This can happen if a module is missing, or it is present, but doesn't have @@ -4468,7 +4478,7 @@ def add_unknown_imported_symbol(self, any_type = AnyType(TypeOfAny.from_unimported_type, missing_import_name=var._fullname) var.type = any_type var.is_suppressed_import = True - self.add_symbol(name, var, context) + self.add_symbol(name, var, context, module_public=module_public) # # Other helpers diff --git a/mypy/test/teststubtest.py b/mypy/test/teststubtest.py index 50f417e920c8..aeb31331e70a 100644 --- a/mypy/test/teststubtest.py +++ b/mypy/test/teststubtest.py @@ -577,6 +577,8 @@ def h(x: str): ... yield Case("", "__all__ = []", None) # dummy case yield Case(stub="", runtime="__all__ += ['y']\ny = 5", error="y") yield Case(stub="", runtime="__all__ += ['g']\ndef g(): pass", error="g") + # Here we should only check that runtime has B, since the stub explicitly re-exports it + yield Case(stub="from mystery import A, B as B # type: ignore", runtime="", error="B") @collect_cases def test_name_mangling(self) -> Iterator[Case]: