Skip to content

Commit 8d5b641

Browse files
authored
Always mention explicit export when relevant (#13925)
Related to #13917 and #13908. This handles situations involving per-module re-exports correctly / is consistent with the error you'd get with attribute access.
1 parent ace0f32 commit 8d5b641

File tree

3 files changed

+17
-18
lines changed

3 files changed

+17
-18
lines changed

mypy/semanal.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -2346,10 +2346,9 @@ def report_missing_module_attribute(
23462346
# Suggest alternatives, if any match is found.
23472347
module = self.modules.get(import_id)
23482348
if module:
2349-
if not self.options.implicit_reexport and source_id in module.names.keys():
2349+
if source_id in module.names.keys() and not module.names[source_id].module_public:
23502350
message = (
2351-
'Module "{}" does not explicitly export attribute "{}"'
2352-
"; implicit reexport disabled".format(import_id, source_id)
2351+
f'Module "{import_id}" does not explicitly export attribute "{source_id}"'
23532352
)
23542353
else:
23552354
alternatives = set(module.names.keys()).difference({source_id})

test-data/unit/check-flags.test

+6-6
Original file line numberDiff line numberDiff line change
@@ -1613,7 +1613,7 @@ a = 5
16131613
[file other_module_2.py]
16141614
from other_module_1 import a
16151615
[out]
1616-
main:2: error: Module "other_module_2" does not explicitly export attribute "a"; implicit reexport disabled
1616+
main:2: error: Module "other_module_2" does not explicitly export attribute "a"
16171617

16181618
[case testNoImplicitReexportRespectsAll]
16191619
# flags: --no-implicit-reexport
@@ -1627,7 +1627,7 @@ from other_module_1 import a, b
16271627
__all__ = ('b',)
16281628
[builtins fixtures/tuple.pyi]
16291629
[out]
1630-
main:2: error: Module "other_module_2" does not explicitly export attribute "a"; implicit reexport disabled
1630+
main:2: error: Module "other_module_2" does not explicitly export attribute "a"
16311631

16321632
[case testNoImplicitReexportStarConsideredExplicit]
16331633
# flags: --no-implicit-reexport
@@ -1643,7 +1643,7 @@ __all__ = ('b',)
16431643

16441644
[case testNoImplicitReexportGetAttr]
16451645
# flags: --no-implicit-reexport --python-version 3.7
1646-
from other_module_2 import a # E: Module "other_module_2" does not explicitly export attribute "a"; implicit reexport disabled
1646+
from other_module_2 import a # E: Module "other_module_2" does not explicitly export attribute "a"
16471647
[file other_module_1.py]
16481648
from typing import Any
16491649
def __getattr__(name: str) -> Any: ...
@@ -1661,7 +1661,7 @@ attr_2 = 6
16611661
[file other_module_2.py]
16621662
from other_module_1 import attr_1, attr_2
16631663
[out]
1664-
main:2: error: Module "other_module_2" does not explicitly export attribute "attr_1"; implicit reexport disabled
1664+
main:2: error: Module "other_module_2" does not explicitly export attribute "attr_1"
16651665

16661666
[case testNoImplicitReexportMypyIni]
16671667
# flags: --config-file tmp/mypy.ini
@@ -1679,7 +1679,7 @@ implicit_reexport = True
16791679
\[mypy-other_module_2]
16801680
implicit_reexport = False
16811681
[out]
1682-
main:2: error: Module "other_module_2" has no attribute "a"
1682+
main:2: error: Module "other_module_2" does not explicitly export attribute "a"
16831683

16841684

16851685
[case testNoImplicitReexportPyProjectTOML]
@@ -1700,7 +1700,7 @@ module = 'other_module_2'
17001700
implicit_reexport = false
17011701

17021702
[out]
1703-
main:2: error: Module "other_module_2" has no attribute "a"
1703+
main:2: error: Module "other_module_2" does not explicitly export attribute "a"
17041704

17051705

17061706
[case testImplicitAnyOKForNoArgs]

test-data/unit/check-modules.test

+9-9
Original file line numberDiff line numberDiff line change
@@ -1787,8 +1787,8 @@ m = n # E: Cannot assign multiple modules to name "m" without explicit "types.M
17871787
[builtins fixtures/module.pyi]
17881788

17891789
[case testNoReExportFromStubs]
1790-
from stub import Iterable # E: Module "stub" has no attribute "Iterable"
1791-
from stub import D # E: Module "stub" has no attribute "D"
1790+
from stub import Iterable # E: Module "stub" does not explicitly export attribute "Iterable"
1791+
from stub import D # E: Module "stub" does not explicitly export attribute "D"
17921792
from stub import C
17931793

17941794
c = C()
@@ -1884,7 +1884,7 @@ class C:
18841884
from util import mod
18851885
reveal_type(mod) # N: Revealed type is "def () -> package.mod.mod"
18861886

1887-
from util import internal_detail # E: Module "util" has no attribute "internal_detail"
1887+
from util import internal_detail # E: Module "util" does not explicitly export attribute "internal_detail"
18881888

18891889
[file package/__init__.pyi]
18901890
from .mod import mod as mod
@@ -1899,7 +1899,7 @@ from package import mod as internal_detail
18991899
[builtins fixtures/module.pyi]
19001900

19011901
[case testNoReExportUnrelatedModule]
1902-
from mod2 import unrelated # E: Module "mod2" has no attribute "unrelated"
1902+
from mod2 import unrelated # E: Module "mod2" does not explicitly export attribute "unrelated"
19031903

19041904
[file mod1/__init__.pyi]
19051905
[file mod1/unrelated.pyi]
@@ -1910,7 +1910,7 @@ from mod1 import unrelated
19101910
[builtins fixtures/module.pyi]
19111911

19121912
[case testNoReExportUnrelatedSiblingPrefix]
1913-
from pkg.unrel import unrelated # E: Module "pkg.unrel" has no attribute "unrelated"
1913+
from pkg.unrel import unrelated # E: Module "pkg.unrel" does not explicitly export attribute "unrelated"
19141914

19151915
[file pkg/__init__.pyi]
19161916
[file pkg/unrelated.pyi]
@@ -1922,7 +1922,7 @@ from pkg import unrelated
19221922

19231923
[case testNoReExportChildStubs]
19241924
import mod
1925-
from mod import C, D # E: Module "mod" has no attribute "C"
1925+
from mod import C, D # E: Module "mod" does not explicitly export attribute "C"
19261926

19271927
reveal_type(mod.x) # N: Revealed type is "mod.submod.C"
19281928
mod.C # E: "Module mod" does not explicitly export attribute "C"
@@ -1940,7 +1940,7 @@ class D:
19401940
[builtins fixtures/module.pyi]
19411941

19421942
[case testNoReExportNestedStub]
1943-
from stub import substub # E: Module "stub" has no attribute "substub"
1943+
from stub import substub # E: Module "stub" does not explicitly export attribute "substub"
19441944

19451945
[file stub.pyi]
19461946
import substub
@@ -2887,10 +2887,10 @@ CustomDict = TypedDict(
28872887
[builtins fixtures/tuple.pyi]
28882888

28892889
[case testNoReExportFromMissingStubs]
2890-
from stub import a # E: Module "stub" has no attribute "a"
2890+
from stub import a # E: Module "stub" does not explicitly export attribute "a"
28912891
from stub import b
28922892
from stub import c # E: Module "stub" has no attribute "c"
2893-
from stub import d # E: Module "stub" has no attribute "d"
2893+
from stub import d # E: Module "stub" does not explicitly export attribute "d"
28942894

28952895
[file stub.pyi]
28962896
from mystery import a, b as b, c as d

0 commit comments

Comments
 (0)