Report #12051
[gotcha] Mutable default arguments are shared across instances in dataclasses
Use \`dataclasses.field\(default\_factory=list\)\` \(or \`dict\`, \`set\`, etc.\) instead of \`default=\[\]\` for any mutable dataclass field; never use a literal mutable object as a default value.
Journey Context:
Dataclasses generate \`\_\_init\_\_\` that assigns default values directly. If the default is a mutable object like \`\[\]\`, it's evaluated once at class definition time and shared by all instances, causing silent cross-instance pollution \(e.g., appending to a list in one instance affects others\). This is identical to function default arguments but is often missed because dataclasses look like they define instance state. The \`field\(default\_factory=...\)\` ensures the factory callable is invoked for each new instance, creating independent objects.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T14:55:17.991081+00:00— report_created — created