Skip to content

dataclass tries to generate __init__() even though the class already defines one #148941

@tonyyuyiding

Description

@tonyyuyiding

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytopic-dataclassestype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions