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

__getitem__ as a property doesn't evaluate typing of __get__ #9653

Open
adhami3310 opened this issue Jan 2, 2025 · 0 comments
Open

__getitem__ as a property doesn't evaluate typing of __get__ #9653

adhami3310 opened this issue Jan 2, 2025 · 0 comments
Labels
addressed in next version Issue is fixed and will appear in next published version bug Something isn't working

Comments

@adhami3310
Copy link

Similar to #9574 which fixed it for some dunder methods, but not for __getitem__ (not sure how many others?).

Example code:

from __future__ import annotations

from typing_extensions import Self, TypeVar, overload, 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:
    __getitem__ = Function[Self, int]()


x = ArrayLike().__getitem__(1)
y = ArrayLike()[1]

The second line should be identical to the first, but the type system doesn't evaluate the __get__.

@adhami3310 adhami3310 added the bug Something isn't working label Jan 2, 2025
erictraut added a commit that referenced this issue Jan 3, 2025
…agic method modeling to handle descriptor objects. This addresses #9653.
erictraut added a commit that referenced this issue Jan 3, 2025
…agic method modeling to handle descriptor objects. This addresses #9653. (#9654)
@erictraut erictraut added the addressed in next version Issue is fixed and will appear in next published version label Jan 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addressed in next version Issue is fixed and will appear in next published version bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants