dataclass tries to generate `__init__()` even though the class already defines one
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Python
- Estrellas
- 77.2k
- Forks
- 35.9k
- Métricas de merge de PR
- Métricas de PR pendientes
Descripción
Bug report
Bug description:
When running this code:
from dataclasses import dataclass
@dataclass
class A:
a: int = 1
@dataclass
class B(A):
b: int
def __init__(self, *args, **kwargs): ...
I got TypeError: non-default argument 'b' follows default argument
However, if I set init=False for the second dataclass, like this:
from dataclasses import dataclass
@dataclass
class A:
a: int = 1
@dataclass(init=False)
class B(A):
b: int
def __init__(self, *args, **kwargs): ...
There error disappeared.
I think the error comes from the process where dataclass tries to generate the default __init__() and checks the fields. But, if I understand correctly, this should not happen when the class already defines its own __init__().
Although I'm not sure whether the behavior should be defined as a bug, I do find the results inconsistant with the docs, which says
If the class already defines
__init__(), this parameter (init) is ignored.
so, intuitively, adding init=False shouldn't have changed the result.
CPython versions tested on:
3.12, 3.13, 3.14
Operating systems tested on:
Windows
Linked PRs
- gh-151184
- gh-153601
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.
Línea de trabajo
Reproduce los dos ejemplos usando dataclasses.dataclass y, después, inspecciona el trabajo enlazado en gh-151184 y gh-153601 antes de decidir si el comportamiento o la documentación son incoherentes. Se considera terminado cuando se haya establecido el tratamiento previsto de un init existente y este esté cubierto por evidencia de regresión adecuada.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- python
- Área
- backend
- Tipo de issue
- Error
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 25/100