Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot pickle any tagged_union #208

Open
denghz opened this issue Jun 19, 2024 · 0 comments
Open

Cannot pickle any tagged_union #208

denghz opened this issue Jun 19, 2024 · 0 comments

Comments

@denghz
Copy link
Contributor

denghz commented Jun 19, 2024


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__

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant