@property returning descriptor instance triggers false positive
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
🐛 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)
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the provided Python reproducer with MyPy's default settings and confirm the false-positive overload error on the property access. The work is done when that access validates without requiring an extra get overload for Field instances, while the existing descriptor behavior remains valid.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100