Error when overriding a property created via decorators
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
Bug Report
When a parent class has a property, created via decorators with a getter and setter. If you attempt to override the fset of the parent by using decorators in the subclass, mypy throws an error.
To Reproduce
class A:
def __init__(self, x: int):
self._x = x
@property
def x(self) -> int:
return self._x
@x.setter
def x(self, new_value: int):
self._x = new_value
class B(A):
def __init__(self):
super().__init__(100)
@A.x.setter
def x(self, new_value: int):
A.x.fset(self, new_value)
print("Do something else ontop of the parent setter")
Expected Behavior
Mypy should produce no errors
Actual Behavior
Mypy produces the errors:
overloaded function has no attribute "setter" [attr-defined] on the decorator line in the subclass.
overloaded function has no attribute "fset" [attr-defined] on the call to fset in the subclass.
If instead of using a decorator, the property is created in the parent class via the property() function like below, then mypy does not produce any errors.
class A:
def __init__(self, x: int):
self._x = x
def _get_x(self) -> int:
return self._x
def _set_x(self, new_value: int):
self._x = new_value
x = property(_get_x, _set_x)
Your Environment
- Mypy version used: 1.0.0
- Mypy command-line flags: None
- Mypy configuration options from
mypy.ini(and other config files): Default configuration - Python version used: 3.10.5
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne damit, die gemeldeten Fehler mit den bereitgestellten Python-Beispielen unter mypy 1.0.0 und der Standardkonfiguration zu reproduzieren. Finde den Einstiegspunkt der Typprüfung für dekorierte Properties und füge einen Regressionstest hinzu, der Setter-Überschreibungen in Unterklassen und den Zugriff auf fset des übergeordneten Elements abdeckt. Als erledigt gilt die Aufgabe, wenn beide Beispiele ohne attr-defined-Fehler die Typprüfung bestehen.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- tooling
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 38/100