From cfd16de960ed9f9d6321dee7addc116b86cd7655 Mon Sep 17 00:00:00 2001 From: Kamil Turek Date: Fri, 31 Jul 2020 20:21:35 +0200 Subject: [PATCH 1/3] Show information about no explicit export instead of suggestions --- mypy/semanal.py | 13 ++++++++----- test-data/unit/check-flags.test | 12 ++++++++++++ 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/mypy/semanal.py b/mypy/semanal.py index 24c9cb7a9e5f..49ff63bd3ab6 100644 --- a/mypy/semanal.py +++ b/mypy/semanal.py @@ -1802,11 +1802,14 @@ def report_missing_module_attribute(self, import_id: str, source_id: str, import # Suggest alternatives, if any match is found. module = self.modules.get(import_id) if module: - alternatives = set(module.names.keys()).difference({source_id}) - matches = best_matches(source_id, alternatives)[:3] - if matches: - suggestion = "; maybe {}?".format(pretty_seq(matches, "or")) - message += "{}".format(suggestion) + if not self.options.implicit_reexport and source_id in module.names.keys(): + message = "Module '{}' does not explicitly export attribute '{}'".format(import_id, source_id) + else: + alternatives = set(module.names.keys()).difference({source_id}) + matches = best_matches(source_id, alternatives)[:3] + if matches: + suggestion = "; maybe {}?".format(pretty_seq(matches, "or")) + message += "{}".format(suggestion) self.fail(message, context, code=codes.ATTR_DEFINED) self.add_unknown_imported_symbol(imported_id, context) diff --git a/test-data/unit/check-flags.test b/test-data/unit/check-flags.test index 37b2c2a92e68..b5020425bf50 100644 --- a/test-data/unit/check-flags.test +++ b/test-data/unit/check-flags.test @@ -1250,6 +1250,18 @@ __all__ = ('b',) [out] main:2: error: Module 'other_module_2' has no attribute 'a' +[case textNoImplicitReexportSuggestions] +# flags: --no-implicit-reexport +from other_module_2 import attr_1 + +[file other_module_1.py] +attr_1 = 5 +attr_2 = 6 +[file other_module_2.py] +from other_module_1 import attr_1, attr_2 +[out] +main:2: error: Module 'other_module_2' does not explicitly export attribute 'attr_1' + [case testNoImplicitReexportMypyIni] # flags: --config-file tmp/mypy.ini from other_module_2 import a From 180ebe17eaf9969a51fb6f5a2e6e00d4547a1cc0 Mon Sep 17 00:00:00 2001 From: Kamil Turek Date: Fri, 31 Jul 2020 20:56:16 +0200 Subject: [PATCH 2/3] Append to message instead of overwriting --- mypy/semanal.py | 2 +- test-data/unit/check-flags.test | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/mypy/semanal.py b/mypy/semanal.py index 49ff63bd3ab6..0ed3301b735b 100644 --- a/mypy/semanal.py +++ b/mypy/semanal.py @@ -1803,7 +1803,7 @@ def report_missing_module_attribute(self, import_id: str, source_id: str, import module = self.modules.get(import_id) if module: if not self.options.implicit_reexport and source_id in module.names.keys(): - message = "Module '{}' does not explicitly export attribute '{}'".format(import_id, source_id) + message += "; implicit reexport disabled" else: alternatives = set(module.names.keys()).difference({source_id}) matches = best_matches(source_id, alternatives)[:3] diff --git a/test-data/unit/check-flags.test b/test-data/unit/check-flags.test index b5020425bf50..6a74e64c13cd 100644 --- a/test-data/unit/check-flags.test +++ b/test-data/unit/check-flags.test @@ -1210,7 +1210,7 @@ a = 5 [file other_module_2.py] from other_module_1 import a [out] -main:2: error: Module 'other_module_2' has no attribute 'a' +main:2: error: Module 'other_module_2' has no attribute 'a'; implicit reexport disabled [case testNoImplicitReexportRespectsAll] # flags: --no-implicit-reexport @@ -1224,7 +1224,7 @@ from other_module_1 import a, b __all__ = ('b',) [builtins fixtures/tuple.pyi] [out] -main:2: error: Module 'other_module_2' has no attribute 'a' +main:2: error: Module 'other_module_2' has no attribute 'a'; implicit reexport disabled [case testNoImplicitReexportStarConsideredImplicit] # flags: --no-implicit-reexport @@ -1234,7 +1234,7 @@ a = 5 [file other_module_2.py] from other_module_1 import * [out] -main:2: error: Module 'other_module_2' has no attribute 'a' +main:2: error: Module 'other_module_2' has no attribute 'a'; implicit reexport disabled [case testNoImplicitReexportStarCanBeReexportedWithAll] # flags: --no-implicit-reexport @@ -1248,7 +1248,7 @@ from other_module_1 import * __all__ = ('b',) [builtins fixtures/tuple.pyi] [out] -main:2: error: Module 'other_module_2' has no attribute 'a' +main:2: error: Module 'other_module_2' has no attribute 'a'; implicit reexport disabled [case textNoImplicitReexportSuggestions] # flags: --no-implicit-reexport @@ -1260,7 +1260,7 @@ attr_2 = 6 [file other_module_2.py] from other_module_1 import attr_1, attr_2 [out] -main:2: error: Module 'other_module_2' does not explicitly export attribute 'attr_1' +main:2: error: Module 'other_module_2' has no attribute 'attr_1'; implicit reexport disabled [case testNoImplicitReexportMypyIni] # flags: --config-file tmp/mypy.ini From af9da279cbb8a13fca7e1af8c87d0c6fd685e2b2 Mon Sep 17 00:00:00 2001 From: Kamil Turek Date: Fri, 14 Aug 2020 21:53:26 +0200 Subject: [PATCH 3/3] Modify message to inform about disabled implicit reexport --- mypy/semanal.py | 3 ++- test-data/unit/check-flags.test | 10 +++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/mypy/semanal.py b/mypy/semanal.py index 0ed3301b735b..1d680eaf3079 100644 --- a/mypy/semanal.py +++ b/mypy/semanal.py @@ -1803,7 +1803,8 @@ def report_missing_module_attribute(self, import_id: str, source_id: str, import module = self.modules.get(import_id) if module: if not self.options.implicit_reexport and source_id in module.names.keys(): - message += "; implicit reexport disabled" + message = ("Module '{}' does not explicitly export attribute '{}'" + "; implicit reexport disabled".format(import_id, source_id)) else: alternatives = set(module.names.keys()).difference({source_id}) matches = best_matches(source_id, alternatives)[:3] diff --git a/test-data/unit/check-flags.test b/test-data/unit/check-flags.test index 6a74e64c13cd..fd775d5c6e01 100644 --- a/test-data/unit/check-flags.test +++ b/test-data/unit/check-flags.test @@ -1210,7 +1210,7 @@ a = 5 [file other_module_2.py] from other_module_1 import a [out] -main:2: error: Module 'other_module_2' has no attribute 'a'; implicit reexport disabled +main:2: error: Module 'other_module_2' does not explicitly export attribute 'a'; implicit reexport disabled [case testNoImplicitReexportRespectsAll] # flags: --no-implicit-reexport @@ -1224,7 +1224,7 @@ from other_module_1 import a, b __all__ = ('b',) [builtins fixtures/tuple.pyi] [out] -main:2: error: Module 'other_module_2' has no attribute 'a'; implicit reexport disabled +main:2: error: Module 'other_module_2' does not explicitly export attribute 'a'; implicit reexport disabled [case testNoImplicitReexportStarConsideredImplicit] # flags: --no-implicit-reexport @@ -1234,7 +1234,7 @@ a = 5 [file other_module_2.py] from other_module_1 import * [out] -main:2: error: Module 'other_module_2' has no attribute 'a'; implicit reexport disabled +main:2: error: Module 'other_module_2' does not explicitly export attribute 'a'; implicit reexport disabled [case testNoImplicitReexportStarCanBeReexportedWithAll] # flags: --no-implicit-reexport @@ -1248,7 +1248,7 @@ from other_module_1 import * __all__ = ('b',) [builtins fixtures/tuple.pyi] [out] -main:2: error: Module 'other_module_2' has no attribute 'a'; implicit reexport disabled +main:2: error: Module 'other_module_2' does not explicitly export attribute 'a'; implicit reexport disabled [case textNoImplicitReexportSuggestions] # flags: --no-implicit-reexport @@ -1260,7 +1260,7 @@ attr_2 = 6 [file other_module_2.py] from other_module_1 import attr_1, attr_2 [out] -main:2: error: Module 'other_module_2' has no attribute 'attr_1'; implicit reexport disabled +main:2: error: Module 'other_module_2' does not explicitly export attribute 'attr_1'; implicit reexport disabled [case testNoImplicitReexportMypyIni] # flags: --config-file tmp/mypy.ini