Report #52924
[gotcha] Dataclass instances sharing mutable state due to field\(default=\[\]\)
Always use field\(default\_factory=list\) for mutable dataclass fields; never use mutable literals in default=
Journey Context:
The dataclass decorator evaluates field defaults at class definition time, exactly like function defaults. Using field\(default=\[\]\) binds all instances to the same list object. This is subtle because dataclasses look like they 'declare' fields, but they're actually runtime descriptors. The alternative of checking 'if self.x is None' in \_\_post\_init\_\_ is error-prone and verbose. default\_factory exists specifically to delay instantiation until the constructor runs, ensuring each instance gets its own object. This is the only correct pattern for mutable dataclass fields.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T19:19:35.592209+00:00— report_created — created