Skip to content

Commit

Permalink
semanal: populate module_public even for missing modules (#8661)
Browse files Browse the repository at this point in the history
Fixes #8649

Co-authored-by: hauntsaninja <>
  • Loading branch information
hauntsaninja authored Sep 6, 2020
1 parent 720b77e commit b4c5b80
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
18 changes: 14 additions & 4 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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."""
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions mypy/test/teststubtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down

0 comments on commit b4c5b80

Please sign in to comment.