python / mypy

Optional static typing for Python
https://www.mypy-lang.org/
Other
18.28k stars 2.8k forks source link

If Callable type is incompatible with a callable protocol, show signature of __call__ #17840

Open JukkaL opened 1 week ago

JukkaL commented 1 week ago

Feature

We sometimes show the signature of __call__ when a value with a callable type is incompatible with a callback protocol, but not always. At least when checking argument types, we don't show the signature of __call__:

from collections.abc import Callable
from typing import Protocol

class C(Protocol):
    def __call__(self, a: int, b: str, c: bytes = b'', /) -> None: ...

def foo(c: C) -> None: ...

f: Callable[[int, str, bytes], None]
c: C

# error: Incompatible types in assignment (expression has type "Callable[[int, str, bytes], None]", variable has type "C")
# note: "C.__call__" has type "Callable[[int, str, DefaultArg(bytes)], None]"
c = f

# error: Argument 1 to "foo" has incompatible type "Callable[[int, str, bytes], None]"; expected "C"
foo(f)

I'd expect mypy to generate the note for the foo(f) line as well.

(Also, DefaultArg(bytes) is awkward in the note, but that's a separate issue.)

tasfia8 commented 6 days ago

Hello JukkaL, nice to read your good first issue! I am a Computer Engineering student and am actively looking for a good open-source first issue that I can work on for my school project. Can I take a look at this?

tasfia8 commented 5 days ago

Could I be assigned this?

JelleZijlstra commented 5 days ago

Feel free to work on this and send a PR!