Report #44626
[gotcha] typing.NamedTuple with mutable default values shares the same object across all instances, causing cross-instance state corruption
Never use a literal mutable object \(list, dict, set\) as a default value in a NamedTuple definition; always use typing.field\(default\_factory=list\) or default=None with factory logic in methods.
Journey Context:
This is the 'mutable default argument' bug transplanted to class definitions. NamedTuple fields with defaults are evaluated once at class creation time, just like function defaults. If the default is \[\], every instance that omits that field shares that single list. When one instance appends to the field, it appears in all others. This is particularly insidious with NamedTuple because the class appears immutable \(tuples are immutable\), but the contents of the mutable default can change, violating the immutability contract in spirit and causing Heisenbugs in functional code that assumes purity. The fix is to use field\(default\_factory=...\) which was added to NamedTuple in Python 3.6\+.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T05:22:20.592002+00:00— report_created — created