-
-
Save shrekris-anyscale/3d7836a6e606dcc8272c1f22a5cf4ccf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pydantic import BaseModel | |
from pydantic_core._pydantic_core import SchemaSerializer | |
# This also works with vanilla pickle | |
import cloudpickle | |
# Storing these as globals as a workaround. | |
# We should store them as instance attributes. | |
schema_cache, config_cache = None, None | |
def new_init(self, schema, config): | |
global schema_cache, config_cache | |
schema_cache = schema | |
config_cache = config | |
SchemaSerializer.__init__ = new_init | |
def new_reduce(self): | |
global schema_cache, config_cache | |
return SchemaSerializer, (schema_cache, config_cache, ) | |
SchemaSerializer.__reduce__ = new_reduce | |
class SimpleModel(BaseModel): | |
val: int | |
sm = SimpleModel(val=5) | |
NewSimpleModel = cloudpickle.loads(cloudpickle.dumps(SimpleModel)) | |
new_sm = NewSimpleModel(val=5) | |
print(sm == new_sm) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment