Report #14579
[gotcha] pickle \_\_getstate\_\_ breaks object identity sharing on unpickle
If your object graph contains shared references \(e.g., two parents referencing the same child\), never return a newly constructed object from \_\_getstate\_\_. Return the instance's \_\_dict\_\_ directly, or use copyreg to register a custom reducer that preserves identity via a persistent ID mechanism.
Journey Context:
Pickle uses a memo dictionary to ensure that obj is obj after unpickling \(identity preservation\). However, if \_\_getstate\_\_ returns a new wrapper object or transforms the state, pickle stores that new object in the memo. When another reference to the same original object is pickled, it sees a different memo entry. On unpickle, you get two distinct objects instead of shared references. This corrupts graph structures \(ASTs, DOM trees\) where mutation assumes shared identity. The solution requires returning the raw state dict or using pickle.Pickler.persistent\_id to explicitly manage shared object identity.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T21:52:43.600552+00:00— report_created — created