Skip to content

Fix binding difference between callables and callback protocols #15993

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

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
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
10 changes: 7 additions & 3 deletions mypy/checkmember.py
Original file line number Diff line number Diff line change
Expand Up @@ -794,12 +794,16 @@ def analyze_var(

call_type: ProperType | None = None
if var.is_initialized_in_class and (not is_instance_var(var) or mx.is_operator):
if isinstance(typ, FunctionLike) and not typ.is_type_obj():
call_type = typ
elif var.is_property:
if var.is_property and (not isinstance(typ, FunctionLike) or typ.is_type_obj()):
call_type = get_proper_type(_analyze_member_access("__call__", typ, mx))
else:
call_type = typ
if (
isinstance(call_type, Instance)
and call_type.type.is_protocol
and call_type.type.get_method("__call__")
):
call_type = get_proper_type(_analyze_member_access("__call__", typ, mx))

if isinstance(call_type, FunctionLike) and not call_type.is_type_obj():
if mx.is_lvalue:
Expand Down
38 changes: 38 additions & 0 deletions test-data/unit/check-protocols.test
Original file line number Diff line number Diff line change
Expand Up @@ -4138,6 +4138,44 @@ class P(Protocol):
class C(P): ...
C(0) # OK

[case testCallbackProtocolMethod]
from typing import Callable, Protocol

class CallbackProtocol(Protocol):
def __call__(self, __x: int) -> int: ...

class CallableObject:
attribute: str
def __call__(self, __x: int) -> int:
return 3

def make_method_protocol() -> CallbackProtocol: ...
def make_method_callable() -> Callable[[int], int]: ...
def make_method_object() -> CallableObject: ...

class ClsProto:
meth = make_method_protocol()

class ClsCall:
meth = make_method_callable()

class ClsObject:
meth = make_method_object()

reveal_type(ClsProto.meth) # N: Revealed type is "__main__.CallbackProtocol"
reveal_type(ClsCall.meth) # N: Revealed type is "def (builtins.int) -> builtins.int"
reveal_type(ClsObject.meth) # N: Revealed type is "__main__.CallableObject"

def takes(p: ClsProto, c: ClsCall, o: ClsObject) -> None:
reveal_type(p.meth(0)) # E: Invalid self argument "ClsProto" to attribute function "meth" with type "Callable[[int], int]" \
# E: Too many arguments for "__call__" of "CallbackProtocol" \
# N: Revealed type is "builtins.int"
reveal_type(c.meth(0)) # E: Invalid self argument "ClsCall" to attribute function "meth" with type "Callable[[int], int]" \
# E: Too many arguments \
# N: Revealed type is "builtins.int"
reveal_type(o.meth(0)) # N: Revealed type is "builtins.int"
reveal_type(o.meth.attribute) # N: Revealed type is "builtins.str"

[case testTypeVarValueConstraintAgainstGenericProtocol]
from typing import TypeVar, Generic, Protocol, overload

Expand Down
Loading