Repository navigation
False positives when redefining property in subclass #5936
Description
Activity
Yeah, this is something that mypy should perhaps support. This is the first time I've seen this idiom, so it's probably not very common though.
This workaround works currently:
class Base: def __init__(self, value: int) -> None: self._value = value @property def value(self) -> int: return self._value class Sub(Base): @property def value(self) -> int: return super().value @value.setter def value(self, value: int) -> None: self._value = value
Reacted by Edgar Ramírez Mondragón and Curly-Howard-Chungus Correspondence | Lamport-Cabot-Codd-Backus-Naur Form- addedfalse-positivemypy gave an error on correct codemypy gave an error on correct code
on Nov 22, 2018 This problem might be a special case of #4644.
Actually it looks like both are a special case of #1465, as pointed out in this comment.
Changing
@value.setterto@Base.value.setterin your example is accepted by mypy. So it seems the problem is not theBase.valueidiom, but the fact that mypy doesn't recognize a property setter unless it is directly preceded by the getter.Reacted by Nick Boultbee and Arsenii A.I had the same issue. Because I have a lot of sub-classes and I don't want to copy the property around so many times I ended up doing this:
- added a type-ignore comment to the decorators where I overwrite the setters, like
@Base.value.setter # type: ignore[attr-defined] - made a comment in the base-class explaining why I do this and pointing to this post
- added a type-ignore comment to the decorators where I overwrite the setters, like
- addedtopic-descriptorsProperties, class vs. instance attributesProperties, class vs. instance attributes
on Apr 1, 2022 - added a commit that references this issue
on Sep 7, 2022 - added 2 commits that reference this issue
on Oct 8, 2022 I've run in to this issue as well and created #14684, and I've found another work-around that produces no errors from MyPy. It seems to not be problematic if the property in the base class is declared using the
property()function as opposed to the decorators, like below, note that my specific use case was attempting to only override the setter of the Base class.class Base: def __init__(self, value: int) -> None: self._value = value def _get_value(self) -> int: return self._value def _set_value(self, value: int): self._value = value value = property(_get_value, _set_value) class Sub(Base): def __init__(self, value: int) -> None: super().__init__(value) @Base.value.setter def value(self, value: int) -> None: Base.value.fset(self, value) print("Do Something after the Base setter")
- added 2 commits that reference this issue
on Jun 18, 2026
I'm running mypy on the following code: (no additional arguments)
And it reports:
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:
This results in mypy reporting:
Again the error is reported on the
@Base.value.setterline.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.