Skip to content

[suggest] Use the type of a method in a parent class as a guess #7980

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions mypy/suggestions.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,23 @@ def find_best(self, func: FuncDef, guesses: List[CallableType]) -> Tuple[Callabl
key=lambda s: (count_errors(errors[s]), self.score_callable(s)))
return best, count_errors(errors[best])

def get_guesses_from_parent(self, node: FuncDef) -> List[CallableType]:
"""Try to get a guess of a method type from a parent class."""
if not node.info:
return []

for parent in node.info.mro[1:]:
pnode = parent.names.get(node.name)
if pnode and isinstance(pnode.node, (FuncDef, Decorator)):
typ = get_proper_type(pnode.node.type)
# FIXME: Doesn't work right with generic tyeps
if isinstance(typ, CallableType) and len(typ.arg_types) == len(node.arguments):
# Return the first thing we find, since it probably doesn't make sense
# to grab things further up in the chain if an earlier parent has it.
return [typ]

return []

def get_suggestion(self, mod: str, node: FuncDef) -> PyAnnotateSignature:
"""Compute a suggestion for a function.

Expand All @@ -430,6 +447,7 @@ def get_suggestion(self, mod: str, node: FuncDef) -> PyAnnotateSignature:
callsites,
uses,
)
guesses += self.get_guesses_from_parent(node)
guesses = self.filter_options(guesses, is_method, ignore_return=True)
best, _ = self.find_best(node, guesses)

Expand Down
36 changes: 36 additions & 0 deletions test-data/unit/fine-grained-suggest.test
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,42 @@ def baz():
() -> str
==

[case testSuggestParent]
# suggest: foo.B.foo
# suggest: foo.B.bar
# suggest: foo.C.foo
[file foo.py]
from typing import TypeVar, Callable, Any
F = TypeVar('F', bound=Callable[..., Any])
def deco(f: F) -> F: ...

class A:
def foo(self, x: int) -> float:
return 0.0

@deco
def bar(self, x: int) -> float:
return 0.0


class B(A):
def foo(self, x):
return 0.0

@deco
def bar(self, x):
return 0.0

class C(B):
def foo(self, x):
return 0.0

[out]
(int) -> float
(int) -> float
(int) -> float
==

[case testSuggestColonBadLocation]
# suggest: tmp/foo.py:7:8:9
[file foo.py]
Expand Down
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