Error when overriding a property created via decorators
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 20.6k
- Fork
- 3.3k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
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
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia riproducendo gli errori segnalati con gli esempi Python forniti usando mypy 1.0.0 e la configurazione predefinita. Individua il punto di ingresso del controllo dei tipi per le property decorate e aggiungi un test di regressione che copra gli override del setter nelle sottoclassi e l’accesso a fset dell’elemento padre. Il lavoro è completato quando entrambi gli esempi superano il controllo dei tipi senza errori attr-defined.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- tooling
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 38/100