Skip to content

Commit d2f6e93

Browse files
committed
[suggest] Use the type of a method in a parent class as a guess
1 parent e99a2b5 commit d2f6e93

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

mypy/suggestions.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,22 @@ def find_best(self, func: FuncDef, guesses: List[CallableType]) -> Tuple[Callabl
404404
key=lambda s: (count_errors(errors[s]), self.score_callable(s)))
405405
return best, count_errors(errors[best])
406406

407+
def get_guesses_from_parent(self, node: FuncDef) -> List[CallableType]:
408+
"""Try to get a guess of a method type from a parent class."""
409+
if not node.info:
410+
return []
411+
412+
for parent in node.info.mro[1:]:
413+
pnode = parent.names.get(node.name)
414+
if pnode and isinstance(pnode.node, (FuncDef, Decorator)):
415+
typ = pnode.node.type
416+
if isinstance(typ, CallableType) and len(typ.arg_types) == len(node.arguments):
417+
# Return the first thing we find, since it probably doesn't make sense
418+
# to grab things further up in the chain if an earlier parent has it.
419+
return [pnode.node.type]
420+
421+
return []
422+
407423
def get_suggestion(self, mod: str, node: FuncDef) -> PyAnnotateSignature:
408424
"""Compute a suggestion for a function.
409425
@@ -426,6 +442,7 @@ def get_suggestion(self, mod: str, node: FuncDef) -> PyAnnotateSignature:
426442
callsites,
427443
uses,
428444
)
445+
guesses += self.get_guesses_from_parent(node)
429446
guesses = self.filter_options(guesses, is_method)
430447
best, _ = self.find_best(node, guesses)
431448

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,42 @@ def baz():
790790
() -> str
791791
==
792792

793+
[case testSuggestParent]
794+
# suggest: foo.B.foo
795+
# suggest: foo.B.bar
796+
# suggest: foo.C.foo
797+
[file foo.py]
798+
from typing import TypeVar, Callable, Any
799+
F = TypeVar('F', bound=Callable[..., Any])
800+
def deco(f: F) -> F: ...
801+
802+
class A:
803+
def foo(self, x: int) -> float:
804+
return 0.0
805+
806+
@deco
807+
def bar(self, x: int) -> float:
808+
return 0.0
809+
810+
811+
class B(A):
812+
def foo(self, x):
813+
return 0.0
814+
815+
@deco
816+
def bar(self, x):
817+
return 0.0
818+
819+
class C(B):
820+
def foo(self, x):
821+
return 0.0
822+
823+
[out]
824+
(int) -> float
825+
(int) -> float
826+
(int) -> float
827+
==
828+
793829
[case testSuggestColonBadLocation]
794830
# suggest: tmp/foo.py:7:8:9
795831
[file foo.py]

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy