Skip to content

Commit c444466

Browse files
committed
Do not consider ‘import a.b as b’ an explicit reexport
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 <[email protected]>
1 parent 47a435f commit c444466

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

mypy/semanal.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2230,7 +2230,7 @@ def visit_import(self, i: Import) -> None:
22302230
if as_id is not None:
22312231
base_id = id
22322232
imported_id = as_id
2233-
module_public = use_implicit_reexport or id.split(".")[-1] == as_id
2233+
module_public = use_implicit_reexport or id == as_id
22342234
else:
22352235
base_id = id.split(".")[0]
22362236
imported_id = base_id

test-data/unit/check-modules.test

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1809,6 +1809,8 @@ m = n # E: Cannot assign multiple modules to name "m" without explicit "types.M
18091809
from stub import Iterable # E: Module "stub" does not explicitly export attribute "Iterable"
18101810
from stub import D # E: Module "stub" does not explicitly export attribute "D"
18111811
from stub import C
1812+
from stub import foo
1813+
from stub import bar # E: Module "stub" does not explicitly export attribute "bar"
18121814

18131815
c = C()
18141816
reveal_type(c.x) # N: Revealed type is "builtins.int"
@@ -1819,13 +1821,19 @@ reveal_type(it) # N: Revealed type is "typing.Iterable[builtins.int]"
18191821
from typing import Iterable
18201822
from substub import C as C
18211823
from substub import C as D
1824+
from package import foo as foo
1825+
import package.bar as bar
18221826

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

18251829
[file substub.pyi]
18261830
class C:
18271831
x: int
18281832

1833+
[file package/foo.pyi]
1834+
1835+
[file package/bar.pyi]
1836+
18291837
[builtins fixtures/module.pyi]
18301838

18311839
[case testNoReExportFromStubsMemberType]

0 commit comments

Comments
 (0)