Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1795,6 +1795,12 @@ def visit_import_from(self, imp: ImportFrom) -> None:
missing_submodule = False
imported_id = as_id or id

# Modules imported in a stub file without using 'from Y import X as X' will

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to create a test with similar problem as in python/typeshed#6171 ? 🤔

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. This PR looks good, but a test case would be a nice bonus.

# not get exported.
# When implicit re-exporting is disabled, we have the same behavior as stubs.
use_implicit_reexport = not self.is_stub_file and self.options.implicit_reexport
module_public = use_implicit_reexport or (as_id is not None and id == as_id)

# If the module does not contain a symbol with the name 'id',
# try checking if it's a module instead.
if not node:
Expand All @@ -1812,15 +1818,12 @@ def visit_import_from(self, imp: ImportFrom) -> None:
fullname = module_id + '.' + id
gvar = self.create_getattr_var(module.names['__getattr__'], imported_id, fullname)
if gvar:
self.add_symbol(imported_id, gvar, imp)
self.add_symbol(
imported_id, gvar, imp, module_public=module_public,
module_hidden=not module_public
)
continue

# Modules imported in a stub file without using 'from Y import X as X' will
# not get exported.
# When implicit re-exporting is disabled, we have the same behavior as stubs.
use_implicit_reexport = not self.is_stub_file and self.options.implicit_reexport
module_public = use_implicit_reexport or (as_id is not None and id == as_id)

if node and not node.module_hidden:
self.process_imported_symbol(
node, module_id, id, imported_id, fullname, module_public, context=imp
Expand Down
10 changes: 10 additions & 0 deletions test-data/unit/check-flags.test
Original file line number Diff line number Diff line change
Expand Up @@ -1525,6 +1525,16 @@ __all__ = ('b',)
[out]
main:2: error: Module "other_module_2" does not explicitly export attribute "a"; implicit reexport disabled

[case testNoImplicitReexportGetAttr]
# flags: --no-implicit-reexport --python-version 3.7
from other_module_2 import a # E: Module "other_module_2" does not explicitly export attribute "a"; implicit reexport disabled
[file other_module_1.py]
from typing import Any
def __getattr__(name: str) -> Any: ...
[file other_module_2.py]
from other_module_1 import a
[builtins fixtures/tuple.pyi]

[case textNoImplicitReexportSuggestions]
# flags: --no-implicit-reexport
from other_module_2 import attr_1
Expand Down
3 changes: 2 additions & 1 deletion test-data/unit/check-newsemanal.test
Original file line number Diff line number Diff line change
Expand Up @@ -2390,7 +2390,8 @@ import p
import p
reveal_type(p.y)
[file p.pyi]
from pp import x as y
from pp import x
y = x
[file pp.pyi]
def __getattr__(attr): ...
[out2]
Expand Down