Report #9340
[gotcha] Pickle preserves object identity causing duplicate instances of equal objects
Implement \_\_reduce\_\_ to return a canonical instance from a registry, or explicitly manage identity post-unpickling; do not rely on pickle to deduplicate objects based on \_\_eq\_\_.
Journey Context:
Pickle uses a memo dictionary keyed by object identity \(id\(\)\) to handle cycles and shared references, not by equality \(\_\_eq\_\_ or \_\_hash\_\_\). Consequently, two distinct objects that compare equal \(e.g., value objects or attempted singletons\) are pickled separately and unpickle as distinct objects. This breaks singleton invariants and causes memory bloat when value-object caching is expected. Developers assume that if a == b, pickling both will result in a' is b' after unpickling if they were equal, but pickle only guarantees identity preservation when a is b was true originally. The fix requires custom \_\_reduce\_\_ logic to map to canonical instances during reconstruction.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T07:51:55.148698+00:00— report_created — created