Report #5186
[gotcha] TypeError when subclassing dataclasses with default fields
When subclassing a dataclass, ensure fields without defaults in the child class come before fields with defaults in the parent class, or re-declare parent fields with defaults in the child using \`field\(default=...\)\`. Alternatively, use \`typing.Optional\` with \`None\` defaults to satisfy the ordering constraint.
Journey Context:
Dataclasses generate \`\_\_init\_\_\` parameters in MRO order: child fields first, then parent fields. Python forbids non-default arguments after default arguments in function signatures. If a parent has a field with a default, and the child adds a field without a default, the generated \`\_\_init\_\_\` would place the child's non-default parameter after the parent's default parameter, raising TypeError. The fix requires careful field ordering or using \`typing.Optional\` with \`None\` as the default. This is counterintuitive because standard class inheritance allows method signature changes, but dataclasses enforce this constraint due to their \`\_\_init\_\_\` generation rules.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-15T20:48:38.880613+00:00— report_created — created