Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

Passing arguments to `__attrs_init_subclass__`

Aperta
#1,335 4 commenti 2 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
35/100
Tipo di issue
Funzionalità
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
python
Ambito
tooling

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

Feature

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

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di python-attrs/attrs

Tutte le issue di python-attrs/attrs

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.