Skip to content

Commit 7089a7f

Browse files
authored
Do not consider import a.b as b an explicit reexport (#14086)
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>
1 parent c9929e2 commit 7089a7f

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
@@ -2466,7 +2466,7 @@ def visit_import(self, i: Import) -> None:
24662466
if as_id is not None:
24672467
base_id = id
24682468
imported_id = as_id
2469-
module_public = use_implicit_reexport or id.split(".")[-1] == as_id
2469+
module_public = use_implicit_reexport or id == as_id
24702470
else:
24712471
base_id = id.split(".")[0]
24722472
imported_id = base_id

test-data/unit/check-modules.test

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1818,6 +1818,8 @@ m = n # E: Cannot assign multiple modules to name "m" without explicit "types.M
18181818
from stub import Iterable # E: Module "stub" does not explicitly export attribute "Iterable"
18191819
from stub import D # E: Module "stub" does not explicitly export attribute "D"
18201820
from stub import C
1821+
from stub import foo
1822+
from stub import bar # E: Module "stub" does not explicitly export attribute "bar"
18211823

18221824
c = C()
18231825
reveal_type(c.x) # N: Revealed type is "builtins.int"
@@ -1828,13 +1830,19 @@ reveal_type(it) # N: Revealed type is "typing.Iterable[builtins.int]"
18281830
from typing import Iterable
18291831
from substub import C as C
18301832
from substub import C as D
1833+
from package import foo as foo
1834+
import package.bar as bar
18311835

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

18341838
[file substub.pyi]
18351839
class C:
18361840
x: int
18371841

1842+
[file package/foo.pyi]
1843+
1844+
[file package/bar.pyi]
1845+
18381846
[builtins fixtures/module.pyi]
18391847

18401848
[case testNoReExportFromStubsMemberType]

0 commit comments

Comments
 (0)