-
Notifications
You must be signed in to change notification settings - Fork 262
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
Add SchemaSerializer.__reduce__
method to enable pickle
serialization
#1006
Changes from 11 commits
a1c6164
ce155b8
3159084
cdc32a2
bba95d8
24ad5b0
bdafefd
8c41c88
70f3032
8b8bfd5
661ac07
3e9b372
9ef470b
26cb67a
ed3ad18
e11ba19
6cbba7b
1d17408
722bbdf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,7 +27,9 @@ class BaseModel: | |
__schema__: SchemaSerializer | ||
|
||
def __init_subclass__(cls) -> None: | ||
cls.__schema__ = SchemaSerializer(core_schema.model_schema(cls, GC_TEST_SCHEMA_INNER)) | ||
cls.__schema__ = SchemaSerializer( | ||
core_schema.model_schema(cls, GC_TEST_SCHEMA_INNER), config={'ser_json_timedelta': 'float'} | ||
) | ||
Comment on lines
+30
to
+32
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Curious why this was necessary? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wasn't necessary, I just wanted to ensure that there was coverage of the garbage collection path when a config was passed given that I'm holding a reference to it now (existing tests didn't take a config). |
||
|
||
cache: 'WeakValueDictionary[int, Any]' = WeakValueDictionary() | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import json | ||
import pickle | ||
from datetime import timedelta | ||
|
||
import pytest | ||
|
||
from pydantic_core import core_schema | ||
from pydantic_core._pydantic_core import SchemaSerializer | ||
|
||
|
||
def repr_function(value, _info): | ||
return repr(value) | ||
|
||
|
||
def test_basic_schema_serializer(): | ||
s = SchemaSerializer(core_schema.dict_schema()) | ||
s = pickle.loads(pickle.dumps(s)) | ||
assert s.to_python({'a': 1, b'b': 2, 33: 3}) == {'a': 1, b'b': 2, 33: 3} | ||
assert s.to_python({'a': 1, b'b': 2, 33: 3, True: 4}, mode='json') == {'a': 1, 'b': 2, '33': 3, 'true': 4} | ||
assert s.to_json({'a': 1, b'b': 2, 33: 3, True: 4}) == b'{"a":1,"b":2,"33":3,"true":4}' | ||
|
||
assert s.to_python({(1, 2): 3}) == {(1, 2): 3} | ||
assert s.to_python({(1, 2): 3}, mode='json') == {'1,2': 3} | ||
assert s.to_json({(1, 2): 3}) == b'{"1,2":3}' | ||
|
||
|
||
@pytest.mark.parametrize( | ||
'value,expected_python,expected_json', | ||
[(None, 'None', b'"None"'), (1, '1', b'"1"'), ([1, 2, 3], '[1, 2, 3]', b'"[1, 2, 3]"')], | ||
) | ||
def test_schema_serializer_capturing_function(value, expected_python, expected_json): | ||
# Test a SchemaSerializer that captures a function. | ||
s = SchemaSerializer( | ||
core_schema.any_schema( | ||
serialization=core_schema.plain_serializer_function_ser_schema(repr_function, info_arg=True) | ||
) | ||
) | ||
s = pickle.loads(pickle.dumps(s)) | ||
assert s.to_python(value) == expected_python | ||
assert s.to_json(value) == expected_json | ||
assert s.to_python(value, mode='json') == json.loads(expected_json) | ||
|
||
|
||
def test_schema_serializer_containing_config(): | ||
s = SchemaSerializer(core_schema.timedelta_schema(), config={'ser_json_timedelta': 'float'}) | ||
s = pickle.loads(pickle.dumps(s)) | ||
|
||
assert s.to_python(timedelta(seconds=4, microseconds=500_000)) == timedelta(seconds=4, microseconds=500_000) | ||
assert s.to_python(timedelta(seconds=4, microseconds=500_000), mode='json') == 4.5 | ||
assert s.to_json(timedelta(seconds=4, microseconds=500_000)) == b'4.5' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's my take:
You'll need to update the
#[pyclass]
definition above forSchemaSerializer
to include#[pyclass(frozen)]
.Justifications:
slf: &PyCell<Self>
is equivalent toself
Python objectslf.get()
gives read access to the Rust data as long as it'sfrozen
type(self)
rather than hard-codeSchemaSerializer
type in case we allow subclassing later.slf.get_type()
is a cleaner way to do it than trying to usegetattr
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense, thanks for the pointers, I'll update!