Pickle Fallback Strategy

The pickle fallback strategy uses predefined hooks that utilize __getstate__ for serialization and a custom implemented __setstate__ if available or otherwise pickle’s default strategy for restoring an object’s state. Both slotted and non-slotted classes are supported.

For more information about stateful objects and the pickle serialization interface, see also how to handle stateful objects.

Caution

Pickle’s default restoration strategy for dict classes just updates an object’s __dict__ attribute. There is no stable way to check which attributes a dict class expects like in slotted classes, as a newly created object has an empty __dict__. A dict class can therefore be restored with any dict[str, Any] without errors, but the resulting class object might be broken with missing or extra __dict__ fields. The serializer thus has no way to ensure a healthy object when restoring dict classes using the pickle fallback strategy. We recommend using attrs classes, dataclasses or pure slotted classes instead.