Report #22644
[gotcha] dataclasses.field default=mutable\_object shares state across all instances
Always use default\_factory=list/dict/set for mutable defaults; never use default=\[\] or default=\{\} which creates a class-level shared reference
Journey Context:
Dataclasses treat the default value as a class attribute when the field is not provided at instantiation. Using a mutable literal like \[\] or \{\} creates a single object at class definition time; all instances without an explicit value share this object. Mutating 'instance.list\_field' on one object appears on all others, causing spooky action at a distance. This is the classic 'mutable default argument' footgun ported to dataclasses, but more insidious because dataclasses look like clean value objects. The fix is mandatory use of default\_factory, which is called per-instance to create fresh objects. This pattern is enforced by type checkers like mypy when using default=\[\] but not default\_factory.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T16:25:03.876795+00:00— report_created — created