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

@property returning descriptor instance triggers false positive #9416

Open
dargueta opened this issue Sep 5, 2020 · 2 comments
Open

@property returning descriptor instance triggers false positive #9416

dargueta opened this issue Sep 5, 2020 · 2 comments
Labels
bug mypy got something wrong topic-descriptors Properties, class vs. instance attributes

Comments

@dargueta
Copy link

dargueta commented Sep 5, 2020

🐛 Bug Report

Accessing a field implementing the descriptor protocol via a property method causes MyPy to complain that there is no matching overload for __get__().

To Reproduce

Run MyPy with all default settings on the following code:

from typing import Optional
from typing import overload
from typing import Type

class A:
    pass

class Field:
    def __init__(self, attr: Optional["Field"] = None):
        self.attr = attr

    @property
    def crashing_property(self) -> Optional["Field"]:
        return self.attr

    @overload
    def __get__(self, instance: None, owner: Type["A"]) -> "Field":
        ...

    @overload
    def __get__(self, instance: "A", owner: Type["A"]) -> Optional["Field"]:
        ...

    def __get__(self, instance, owner):
        if not instance:
            return self
        return self.attr

f = Field()  # type: Field
f.attr  # Works fine
f.crashing_property  # Triggers "no overload variant"

n.b. adding __set__() makes no difference

Expected Behavior

Accessing crashing_property shouldn't cause an error.

Actual Behavior

The last line in the example throws the following error:

test.py:39: error: No overload variant of "__get__" of "Field" matches argument types "Field", "Type[Field]"
test.py:39: note: Possible overload variants:
test.py:39: note:     def __get__(self, instance: None, owner: Type[A]) -> Field
test.py:39: note:     def __get__(self, instance: A, owner: Type[A]) -> Optional[Field]

If you add this the code validates fine:

@overload
def __get__(self, instance: "Field", owner: Type["Field"]):
    ...

Your Environment

  • Mypy version used: 0.782, 0.800, 0.812, 0.910
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None, using all defaults
  • Python version used: CPython 3.7.7, 3.8.6, 3.9.1
  • Operating system and version: Ubuntu 20.04, MacOS 11.1 (Big Sur)
@dargueta dargueta added the bug mypy got something wrong label Sep 5, 2020
@dargueta
Copy link
Author

Update: Still happening as of 0.910.

@JelleZijlstra JelleZijlstra added the topic-descriptors Properties, class vs. instance attributes label Mar 19, 2022
@dargueta
Copy link
Author

dargueta commented Jul 6, 2024

Still happening with MyPy 1.10.1 on Python 3.12.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-descriptors Properties, class vs. instance attributes
Projects
None yet
Development

No branches or pull requests

2 participants