Initialization order of fields overridden in subclasses
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Idoneità per principianti
- 25/100
Direzione di ricerca
Start by reproducing the provided attrs example and reading the subclassing discussion in related issue #637, alongside the referenced dataclasses inheritance behavior in PEP 557. The issue does not name implementation files or tests; completion would require maintainer agreement on the intended initialization semantics and a verified resolution for the failing BetterXContainer case.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
I have a code similar to the following, that relies very heavily on mypy for correctness:
import attr
class X:
""" Some other class from the code """
class XButBetter(X):
""" Some other class from the code """
@attr.s(auto_attribs=True)
class XContainer:
the_x: X
x_metadata: int = attr.ib(init=False)
@x_metadata.default
def _x_metadata_default(self) -> int:
return self.the_x.get_metadata()
def some_useful_function(self) -> str:
""" A useful but very complex function dealing with self.the_x """
@attr.s(auto_attribs=True)
class BetterXContainer(XContainer):
the_x: XButBetter
def some_function(self) -> None:
""" some code assuming the_x is the better version """
xc = XContainer(X())
xcb = BetterXContainer(XButBetter())
The last line fails with AttributeError: 'BetterXContainer' object has no attribute 'the_x', because the initialization order of the attributes changed in the subclass. Another similar issue is described on stackoverflow.
Any plans on implementing subclassing similarly to dataclasses? Would that be a breaking API change? (Probably.)
Related attrs issue I found: #637
PS.: I realize this is subclassing issue n+1 when subclassing is frowned upon, and this is somewhat similar to the classic OOP problem of subclassing Circle from Ellipse, so feel free to just say "damn man, this is not proper OOP, so wontfix".
- 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 ·