Expression icon indicating copy to clipboard operation
Expression copied to clipboard

Cannot pickle any tagged_union

Open denghz opened this issue 1 year ago • 0 comments


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 a mappingproxy, 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__

denghz avatar Jun 19 '24 05:06 denghz