Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve (some) implicitly exported types #13967

Merged
merged 1 commit into from
Oct 31, 2022
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
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