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

pydantic BaseModel cannot be pickled w/ cloudpickle >= 1.3 #403

Closed
marco-neumann-by opened this issue Dec 3, 2020 · 5 comments · Fixed by #409
Closed

pydantic BaseModel cannot be pickled w/ cloudpickle >= 1.3 #403

marco-neumann-by opened this issue Dec 3, 2020 · 5 comments · Fixed by #409

Comments

@marco-neumann-by
Copy link
Contributor

Example

import cloudpickle
import pydantic

class Bar(pydantic.BaseModel):
    pass

if __name__ == '__main__':
    cloudpickle.loads(cloudpickle.dumps(Bar()))

Technical Background

The issue is the interaction between:

https://github.com/samuelcolvin/pydantic/blob/de0657e4a5495de9378db9fe15a17334c2b0fae5/pydantic/main.py#L865-L868

and:

is_litteral = getattr(obj, '__values__', None) is not None

Refs

This was originally discussed in #393.

@erccarls
Copy link

Is there any workaround for this?

@marco-neumann-by
Copy link
Contributor Author

I should note that this is only an issue on Python < 3.7. So upgrading is one workaround. Or a PR to cloudpickle that makes the linked inspection code more robust.

@LaurentMazare
Copy link

I'm actually running into an issue that seems related with Python 3.8.5 (cloudpickle version 1.6.0, pydantic version 1.7.3).
The example given at the top of this issue works fine however, adding a field to the model makes cloudpickle break whereas the standard pickle module works fine.

Below is some code that reproduces the issue.

import cloudpickle
import pydantic
import pickle

class Bar(pydantic.BaseModel):
    a: int

pickle.loads(pickle.dumps(Bar(a=1))) # This works well
cloudpickle.loads(cloudpickle.dumps(Bar(a=1))) # This fails with the error below
PicklingError: Can't pickle <cyfunction int_validator at 0x7ff32fa23ad0>: attribute lookup lambda12 on pydantic.validators failed

@marco-neumann-by
Copy link
Contributor Author

@LaurentMazare I've tried to reproduce your issue and first couldn't under Python 3.8. After some digging I've figured out that the difference between your and my environment must be the exact pydantic "flavor" (cythonized vs not cythonized). I think what you reported here is a different issue with a different root cause. So I've opened #408 with all the details and welcome you to continue the discussion there.

@jondequinor
Copy link

Will this be included in a release? If so, is there an ETA for that release?

jondequinor added a commit to jondequinor/ert that referenced this issue Mar 25, 2021
Also bypass cloudpipe/cloudpickle#403 by
not storing the Record (which subclasses pydantic.BaseModel) on the
transmitter, splitting it up into (data, index).
jondequinor added a commit to equinor/ert that referenced this issue Mar 25, 2021
Also bypass cloudpipe/cloudpickle#403 by
not storing the Record (which subclasses pydantic.BaseModel) on the
transmitter, splitting it up into (data, index).
sondreso pushed a commit to equinor/ert that referenced this issue Mar 25, 2021
Also bypass cloudpipe/cloudpickle#403 by
not storing the Record (which subclasses pydantic.BaseModel) on the
transmitter, splitting it up into (data, index).
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

Successfully merging a pull request may close this issue.

4 participants