Skip to content

overload, Protocol and generic self: annotations #19381

@Alan-Chen99

Description

@Alan-Chen99

overload with a self annotation involving a protocol never get matched:

class Proto[A_contra](Protocol):
    def __call__(self, arg: A_contra, /) -> None: ...

class Fn:
    def __call__(self, arg: int, /) -> None:
        pass

@dataclass(frozen=True)
class Testclass[T_co]:
    val: T_co

    # this overload is problematic
    @overload
    def meth[T2](self: Testclass[Proto[T2]], arg: T2) -> int: ...
    @overload
    def meth(self, arg: None) -> str: ...
    def meth(self: Any, *args, **kwargs):
        return self.val

def fn(x: Testclass[Fn]):
    # error: No overload variant of "meth" of "Testclass" matches argument type "int"  [call-overload]
    # note: Possible overload variant:
    # note:     def meth(self, arg: None) -> str
    x.meth(5)

it works fine if it is not an overload, or if using Callable rather than Proto.
https://mypy-play.net/?mypy=latest&python=3.12&gist=39ec9991c68ca9ed50f149359459e858
this works fine in pyright.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions