Skip to content

Commit

Permalink
Do not consider import a.b as b an explicit reexport (#14086)
Browse files Browse the repository at this point in the history
The point of the `import a as a` and `from a import b as b` syntax for
explicit reexport is that it indicates an intention to do something
different from the ordinary `import a` and `from a import b`.

That is not the case with `import a.b as b`. Even mypy’s own code
includes `import mypy.types as types`, which was not intended to be a
reexport; if it were, it would be written `from mypy import types as
types`.

Pyright agrees that `import a.b as b` should not reexport.

Signed-off-by: Anders Kaseorg <andersk@mit.edu>
  • Loading branch information
andersk authored Sep 20, 2023
1 parent c9929e2 commit 7089a7f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -2466,7 +2466,7 @@ def visit_import(self, i: Import) -> None:
if as_id is not None:
base_id = id
imported_id = as_id
module_public = use_implicit_reexport or id.split(".")[-1] == as_id
module_public = use_implicit_reexport or id == as_id
else:
base_id = id.split(".")[0]
imported_id = base_id
Expand Down
8 changes: 8 additions & 0 deletions test-data/unit/check-modules.test
Original file line number Diff line number Diff line change
Expand Up @@ -1818,6 +1818,8 @@ m = n # E: Cannot assign multiple modules to name "m" without explicit "types.M
from stub import Iterable # E: Module "stub" does not explicitly export attribute "Iterable"
from stub import D # E: Module "stub" does not explicitly export attribute "D"
from stub import C
from stub import foo
from stub import bar # E: Module "stub" does not explicitly export attribute "bar"

c = C()
reveal_type(c.x) # N: Revealed type is "builtins.int"
Expand All @@ -1828,13 +1830,19 @@ reveal_type(it) # N: Revealed type is "typing.Iterable[builtins.int]"
from typing import Iterable
from substub import C as C
from substub import C as D
from package import foo as foo
import package.bar as bar

def fun(x: Iterable[str]) -> Iterable[int]: pass

[file substub.pyi]
class C:
x: int

[file package/foo.pyi]

[file package/bar.pyi]

[builtins fixtures/module.pyi]

[case testNoReExportFromStubsMemberType]
Expand Down

0 comments on commit 7089a7f

Please sign in to comment.