Skip to content

Commit 4e2f4ff

Browse files
authored
semanal: fix module_public in the presence of getattr (#11411)
1 parent 0483e15 commit 4e2f4ff

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

mypy/semanal.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1808,6 +1808,12 @@ def visit_import_from(self, imp: ImportFrom) -> None:
18081808
missing_submodule = False
18091809
imported_id = as_id or id
18101810

1811+
# Modules imported in a stub file without using 'from Y import X as X' will
1812+
# not get exported.
1813+
# When implicit re-exporting is disabled, we have the same behavior as stubs.
1814+
use_implicit_reexport = not self.is_stub_file and self.options.implicit_reexport
1815+
module_public = use_implicit_reexport or (as_id is not None and id == as_id)
1816+
18111817
# If the module does not contain a symbol with the name 'id',
18121818
# try checking if it's a module instead.
18131819
if not node:
@@ -1825,15 +1831,12 @@ def visit_import_from(self, imp: ImportFrom) -> None:
18251831
fullname = module_id + '.' + id
18261832
gvar = self.create_getattr_var(module.names['__getattr__'], imported_id, fullname)
18271833
if gvar:
1828-
self.add_symbol(imported_id, gvar, imp)
1834+
self.add_symbol(
1835+
imported_id, gvar, imp, module_public=module_public,
1836+
module_hidden=not module_public
1837+
)
18291838
continue
18301839

1831-
# Modules imported in a stub file without using 'from Y import X as X' will
1832-
# not get exported.
1833-
# When implicit re-exporting is disabled, we have the same behavior as stubs.
1834-
use_implicit_reexport = not self.is_stub_file and self.options.implicit_reexport
1835-
module_public = use_implicit_reexport or (as_id is not None and id == as_id)
1836-
18371840
if node and not node.module_hidden:
18381841
self.process_imported_symbol(
18391842
node, module_id, id, imported_id, fullname, module_public, context=imp

test-data/unit/check-flags.test

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1525,6 +1525,16 @@ __all__ = ('b',)
15251525
[out]
15261526
main:2: error: Module "other_module_2" does not explicitly export attribute "a"; implicit reexport disabled
15271527

1528+
[case testNoImplicitReexportGetAttr]
1529+
# flags: --no-implicit-reexport --python-version 3.7
1530+
from other_module_2 import a # E: Module "other_module_2" does not explicitly export attribute "a"; implicit reexport disabled
1531+
[file other_module_1.py]
1532+
from typing import Any
1533+
def __getattr__(name: str) -> Any: ...
1534+
[file other_module_2.py]
1535+
from other_module_1 import a
1536+
[builtins fixtures/tuple.pyi]
1537+
15281538
[case textNoImplicitReexportSuggestions]
15291539
# flags: --no-implicit-reexport
15301540
from other_module_2 import attr_1

test-data/unit/check-newsemanal.test

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2391,7 +2391,8 @@ import p
23912391
import p
23922392
reveal_type(p.y)
23932393
[file p.pyi]
2394-
from pp import x as y
2394+
from pp import x
2395+
y = x
23952396
[file pp.pyi]
23962397
def __getattr__(attr): ...
23972398
[out2]

0 commit comments

Comments
 (0)