mypy icon indicating copy to clipboard operation
mypy copied to clipboard

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

Open JukkaL opened this issue 1 year ago • 5 comments

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.)

JukkaL avatar Sep 27 '24 09:09 JukkaL

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 avatar Sep 29 '24 08:09 tasfia8

Could I be assigned this?

tasfia8 avatar Sep 30 '24 20:09 tasfia8

Feel free to work on this and send a PR!

JelleZijlstra avatar Sep 30 '24 20:09 JelleZijlstra

Hello, Is it still open, Can I do this issue?

ChengPuPu avatar Oct 12 '24 11:10 ChengPuPu

Hello @ChengPuPu ! I am working on this issue.

tasfia8 avatar Oct 12 '24 19:10 tasfia8

This was fixed by https://github.com/python/mypy/pull/18214.

A5rocks avatar Feb 19 '25 06:02 A5rocks