Hacktoberfest 2026: los issues que los mantenedores marcaron para octubre, abiertos y aptos para principiantes. Explorar issues de Hacktoberfest

Initialization order of fields overridden in subclasses

Abierto
#707 9 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
5/5
Tiempo estimado
Más de una semana
Aptitud para principiantes
25/100
Tipo de issue
Nueva funcionalidad
Claridad
Bastante claro
Estado de actividad
Estancado
Stack tecnológico
python
Área
tooling

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

Typing

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".

Lenguaje dominante
Python
Estrellas
5.8k
Forks
480
Merge medio
2 h 15 min
PR fusionados (30 d)
2

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de python-attrs/attrs

Todos los issues de python-attrs/attrs

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.