False positives when redefining property in subclass
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
I'm running mypy on the following code: (no additional arguments)
class Base:
def __init__(self, value):
self._value = value
@property
def value(self):
return self._value
class Sub(Base):
@Base.value.setter
def value(self, value):
self._value = value
And it reports:
inherited_property.py:10: error: "Callable[[Any], Any]" has no attribute "setter"
Line 10 is @Base.value.setter, so it seems to have a problem with referring to a property defined in a different class. Python itself has no problem with this though.
If the property has a setter in the base class, a slightly different error is reported:
class Base:
def __init__(self, value):
self._value = value
@property
def value(self):
return self._value
@value.setter
def value(self, value):
self._value = value
class Sub(Base):
@Base.value.setter
def value(self, value):
self._value = value
This results in mypy reporting:
inherited_property.py:14: error: overloaded function has no attribute "setter"
Again the error is reported on the @Base.value.setter line.
I'm using a mypy dev snapshot (0.650+) on Python 3.6.5.
I found the existing issue #220, but that is so generic that I figured it would make sense to open a new separate issue for tracking this specific use case.
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
Reproduce the reported diagnostics with the two Python examples, starting with the handling of @property and inherited attribute references in mypy. Trace why Base.value is treated as a callable or overloaded function, then add coverage showing that subclass setters using @Base.value.setter are accepted without errors.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100