From 68bcdc3c3778e0a617c2a53c4750c18a7fa95b0c Mon Sep 17 00:00:00 2001 From: Brian Schubert Date: Thu, 17 Apr 2025 14:42:21 -0400 Subject: [PATCH] Fix `dmypy suggest` detection of reexports --- mypy/suggestions.py | 2 +- test-data/unit/fine-grained-suggest.test | 30 ++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/mypy/suggestions.py b/mypy/suggestions.py index 16e630bf8c6e..3eb64448a115 100644 --- a/mypy/suggestions.py +++ b/mypy/suggestions.py @@ -837,7 +837,7 @@ def visit_instance(self, t: Instance) -> str: if self.module: parts = obj.split(".") # need to split the object part if it is a nested class tree = self.graph[self.module].tree - if tree and parts[0] in tree.names: + if tree and parts[0] in tree.names and mod not in tree.names: mod = self.module if (mod, obj) == ("builtins", "tuple"): diff --git a/test-data/unit/fine-grained-suggest.test b/test-data/unit/fine-grained-suggest.test index 0ed3be4055ea..6f80e57f9d60 100644 --- a/test-data/unit/fine-grained-suggest.test +++ b/test-data/unit/fine-grained-suggest.test @@ -207,6 +207,36 @@ foo(B()) (baz.B) -> Tuple[foo.A, foo:A.C] == +[case testSuggestReexportNamingNameMatchesModule1] +# suggest: foo.foo +[file foo.py] +import bar +def foo(): + return bar.bar() + +[file bar.py] +class bar: ... # name matches module name + +[out] +() -> bar.bar +== + +[case testSuggestReexportNamingNameMatchesModule2] +# suggest: foo.foo +[file foo.py] +import bar +import qux +def foo(): + return qux.bar() + +[file bar.py] +[file qux.py] +class bar: ... # name matches another module name + +[out] +() -> qux.bar +== + [case testSuggestInferInit] # suggest: foo.Foo.__init__ [file foo.py]