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

classes with descriptor properties in Unions #5570

Open
srittau opened this issue Sep 4, 2018 · 2 comments
Open

classes with descriptor properties in Unions #5570

srittau opened this issue Sep 4, 2018 · 2 comments
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal topic-descriptors Properties, class vs. instance attributes topic-union-types

Comments

@srittau
Copy link
Contributor

srittau commented Sep 4, 2018

Please consider:

from typing import Any, Union

class getter:
    def __get__(self, instance: "X1", owner: Any) -> str:
        return ""

class X1:
    prop = getter()

class X2:
    def __init__(self) -> None:
        self.prop = ""

def foo(x: Union[X1, X2]) -> None:
    x.prop

mypy 0.620 (Python 3.6.6, default options) will warn:

foo.py:15: error: Argument 1 to "__get__" of "getter" has incompatible type "Union[X1, X2]"; expected "X1"

Changing the annotation of instance to Any will work around this problem.

@ilevkivskyi ilevkivskyi added bug mypy got something wrong priority-1-normal topic-union-types false-positive mypy gave an error on correct code labels Sep 4, 2018
@ilevkivskyi
Copy link
Member

ilevkivskyi commented Oct 14, 2019

Note there is even more subtle manifestation of this bug if overloads are involved:

class getter:
    @overload
    def __get__(self, instance: None, owner: Any) -> getter: ...
    @overload
    def __get__(self, instance: object, owner: Any) -> str: ...

class X1:
    prop = getter()
class X2:
    prop = getter()

def foo(x: Type[Union[X1, X2]]) -> None:
    reveal_type(x.prop)

This shows str instead of getter because a wrong type is passed for instance (same cause as in the original example). The fix may involve some refactoring for how attributes on unions are accessed.

@daisylb
Copy link

daisylb commented Feb 3, 2022

FWIW, I'm running into this exact same issue on 0.931, because django-stubs uses the instance: None trick on Fields to model descriptors that return themselves when not accessed on an instance.

Another short reproducible example, based on the django-stubs code:
from typing import Type, Union, Generic, TypeVar, overload

T = TypeVar("T")


class Model: ...

_T = TypeVar("_T", bound="Field")
_GT = TypeVar("_GT", covariant=True)

class Field(Generic[_GT]):
    @overload
    def __get__(self: _T, instance: None, owner) -> _T: ...
    # Model instance access
    @overload
    def __get__(self, instance: Model, owner) -> _GT: ...
    # non-Model instances
    @overload
    def __get__(self: _T, instance, owner) -> _T: ...


class A:
    attr: str


class B(Model):
    attr: Field[str] = Field()

b_inst: B
a_or_b_inst: Union[A, B]

reveal_type(B.attr) # test.Field[builtins.str]
reveal_type(b_inst.attr) # builtins.str*
reveal_type(a_or_b_inst.attr) # Union[builtins.str, test.Field[builtins.str]]
                              # (should just be builtins.str)

@JelleZijlstra JelleZijlstra added the topic-descriptors Properties, class vs. instance attributes label Mar 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal topic-descriptors Properties, class vs. instance attributes topic-union-types
Projects
None yet
Development

No branches or pull requests

4 participants