Passing arguments to `__attrs_init_subclass__`
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 35/100
Direzione di ricerca
Start at attrs' attrs_init_subclass entry point and compare its class-definition handling with the PEP 487 init_subclass example in the issue. Reproduce the @attrs.define class Derived(Base, foo=12) case and verify whether keyword arguments reach Base.attrs_init_subclass; done means the example works without TypeError or the supported workaround is documented.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Hello, thanks for the great work!
My question/issue is related to __attrs_init_subclass__ that was recently added.
PEP 487 allows for passing arguments (not sure whether that's the proper name) at class definition.
The use case, for me, would be to have some sort of abstract class attributes that should be defined by concrete classes.
To make it short, reusing the example from the docs, this works
class Base:
@classmethod
def __init_subclass__(cls, foo):
cls.foo = foo
print(f"Base has been subclassed by attrs {cls} and foo={cls.foo}.")
class Derived(Base, foo=42):
pass
>>> Base has been subclassed by attrs <class '__main__.Derived'> and foo=42.
while
import attrs
class Base:
@classmethod
def __attrs_init_subclass__(cls, foo):
cls.foo = foo
print(f"Base has been subclassed by attrs {cls} and foo={cls.foo}.")
@attrs.define
class Derived(Base, foo=12):
pass
does not, throwing TypeError: Derived.__init_subclass__() takes no keyword arguments.
Would there be any workaround?
- 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
-
area: harness bug status: needs-triage
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
Human-Agent-Society/reef#625 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 80/100
learningequality/kolibri#15351 · 2 commenti ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
-
Name consistency Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
eellak/triplestore#65 · 1 commento ·