slots declared on attrs-decorated class are ignored/lost
I maintainer di solito rispondono entro 1 giorno
@anshurajbisoyi98-ctrl ci sta già lavorando.
Dal 3/10/2026.
- #1638 di @anshurajbisoyi98-ctrl — aperta
Valutazione
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Idoneità per principianti
- 68/100
- Tipo di issue
- Bug
- Chiarezza
- Abbastanza chiara
- Stato di attività
- Attiva
- Stack tecnologico
- python
- Ambito
- developer-experience
Direzione di ricerca
Start by reproducing the two examples in the issue and compare how attrs processes slots declared on the class with slots inherited from a base class. Trace the slot-processing entry point and add a regression test for preserving a class-declared private slot; done means assigning that slot works while it remains outside attrs fields and generated methods.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
If a slotted attrs class declares its own slots, those slots are lost/ignored when the class is processed by attrs. E.g.
@attrs.define
class A:
__slots__ = ("private",)
public: str = "foo"
a = A()
a.private = 1
produces the error TypeError: descriptor 'private' for 'A' objects doesn't apply to a 'A' object.
However, declaring an attrs class that subclasses a class that defines slots, the parent class's slots are preserved. E.g.
class Base:
__slots__ = ("private",)
@attrs.define
class B(Base):
public: str = "bar"
b = B()
b.private = 1
sets the slots attribute without issue.
Is this intentional behavior or dictated by the design of attrs? I've looked at the code and it looks like it goes out and gets slots defined on base classes and adds them to the slots defined on the new class. Using that same method, it doesn't look like it would be hard to preserve slots declared on the slotted attrs class itself, but I haven't tried to code it and I don't know if it would break something I'm not seeing. (Note that declaring __slots__ on a class decorated with @attrs.define(slotted=False) doesn't work for other reasons, so this question/issue really only applies to slotted attrs classes).
It can often be useful to have private attributes on a class that may be used for internal instance management but aren't really part of the data model represented by the class and should be ignored by attrs--not included in the init or hash or equality functions. This can, of course, be accomplished by declaring the attribute with init=False, hash=False, and eq=False, but the attribute still shows up in the collections returned by attrs.fields or attrs.fields_dict and is serialized by attrs.asdict (which, similarly, can be worked around with a filter). Declaring slots would be a simple way to allow an instance to have attributes that attrs is entirely unaware of. Using a base class is a workaround, but it would be preferable not to have to create a separate base class or mixin solely to add extra slots.
- Lingua principale
- Python
- Stelle
- 5.9k
- Fork
- 499
- Merge medio
- 3g 12h
- PR unite (30g)
- 3
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi 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
-
Docs: `attrs.filters` is missing stated examplesForse già presa @feiiiiii5 l’ha presa 11 giorni fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
python-attrs/attrs#1627 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 5/5 Più di una settimana Idoneità per principianti 20/100
python-attrs/attrs#1620 ·
I maintainer di solito rispondono entro 1 giorno
-
Incorrect annotation for forward reference in generated constructorForse già presa Una pull request collegata a questa issue è aperta o già unita. Aperta
Difficoltà 3/5 1-2 giorni Idoneità per principianti 55/100
python-attrs/attrs#1596 · 2 commenti · 1 reazione ·
I maintainer di solito rispondono entro 1 giorno
-
Decorated functions do not work with inheritanceForse già presa Una pull request collegata a questa issue è aperta o già unita. Aperta
Difficoltà 4/5 3-5 giorni Idoneità per principianti 35/100
python-attrs/attrs#1549 · 3 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 38/100
python-attrs/attrs#1543 · 2 commenti ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di python-attrs/attrs
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
I maintainer di solito rispondono entro 3 giorni
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
modelcontextprotocol/python-sdk#3648 ·
I maintainer di solito rispondono entro 1 giorno
-
docs good first issue
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
VenetoStato/giorgio#6 ·
-
Claiming namespace ddalusAperta
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 70/100
EclipseFdn/open-vsx.org#13831 ·
I maintainer di solito rispondono entro 1 giorno
-
feature request
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
I maintainer di solito rispondono entro 2 giorni