Bases: object
Mixin for objects that can save and restore their internal state.
Provides versioned serialization with automatic state migration. Objects can be
pickled, saved to disk, transmitted over networks, or stored in databases, then
restored to their previous state. Version tracking ensures backward compatibility
when object structure changes.
Subclasses can override _migrate_state to handle state migrations between versions.
Increment _VERSION when making incompatible changes to object structure.
-
__getstate__() → Mapping[str, Any][source]
Serialize object state with version metadata.
- Return type:
Mapping[str, Any]
- Returns:
Versioned state dictionary containing version number, class name,
and the object’s internal state.
- Overrides:
object.__getstate__
-
__setstate__(state: Any) → None[source]
Restore object from serialized state.
Handles both versioned and legacy state formats. Automatically migrates
state from older versions using _migrate_state. Warns when loading
legacy objects or when current version is older than saved version.
- Parameters:
state (Any) – Serialized state, either VersionedState dict or legacy format.
- Return type:
None