Skip to content

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

Closed

Description

@marco-neumann-by

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.

Activity

  1. erccarls commented on Dec 17, 2020

    @erccarls

    Is there any workaround for this?

  2. marco-neumann-by commented on Dec 17, 2020

    @marco-neumann-by
    ContributorAuthor

    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.

  3. LaurentMazare commented on Dec 23, 2020

    @LaurentMazare

    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
    
  4. marco-neumann-by commented on Jan 21, 2021

    @marco-neumann-by
    ContributorAuthor

    @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.

  5. jondequinor commented on Mar 25, 2021

    @jondequinor

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

  6. added a commit that references this issue on Mar 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions