Skip to content

callable protocols cannot infer the type of their own self #15188

Description

@glyph

Bug Report

Methods on protocols with a __call__ method cannot infer the type of self
as callable in other methods.

To Reproduce

https://mypy-play.net/?mypy=latest&python=3.11&flags=strict&gist=7234281168a0762dd9a81b1f10d8a96a

from typing import Callable, ParamSpec, Protocol, TypeVar

Params = ParamSpec("Params")
Result = TypeVar("Result", covariant=True)

class FancyMethod(Protocol):

    def __call__(self, arg1: int, arg2: str) -> list[float]:
        """
        Calling me results in a very specific signature.
        """

    def returnMe(self: Callable[Params, Result]) -> Callable[Params, Result]:
        ...

def externalGet(self: Callable[Params, Result]) -> Callable[Params, Result]:
    return self

Expected Behavior

I would expect that this would be inferred in returnMe the same way that it would be in externalGet.

Actual Behavior

main.py:13: error: The erased type of self "def (*Params.args, **Params.kwargs) -> Result`-2" is not a supertype of its class "__main__.FancyMethod"  [misc]

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions