Skip to content

Show the signature of __call__ for arguments with incompatible types … #18214

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
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
5 changes: 4 additions & 1 deletion mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -870,7 +870,10 @@ def incompatible_argument_note(
)
if call:
self.note_call(original_caller_type, call, context, code=code)

if isinstance(callee_type, Instance) and callee_type.type.is_protocol:
call = find_member("__call__", callee_type, callee_type, is_operator=True)
if call:
self.note_call(callee_type, call, context, code=code)
self.maybe_note_concatenate_pos_args(original_caller_type, callee_type, context, code)

def maybe_note_concatenate_pos_args(
Expand Down
12 changes: 8 additions & 4 deletions test-data/unit/check-inference.test
Original file line number Diff line number Diff line change
Expand Up @@ -3677,7 +3677,8 @@ def f(x: Call[T]) -> Tuple[T, T]: ...

def g(__x: str) -> None: pass
reveal_type(f(g)) # N: Revealed type is "Tuple[Never, Never]" \
# E: Argument 1 to "f" has incompatible type "Callable[[str], None]"; expected "Call[Never]"
# E: Argument 1 to "f" has incompatible type "Callable[[str], None]"; expected "Call[Never]" \
# N: "Call[Never].__call__" has type "Callable[[NamedArg(Never, 'x')], None]"
[builtins fixtures/list.pyi]

[case testCallableInferenceAgainstCallableNamedVsPosOnly]
Expand All @@ -3693,7 +3694,8 @@ def f(x: Call[T]) -> Tuple[T, T]: ...

def g(*, x: str) -> None: pass
reveal_type(f(g)) # N: Revealed type is "Tuple[Never, Never]" \
# E: Argument 1 to "f" has incompatible type "Callable[[NamedArg(str, 'x')], None]"; expected "Call[Never]"
# E: Argument 1 to "f" has incompatible type "Callable[[NamedArg(str, 'x')], None]"; expected "Call[Never]" \
# N: "Call[Never].__call__" has type "Callable[[Never], None]"
[builtins fixtures/list.pyi]

[case testCallableInferenceAgainstCallablePosOnlyVsKwargs]
Expand All @@ -3709,7 +3711,8 @@ def f(x: Call[T]) -> Tuple[T, T]: ...

def g(**x: str) -> None: pass
reveal_type(f(g)) # N: Revealed type is "Tuple[Never, Never]" \
# E: Argument 1 to "f" has incompatible type "Callable[[KwArg(str)], None]"; expected "Call[Never]"
# E: Argument 1 to "f" has incompatible type "Callable[[KwArg(str)], None]"; expected "Call[Never]" \
# N: "Call[Never].__call__" has type "Callable[[Never], None]"
[builtins fixtures/list.pyi]

[case testCallableInferenceAgainstCallableNamedVsArgs]
Expand All @@ -3725,7 +3728,8 @@ def f(x: Call[T]) -> Tuple[T, T]: ...

def g(*args: str) -> None: pass
reveal_type(f(g)) # N: Revealed type is "Tuple[Never, Never]" \
# E: Argument 1 to "f" has incompatible type "Callable[[VarArg(str)], None]"; expected "Call[Never]"
# E: Argument 1 to "f" has incompatible type "Callable[[VarArg(str)], None]"; expected "Call[Never]" \
# N: "Call[Never].__call__" has type "Callable[[NamedArg(Never, 'x')], None]"
[builtins fixtures/list.pyi]

[case testInferenceAgainstTypeVarActualBound]
Expand Down
24 changes: 16 additions & 8 deletions test-data/unit/check-protocols.test
Original file line number Diff line number Diff line change
Expand Up @@ -2473,7 +2473,8 @@ def func(caller: Caller) -> None:
pass

func(call)
func(bad) # E: Argument 1 to "func" has incompatible type "Callable[[int, VarArg(str)], None]"; expected "Caller"
func(bad) # E: Argument 1 to "func" has incompatible type "Callable[[int, VarArg(str)], None]"; expected "Caller" \
# N: "Caller.__call__" has type "Callable[[Arg(str, 'x'), VarArg(int)], None]"
[builtins fixtures/tuple.pyi]
[out]

Expand Down Expand Up @@ -2510,7 +2511,8 @@ def func(caller: Caller) -> None:
pass

func(call)
func(bad) # E: Argument 1 to "func" has incompatible type "Callable[[int], int]"; expected "Caller"
func(bad) # E: Argument 1 to "func" has incompatible type "Callable[[int], int]"; expected "Caller" \
# N: "Caller.__call__" has type "Callable[[Arg(T, 'x')], T]"
[builtins fixtures/tuple.pyi]
[out]

Expand All @@ -2530,7 +2532,8 @@ def func(caller: Caller) -> None:
pass

func(call)
func(bad) # E: Argument 1 to "func" has incompatible type "Callable[[T], Tuple[T, T]]"; expected "Caller"
func(bad) # E: Argument 1 to "func" has incompatible type "Callable[[T], Tuple[T, T]]"; expected "Caller" \
# N: "Caller.__call__" has type "Callable[[Arg(int, 'x')], int]"
[builtins fixtures/tuple.pyi]
[out]

Expand All @@ -2557,7 +2560,8 @@ def func(caller: Caller) -> None:
pass

func(call)
func(bad) # E: Argument 1 to "func" has incompatible type "Callable[[Union[int, str]], Union[int, str]]"; expected "Caller"
func(bad) # E: Argument 1 to "func" has incompatible type "Callable[[Union[int, str]], Union[int, str]]"; expected "Caller" \
# N: "Caller.__call__" has type overloaded function
[out]

[case testCallableImplementsProtocolExtraNote]
Expand Down Expand Up @@ -2596,7 +2600,8 @@ def anon(caller: CallerAnon) -> None:


func(call)
func(bad) # E: Argument 1 to "func" has incompatible type "Callable[[str], None]"; expected "Caller"
func(bad) # E: Argument 1 to "func" has incompatible type "Callable[[str], None]"; expected "Caller" \
# N: "Caller.__call__" has type "Callable[[Arg(str, 'x')], None]"
anon(bad)
[out]

Expand All @@ -2619,7 +2624,8 @@ a: Other
b: Bad

func(a)
func(b) # E: Argument 1 to "func" has incompatible type "Bad"; expected "One"
func(b) # E: Argument 1 to "func" has incompatible type "Bad"; expected "One" \
# N: "One.__call__" has type "Callable[[Arg(str, 'x')], None]"
[out]

[case testJoinProtocolCallback]
Expand Down Expand Up @@ -3589,7 +3595,8 @@ test(C) # E: Argument 1 to "test" has incompatible type "Type[C]"; expected "P"
# N: Expected: \
# N: def __call__(x: int, y: int) -> Any \
# N: Got: \
# N: def __init__(x: int, y: str) -> C
# N: def __init__(x: int, y: str) -> C \
# N: "P.__call__" has type "Callable[[Arg(int, 'x'), Arg(int, 'y')], Any]"

[case testProtocolClassObjectPureCallback]
from typing import Any, ClassVar, Protocol
Expand All @@ -3610,7 +3617,8 @@ test(C) # E: Argument 1 to "test" has incompatible type "Type[C]"; expected "P"
# N: Expected: \
# N: def __call__(x: int, y: int) -> Any \
# N: Got: \
# N: def __init__(x: int, y: str) -> C
# N: def __init__(x: int, y: str) -> C \
# N: "P.__call__" has type "Callable[[Arg(int, 'x'), Arg(int, 'y')], Any]"
[builtins fixtures/type.pyi]

[case testProtocolClassObjectCallableError]
Expand Down
Loading