microsoft / pyright

Static Type Checker for Python
Other
13.29k stars 1.44k forks source link

Pyright hangs on code involving polymorphic functions #8987

Open LeeeeT opened 1 month ago

LeeeeT commented 1 month ago

Pyright hangs on the following code:

from collections.abc import Callable
from typing import Protocol

class MyProto(Protocol):
    def __call__[A, B](self, a: Callable[[A], B], b: Callable[[Callable[[A], B]], A]) -> B: ...

p: MyProto = lambda a: lambda b: a(b)

The code contains a type error on purpose.

LeeeeT commented 1 month ago

If I fix the type error in the implementation...

p: MyProto = lambda a, b: a(b(a))

...pyright correctly shows no errors.

But if I then curry both the protocol and the implementation using my favorite ✨ curry 💅 function...

def curry[First, *Rest, Result](function: Callable[[First, *Rest], Result]) -> Callable[[*Rest], Callable[[First], Result]]:
    return lambda *rest: lambda first: function(first, *rest)

class MyProto(Protocol):
    @curry
    def __call__[A, B](self, a: Callable[[A], B], b: Callable[[Callable[[A], B]], A]) -> B: ...

p: MyProto = curry(lambda a, b: a(b(a)))

...pyright, again, hangs.