Skip to content

Commit b678d9f

Browse files
Fix incorrect signature suggestion from dmypy suggest when type name matches imported module name (#18937)
Fixes #18935
1 parent be11ab8 commit b678d9f

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

mypy/suggestions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -852,7 +852,7 @@ def visit_instance(self, t: Instance) -> str:
852852
if self.module:
853853
parts = obj.split(".") # need to split the object part if it is a nested class
854854
tree = self.graph[self.module].tree
855-
if tree and parts[0] in tree.names:
855+
if tree and parts[0] in tree.names and mod not in tree.names:
856856
mod = self.module
857857

858858
if (mod, obj) == ("builtins", "tuple"):

test-data/unit/fine-grained-suggest.test

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,36 @@ foo(B())
207207
(baz.B) -> Tuple[foo.A, foo:A.C]
208208
==
209209

210+
[case testSuggestReexportNamingNameMatchesModule1]
211+
# suggest: foo.foo
212+
[file foo.py]
213+
import bar
214+
def foo():
215+
return bar.bar()
216+
217+
[file bar.py]
218+
class bar: ... # name matches module name
219+
220+
[out]
221+
() -> bar.bar
222+
==
223+
224+
[case testSuggestReexportNamingNameMatchesModule2]
225+
# suggest: foo.foo
226+
[file foo.py]
227+
import bar
228+
import qux
229+
def foo():
230+
return qux.bar()
231+
232+
[file bar.py]
233+
[file qux.py]
234+
class bar: ... # name matches another module name
235+
236+
[out]
237+
() -> qux.bar
238+
==
239+
210240
[case testSuggestInferInit]
211241
# suggest: foo.Foo.__init__
212242
[file foo.py]

0 commit comments

Comments
 (0)