Skip to content

support Callable / callable Protocols in suggest decorator unwarpping #19072

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
14 changes: 13 additions & 1 deletion mypy/suggestions.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,18 @@ def is_implicit_any(typ: Type) -> bool:
return isinstance(typ, AnyType) and not is_explicit_any(typ)


def _arg_accepts_function(typ: ProperType) -> bool:
return (
# TypeVar / Callable
isinstance(typ, (TypeVarType, CallableType))
or
# Protocol with __call__
isinstance(typ, Instance)
and typ.type.is_protocol
and typ.type.get_method("__call__") is not None
)


class SuggestionEngine:
"""Engine for finding call sites and suggesting signatures."""

Expand Down Expand Up @@ -659,7 +671,7 @@ def extract_from_decorator(self, node: Decorator) -> FuncDef | None:
for ct in typ.items:
if not (
len(ct.arg_types) == 1
and isinstance(ct.arg_types[0], TypeVarType)
and _arg_accepts_function(get_proper_type(ct.arg_types[0]))
and ct.arg_types[0] == ct.ret_type
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this become is_same_type to allow aliases?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume you mean the == line?

probably! but that'd be a separate issue / patch I would imagine

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, sorry, misclicked - I meant the == line of course.

):
return None
Expand Down
32 changes: 32 additions & 0 deletions test-data/unit/fine-grained-suggest.test
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,38 @@ foo3('hello hello')
(str) -> str
==

[case testSuggestInferFuncDecorator6]
# suggest: foo.f
[file foo.py]
from __future__ import annotations

from typing import Callable, Protocol, TypeVar
from typing_extensions import ParamSpec

P = ParamSpec('P')
R = TypeVar('R')
R_co = TypeVar('R_co', covariant=True)

class Proto(Protocol[P, R_co]):
def __call__(self, *args: P.args, **kwargs: P.kwargs) -> R_co: ...

def dec1(f: Callable[P, R]) -> Callable[P, R]: ...
def dec2(f: Callable[..., R]) -> Callable[..., R]: ...
def dec3(f: Proto[P, R_co]) -> Proto[P, R_co]: ...

@dec1
@dec2
@dec3
def f(x):
return x

f('hi')

[builtins fixtures/isinstancelist.pyi]
[out]
(str) -> str
==

[case testSuggestFlexAny1]
# suggest: --flex-any=0.4 m.foo
# suggest: --flex-any=0.7 m.foo
Expand Down