Thanks to visit codestin.com
Credit goes to github.com

Skip to content

__get__ is incorrectly inferred when overloaded using self #19065

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

Closed
finlayacourt opened this issue May 9, 2025 · 1 comment
Closed

__get__ is incorrectly inferred when overloaded using self #19065

finlayacourt opened this issue May 9, 2025 · 1 comment
Labels
bug mypy got something wrong

Comments

@finlayacourt
Copy link

finlayacourt commented May 9, 2025

Bug Report

Define overloads of __get__ for a generic class using self.

class Parent(Generic[_T]):
    def __init__(self, null: _T) -> None: ...
    @overload
    def __get__(self: Parent[Literal[False]], instance, owner) -> int: ...
    @overload
    def __get__(self: Parent[Literal[True]], instance, owner) -> int | None: ...

Define a child of this class.

class Child1(Parent[_T]): ...

Then __get__ is correctly inferred when called directly, but not when called implictly.

class Model:
    child1 = Child1(null=False)
    child1a = Child1(null=True)

reveal_type(Child1(null=False).__get__(None, None)) # Revealed type is "builtins.int"
reveal_type(Child1(null=True).__get__(None, None)) # Revealed type is "Union[builtins.int, None]"
reveal_type(Model().child1) # Revealed type is "builtins.int"
reveal_type(Model().child1a) # Revealed type is "builtins.int" <- INCORRECT

Note that this bug is avoided if the overloads are redefined in the child class.

class Child2(Parent[_T]):
    @overload
    def __get__(self: Child2[Literal[False]], instance, owner) -> int: ...
    @overload
    def __get__(self: Child2[Literal[True]], instance, owner) -> int | None: ...

class Model:
    child2 = Child2(null=False)
    child2a = Child2(null=True)

reveal_type(Child2(null=False).__get__(None, None)) # Revealed type is "builtins.int"
reveal_type(Child2(null=True).__get__(None, None)) # Revealed type is "Union[builtins.int, None]"
reveal_type(Model().child2) # Revealed type is "builtins.int"
reveal_type(Model().child2a) # Revealed type is "Union[builtins.int, None]" <- CORRECT

pyright correctly handles this scenario.

To Reproduce

https://mypy-play.net/?mypy=latest&python=3.13&gist=c81567a4853ba23e6eab357147b6625b

@finlayacourt finlayacourt added the bug mypy got something wrong label May 9, 2025
@sterliakov
Copy link
Collaborator

Works on master, this will be likely included in the 1.16.0 release (whenever it happens).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

2 participants