Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dunder method access to inner class __call__ ignores __get__ overloads #9574

Open
adhami3310 opened this issue Dec 12, 2024 · 0 comments
Open
Labels
bug Something isn't working

Comments

@adhami3310
Copy link

from __future__ import annotations

from typing_extensions import Self, TypeVar, overload, reveal_type, TypeVarTuple

S = TypeVar("S")
R = TypeVarTuple("R")


class Function[*T]:
    def __init__(self, *prefilled_args):
        self.prefilled_args = prefilled_args

    def __call__(self, *args: *T) -> None:
        print(f"{self.prefilled_args + args=}")

    def partial_once[S, *R](self: Function[S, *R], arg: S) -> Function[*R]:
        return Function(*self.prefilled_args, arg)

    @overload
    def __get__(self, instance: None, owner) -> Self: ...

    @overload
    def __get__(self: Function[S, *R], instance: S, owner) -> Function[*R]: ...

    def __get__(self, instance, owner):
        if instance is None:
            return self
        return self.partial_once(instance)


class ArrayLike:
    __add__ = Function[Self, int]()


x = ArrayLike().__add__(1)
y = ArrayLike() + 1

Some of the code here is a bit much, but I wanted to illustrate some form of useful code that would use such functionality.

Both calls succeed just fine in runtime with the expected output:

self.prefilled_args + args=(<__main__.ArrayLike object at 0x7f2c575370e0>, 1)

Yet Pyright errors:

error: Operator "+" not supported for types "ArrayLike" and "Literal[1]" (reportOperatorIssue)

only for the second one.

It seems when using dunder methods, pyright is not evaluating the get in the type checking.

MyPy fails both which I assume is more related to using TypeVarTuple, but Pyright seems to handle that better.

Note in the example above we have another typing error at self.partial_once(instance) but that one is correctly caught.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant