Expression
Expression copied to clipboard
Cannot pickle any tagged_union
Bug Description
Pickling tagge_union type is not working because __dataclass_fields__ is set on the object's __dict__ instead of the class __dict__. Additionally, __dataclass_fields__ cannot be pickled automatically since it contains a mappingproxy.
Expected Behavior
The Option type should be pickled successfully without errors.
Code Example Below is a minimal code example to illustrate the issue:
import pickle
from expression import Some
obj = Some(1)
# Attempt to pickle the object
try:
pickled_obj = pickle.dumps(obj)
unpickled_obj = pickle.loads(pickled_obj)
print("Pickling and unpickling successful.")
except Exception as e:
print(f"Error during pickling: {e}")
Additional Context
- The issue arises because
__dataclass_fields__is set on the instance's__dict__rather than the class's__dict__. -
__dataclass_field__contains amappingproxy, which cannot be pickled automatically.
Environment
- Pydantic version: 5.0.2
Error Message If applicable, include the error message you encountered:
Error during pickling: can't pickle 'mappingproxy' object
Potential Solution
Implement __setstate__ and __getstate__