microsoft / pyright

Static Type Checker for Python
Other
13.48k stars 1.48k forks source link

assigning a class with __call__ to dunder method shows the wrong type #9470

Open adhami3310 opened 1 week ago

adhami3310 commented 1 week ago
from typing_extensions import Self, reveal_type

class Function:
    def __call__(self, *args, **kwargs) -> Self:
        return self

class ArrayLike:
    __getitem__ = Function()

x = ArrayLike()[0]
reveal_type(x) # Type of "x" is "ArrayLike"
print(type(x))

but prints: <class '__main__.Function'>, as self is Function. This doesn't feel like intended behavior.

I'm using pyright 1.1.389.

adhami3310 commented 1 week ago

Interestingly, x = ArrayLike().__getitem__(0) does show x: Function. So there's a discrepancy, even though the two are equivalent.

adhami3310 commented 1 week ago

mypy gets it right: https://mypy-play.net/?mypy=latest&python=3.12&gist=db9e2a8587ea15861d380e653fbd2d0b