Skip to content

Commit b4c5b80

Browse files
authored
semanal: populate module_public even for missing modules (#8661)
Fixes #8649 Co-authored-by: hauntsaninja <>
1 parent 720b77e commit b4c5b80

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

mypy/semanal.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1762,8 +1762,15 @@ def visit_import_from(self, imp: ImportFrom) -> None:
17621762
# Target module exists but the imported name is missing or hidden.
17631763
self.report_missing_module_attribute(module_id, id, imported_id, imp)
17641764
else:
1765+
module_public = (
1766+
not self.is_stub_file
1767+
and self.options.implicit_reexport
1768+
or as_id is not None
1769+
)
17651770
# Import of a missing (sub)module.
1766-
self.add_unknown_imported_symbol(imported_id, imp, target_name=fullname)
1771+
self.add_unknown_imported_symbol(
1772+
imported_id, imp, target_name=fullname, module_public=module_public
1773+
)
17671774

17681775
def process_imported_symbol(self,
17691776
node: SymbolTableNode,
@@ -4415,7 +4422,9 @@ def add_module_symbol(self,
44154422
module_public=module_public,
44164423
module_hidden=module_hidden)
44174424
else:
4418-
self.add_unknown_imported_symbol(as_id, context, target_name=id)
4425+
self.add_unknown_imported_symbol(
4426+
as_id, context, target_name=id, module_public=module_public
4427+
)
44194428

44204429
def add_local(self, node: Union[Var, FuncDef, OverloadedFuncDef], context: Context) -> None:
44214430
"""Add local variable or function."""
@@ -4440,7 +4449,8 @@ def add_imported_symbol(self,
44404449
def add_unknown_imported_symbol(self,
44414450
name: str,
44424451
context: Context,
4443-
target_name: Optional[str] = None) -> None:
4452+
target_name: Optional[str] = None,
4453+
module_public: bool = True) -> None:
44444454
"""Add symbol that we don't know what it points to because resolving an import failed.
44454455
44464456
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,
44684478
any_type = AnyType(TypeOfAny.from_unimported_type, missing_import_name=var._fullname)
44694479
var.type = any_type
44704480
var.is_suppressed_import = True
4471-
self.add_symbol(name, var, context)
4481+
self.add_symbol(name, var, context, module_public=module_public)
44724482

44734483
#
44744484
# Other helpers

mypy/test/teststubtest.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,8 @@ def h(x: str): ...
577577
yield Case("", "__all__ = []", None) # dummy case
578578
yield Case(stub="", runtime="__all__ += ['y']\ny = 5", error="y")
579579
yield Case(stub="", runtime="__all__ += ['g']\ndef g(): pass", error="g")
580+
# Here we should only check that runtime has B, since the stub explicitly re-exports it
581+
yield Case(stub="from mystery import A, B as B # type: ignore", runtime="", error="B")
580582

581583
@collect_cases
582584
def test_name_mangling(self) -> Iterator[Case]:

0 commit comments

Comments
 (0)