cached_property in slotted classes doesn't work if overridden in child class
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 38/100
- Tipo di issue
- Bug
- Chiarezza
- Abbastanza chiara
- Stato di attività
- Ferma
- Stack tecnologico
- python
- Ambito
- developer-experience
Direzione di ricerca
Start in src/attr/_make.py around the generated getattr handling described in the issue, and read the cached-properties-on-slotted-classes documentation. Run the provided Parent/Child reproducer and trace the generated lookup across the MRO. Done means Child().name can call the parent cached_property and produce "Bob (son of Alice)" without disabling slots.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
With attrs 24.2.0 and 23.2.0, if you use cached_property and try to override it in a child, and access the parent value, you get an AttributeError. We ran into this in our code when converting some classes to use slots.
I see there was support added in 24.2.0 for
Allow super() calls in slotted cached properties
But attrs still doesn't support "super() calls of overridden cached_property's in slotted classes":
from functools import cached_property
import attrs
@attrs.define(slots=True)
class Parent:
@cached_property
def name(self) -> str:
return "Alice"
@attrs.define(slots=True)
class Child(Parent):
@cached_property
def name(self) -> str:
return f"Bob (son of {super().name})"
p = Parent()
print(p.name) # prints Alice
c = Child()
print(c.name) # error:
Traceback (most recent call last):
File "foo.py", line 23, in <module>
print(c.name)
^^^^^^
File "<attrs generated getattr __main__.Child>", line 6, in __getattr__
File "foo.py", line 18, in name
return f"Bob (son of {super().name})"
^^^^^^^^^^^^
AttributeError: 'Child' object has no attribute 'name'
Workarounds:
- disable slots
- don't use cached_property
- don't override the method
Elaboration:
I see the special handling is described in the docs and I see in the code that it generates a special __getattr__ that sets the generated value in the slot after being called. Perhaps when constructing a class, attrs should also walk the MRO for cached_propertys and handle any overridden cached_property methods — perhaps create some sort of namespaced/name-mangled properties for the parent.
- Lingua principale
- Python
- Stelle
- 5.8k
- Fork
- 480
- Merge medio
- 2h 15m
- PR unite (30g)
- 2
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.
Altre issue di python-attrs/attrs
-
Difficoltà 5/5 Più di una settimana Idoneità per principianti 20/100
python-attrs/attrs#1620 ·
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 55/100
python-attrs/attrs#1596 · 2 commenti · 1 reazione ·
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 35/100
python-attrs/attrs#1549 · 3 commenti ·
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 38/100
python-attrs/attrs#1543 · 2 commenti ·
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 35/100
python-attrs/attrs#1532 ·
Tutte le issue di python-attrs/attrs
Issue simili
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
stephrobert/dsoxlab#238 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
sublimehq/package_control#1780 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 65/100
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
nwg-piotr/nwg-displays#145 ·