From c1928d9eb4127e39bf0dfc2b4bb1020feb2ad3bc Mon Sep 17 00:00:00 2001 From: Jukka Lehtosalo Date: Wed, 28 Apr 2021 16:41:34 +0100 Subject: [PATCH] Fix re-exporting __all__ in a stub file Propagate the value of `__all__` from the imported module. Fixes #10381. --- mypy/semanal.py | 2 ++ test-data/unit/check-modules.test | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/mypy/semanal.py b/mypy/semanal.py index 632f29e78777..0927e465b07c 100644 --- a/mypy/semanal.py +++ b/mypy/semanal.py @@ -1758,6 +1758,8 @@ def visit_import_from(self, imp: ImportFrom) -> None: # precedence, but doesn't seem to be important in most use cases. node = SymbolTableNode(GDEF, self.modules[fullname]) else: + if id == as_id == '__all__' and module_id in self.export_map: + self.all_exports[:] = self.export_map[module_id] node = module.names.get(id) missing_submodule = False diff --git a/test-data/unit/check-modules.test b/test-data/unit/check-modules.test index c2eeb90e884b..077f381561bb 100644 --- a/test-data/unit/check-modules.test +++ b/test-data/unit/check-modules.test @@ -2825,3 +2825,21 @@ from mystery import a, b as b, c as d [out] tmp/stub.pyi:1: error: Cannot find implementation or library stub for module named "mystery" tmp/stub.pyi:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports + +[case testReExportAllInStub] +from m1 import C +from m1 import D # E: Module "m1" has no attribute "D" +C() +C(1) # E: Too many arguments for "C" +[file m1.pyi] +from m2 import * +[file m2.pyi] +from m3 import * +from m3 import __all__ as __all__ +class D: pass +[file m3.pyi] +from m4 import C as C +__all__ = ['C'] +[file m4.pyi] +class C: pass +[builtins fixtures/list.pyi]