slots declared on attrs-decorated class are ignored/lost
Los mantenedores suelen responder en 1 día
@anshurajbisoyi98-ctrl ya está trabajando en esto.
Desde el 3/10/2026.
- #1638 de @anshurajbisoyi98-ctrl — abierto
Evaluación
- Dificultad
- 3/5
- Tiempo estimado
- 1-2 días
- Aptitud para principiantes
- 68/100
- Tipo de issue
- Error
- Claridad
- Bastante claro
- Estado de actividad
- Activo
- Stack tecnológico
- python
- Área
- developer-experience
Línea de trabajo
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.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
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.
- Lenguaje dominante
- Python
- Estrellas
- 5.9k
- Forks
- 499
- Merge medio
- 3 d 12 h
- PR fusionados (30 d)
- 3
Preparar el entorno
- Sin Dockerfile ni archivo de Docker Compose
- Tiene una plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de python-attrs/attrs
-
Docs: `attrs.filters` is missing stated examplesPosiblemente ocupada @feiiiiii5 la tomó hace 11 días. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
python-attrs/attrs#1627 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 5/5 Más de una semana Aptitud para principiantes 20/100
python-attrs/attrs#1620 ·
Los mantenedores suelen responder en 1 día
-
Incorrect annotation for forward reference in generated constructorPosiblemente ocupada @MohammedAnasNathani la tomó hace 68 días. Abierto
Dificultad 3/5 1-2 días Aptitud para principiantes 55/100
python-attrs/attrs#1596 · 2 comentarios · 1 reacción ·
Los mantenedores suelen responder en 1 día
-
Decorated functions do not work with inheritancePosiblemente ocupada @sb123sb123 la tomó hace 14 días. Abierto
Dificultad 4/5 3-5 días Aptitud para principiantes 35/100
python-attrs/attrs#1549 · 3 comentarios ·
Los mantenedores suelen responder en 1 día
-
Dificultad 4/5 3-5 días Aptitud para principiantes 38/100
python-attrs/attrs#1543 · 2 comentarios ·
Los mantenedores suelen responder en 1 día
Todos los issues de python-attrs/attrs
Issues similares
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
Juniper/ansible-junos-stdlib#904 ·
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 82/100
pollen-robotics/reachy_mini#1457 ·
Los mantenedores suelen responder en 1 día
-
area:runtime good first issue
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
WATonomous/wato_f1tenth#39 ·
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 82/100
FireDynamics/fdsreader#123 ·
-
Dificultad 1/5 Menos de una hora Aptitud para principiantes 85/100