Skip to content

Commit

Permalink
Preserve (some) implicitly exported types (#13967)
Browse files Browse the repository at this point in the history
Fixes some of #13965, fixes #12749

We also need to modify attribute access logic (this is the TODO in the
PR), which is a little trickier. But cases like #13933 convinced me it's
worth making this change, even before I get around to figuring that out.
  • Loading branch information
hauntsaninja authored Oct 31, 2022
1 parent 7569d88 commit 8c69124
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
28 changes: 20 additions & 8 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -2303,10 +2303,20 @@ def visit_import_from(self, imp: ImportFrom) -> None:
)
continue

if node and not node.module_hidden:
if node:
self.process_imported_symbol(
node, module_id, id, imported_id, fullname, module_public, context=imp
)
if node.module_hidden:
self.report_missing_module_attribute(
module_id,
id,
imported_id,
module_public=module_public,
module_hidden=not module_public,
context=imp,
add_unknown_imported_symbol=False,
)
elif module and not missing_submodule:
# Target module exists but the imported name is missing or hidden.
self.report_missing_module_attribute(
Expand Down Expand Up @@ -2394,6 +2404,7 @@ def report_missing_module_attribute(
module_public: bool,
module_hidden: bool,
context: Node,
add_unknown_imported_symbol: bool = True,
) -> None:
# Missing attribute.
if self.is_incomplete_namespace(import_id):
Expand All @@ -2418,13 +2429,14 @@ def report_missing_module_attribute(
suggestion = f"; maybe {pretty_seq(matches, 'or')}?"
message += f"{suggestion}"
self.fail(message, context, code=codes.ATTR_DEFINED)
self.add_unknown_imported_symbol(
imported_id,
context,
target_name=None,
module_public=module_public,
module_hidden=not module_public,
)
if add_unknown_imported_symbol:
self.add_unknown_imported_symbol(
imported_id,
context,
target_name=None,
module_public=module_public,
module_hidden=not module_public,
)

if import_id == "typing":
# The user probably has a missing definition in a test fixture. Let's verify.
Expand Down
13 changes: 9 additions & 4 deletions test-data/unit/check-flags.test
Original file line number Diff line number Diff line change
Expand Up @@ -1606,14 +1606,19 @@ strict_equality = false


[case testNoImplicitReexport]
# flags: --no-implicit-reexport
from other_module_2 import a
# flags: --no-implicit-reexport --show-error-codes
from other_module_2 import a # E: Module "other_module_2" does not explicitly export attribute "a" [attr-defined]
reveal_type(a) # N: Revealed type is "builtins.int"

import other_module_2
# TODO: this should also reveal builtins.int, see #13965
reveal_type(other_module_2.a) # E: "object" does not explicitly export attribute "a" [attr-defined] \
# N: Revealed type is "Any"

[file other_module_1.py]
a = 5
[file other_module_2.py]
from other_module_1 import a
[out]
main:2: error: Module "other_module_2" does not explicitly export attribute "a"

[case testNoImplicitReexportRespectsAll]
# flags: --no-implicit-reexport
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-modules.test
Original file line number Diff line number Diff line change
Expand Up @@ -1794,7 +1794,7 @@ from stub import C
c = C()
reveal_type(c.x) # N: Revealed type is "builtins.int"
it: Iterable[int]
reveal_type(it) # N: Revealed type is "Any"
reveal_type(it) # N: Revealed type is "typing.Iterable[builtins.int]"

[file stub.pyi]
from typing import Iterable
Expand Down

0 comments on commit 8c69124

Please sign in to comment.