1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
|
import dataclasses
import json
import platform
import sys
from typing import ClassVar
import pytest
from pydantic_core import SchemaSerializer, SchemaValidator, core_schema
on_pypy = platform.python_implementation() == 'PyPy'
# pypy doesn't seem to maintain order of `__dict__`
if on_pypy:
IsStrictDict = dict
else:
from dirty_equals import IsStrictDict
@dataclasses.dataclass
class Foo:
a: str
b: bytes
def test_dataclass():
schema = core_schema.dataclass_schema(
Foo,
core_schema.dataclass_args_schema(
'Foo',
[
core_schema.dataclass_field(name='a', schema=core_schema.str_schema()),
core_schema.dataclass_field(name='b', schema=core_schema.bytes_schema()),
],
),
['a', 'b'],
)
s = SchemaSerializer(schema)
assert s.to_python(Foo(a='hello', b=b'more')) == IsStrictDict(a='hello', b=b'more')
assert s.to_python(Foo(a='hello', b=b'more'), mode='json') == IsStrictDict(a='hello', b='more')
j = s.to_json(Foo(a='hello', b=b'more'))
if on_pypy:
assert json.loads(j) == {'a': 'hello', 'b': 'more'}
else:
assert j == b'{"a":"hello","b":"more"}'
assert s.to_python(Foo(a='hello', b=b'more'), exclude={'b'}) == IsStrictDict(a='hello')
assert s.to_json(Foo(a='hello', b=b'more'), include={'a'}) == b'{"a":"hello"}'
def test_serialization_exclude():
schema = core_schema.dataclass_schema(
Foo,
core_schema.dataclass_args_schema(
'Foo',
[
core_schema.dataclass_field(
name='a', schema=core_schema.str_schema(), serialization_exclude_if=lambda x: x == 'bye'
),
core_schema.dataclass_field(name='b', schema=core_schema.bytes_schema(), serialization_exclude=True),
],
),
['a', 'b'],
)
s = SchemaSerializer(schema)
assert s.to_python(Foo(a='hello', b=b'more')) == {'a': 'hello'}
assert s.to_python(Foo(a='hello', b=b'more'), mode='json') == {'a': 'hello'}
# a = 'bye' excludes it
assert s.to_python(Foo(a='bye', b=b'more'), mode='json') == {}
j = s.to_json(Foo(a='hello', b=b'more'))
if on_pypy:
assert json.loads(j) == {'a': 'hello'}
else:
assert j == b'{"a":"hello"}'
j = s.to_json(Foo(a='bye', b=b'more'))
if on_pypy:
assert json.loads(j) == {}
else:
assert j == b'{}'
def test_serialization_alias():
schema = core_schema.dataclass_schema(
Foo,
core_schema.dataclass_args_schema(
'Foo',
[
core_schema.dataclass_field(name='a', schema=core_schema.str_schema()),
core_schema.dataclass_field(name='b', schema=core_schema.bytes_schema(), serialization_alias='BAR'),
],
),
['a', 'b'],
)
s = SchemaSerializer(schema)
assert s.to_python(Foo(a='hello', b=b'more'), by_alias=True) == IsStrictDict(a='hello', BAR=b'more')
assert s.to_python(Foo(a='hello', b=b'more'), mode='json', by_alias=True) == IsStrictDict(a='hello', BAR='more')
j = s.to_json(Foo(a='hello', b=b'more'), by_alias=True)
if on_pypy:
assert json.loads(j) == {'a': 'hello', 'BAR': 'more'}
else:
assert j == b'{"a":"hello","BAR":"more"}'
def test_properties():
@dataclasses.dataclass
class FooProp:
a: str
b: bytes
@property
def c(self) -> str:
return f'{self.a} {self.b.decode()}'
schema = core_schema.dataclass_schema(
Foo,
core_schema.dataclass_args_schema(
'FooProp',
[
core_schema.dataclass_field(name='a', schema=core_schema.str_schema()),
core_schema.dataclass_field(name='b', schema=core_schema.bytes_schema()),
],
computed_fields=[core_schema.computed_field('c', core_schema.str_schema())],
),
['a', 'b'],
)
s = SchemaSerializer(schema)
assert s.to_python(FooProp(a='hello', b=b'more')) == IsStrictDict(a='hello', b=b'more', c='hello more')
assert s.to_python(FooProp(a='hello', b=b'more'), mode='json') == IsStrictDict(a='hello', b='more', c='hello more')
j = s.to_json(FooProp(a='hello', b=b'more'))
if on_pypy:
assert json.loads(j) == {'a': 'hello', 'b': 'more', 'c': 'hello more'}
else:
assert j == b'{"a":"hello","b":"more","c":"hello more"}'
assert s.to_python(FooProp(a='hello', b=b'more'), exclude={'b'}) == IsStrictDict(a='hello', c='hello more')
assert s.to_json(FooProp(a='hello', b=b'more'), include={'a'}) == b'{"a":"hello"}'
@pytest.mark.skipif(sys.version_info < (3, 10), reason='slots are only supported for dataclasses in Python > 3.10')
def test_slots_mixed():
@dataclasses.dataclass(slots=True)
class Model:
x: int
y: dataclasses.InitVar[str]
z: ClassVar[str] = 'z-classvar'
@dataclasses.dataclass
class SubModel(Model):
x2: int
y2: dataclasses.InitVar[str]
z2: ClassVar[str] = 'z2-classvar'
schema = core_schema.dataclass_schema(
SubModel,
core_schema.dataclass_args_schema(
'SubModel',
[
core_schema.dataclass_field(name='x', schema=core_schema.int_schema()),
core_schema.dataclass_field(name='y', init_only=True, schema=core_schema.str_schema()),
core_schema.dataclass_field(name='x2', schema=core_schema.int_schema()),
core_schema.dataclass_field(name='y2', init_only=True, schema=core_schema.str_schema()),
],
),
['x', 'x2'],
slots=True,
)
dc = SubModel(x=1, y='a', x2=2, y2='b')
assert dataclasses.asdict(dc) == {'x': 1, 'x2': 2}
s = SchemaSerializer(schema)
assert s.to_python(dc) == {'x': 1, 'x2': 2}
assert s.to_json(dc) == b'{"x":1,"x2":2}'
@pytest.mark.xfail(reason='dataclasses do not serialize extras')
def test_extra_custom_serializer():
@dataclasses.dataclass
class Model:
pass
schema = core_schema.dataclass_schema(
Model,
core_schema.dataclass_args_schema(
'Model',
[],
extra_behavior='allow',
# extras_schema=core_schema.any_schema(
# serialization=core_schema.plain_serializer_function_ser_schema(
# lambda v: v + ' bam!',
# )
# )
),
[],
)
s = SchemaSerializer(schema)
v = SchemaValidator(schema)
m = v.validate_python({'extra': 'extra'})
assert s.to_python(m) == {'extra': 'extra bam!'}
def test_dataclass_initvar_not_required_on_union_ser() -> None:
@dataclasses.dataclass
class Foo:
x: int
init_var: dataclasses.InitVar[int] = 1
@dataclasses.dataclass
class Bar:
x: int
schema = core_schema.union_schema(
[
core_schema.dataclass_schema(
Foo,
core_schema.dataclass_args_schema(
'Foo',
[
core_schema.dataclass_field(name='x', schema=core_schema.int_schema()),
core_schema.dataclass_field(
name='init_var',
init_only=True,
schema=core_schema.with_default_schema(core_schema.int_schema(), default=1),
),
],
),
['x'],
post_init=True,
),
core_schema.dataclass_schema(
Bar,
core_schema.dataclass_args_schema(
'Bar', [core_schema.dataclass_field(name='x', schema=core_schema.int_schema())]
),
['x'],
),
]
)
s = SchemaSerializer(schema)
assert s.to_python(Foo(x=1), warnings='error') == {'x': 1}
assert s.to_python(Foo(x=1, init_var=2), warnings='error') == {'x': 1}
@pytest.mark.parametrize(
'config,runtime,expected',
[
(True, True, {'my_alias': 'hello'}),
(True, False, {'my_field': 'hello'}),
(True, None, {'my_alias': 'hello'}),
(False, True, {'my_alias': 'hello'}),
(False, False, {'my_field': 'hello'}),
(False, None, {'my_field': 'hello'}),
(None, True, {'my_alias': 'hello'}),
(None, False, {'my_field': 'hello'}),
(None, None, {'my_field': 'hello'}),
],
)
def test_by_alias_and_name_config_interaction(config, runtime, expected) -> None:
"""This test reflects the priority that applies for config vs runtime serialization alias configuration.
If the runtime value (by_alias) is set, that value is used.
If the runtime value is unset, the config value (serialize_by_alias) is used.
If neither are set, the default, False, is used.
"""
@dataclasses.dataclass
class Foo:
my_field: str
schema = core_schema.dataclass_schema(
Foo,
core_schema.dataclass_args_schema(
'Foo',
[
core_schema.dataclass_field(
name='my_field', schema=core_schema.str_schema(), serialization_alias='my_alias'
),
],
),
['my_field'],
config=core_schema.CoreConfig(serialize_by_alias=config or False),
)
s = SchemaSerializer(schema)
assert s.to_python(Foo(my_field='hello'), by_alias=runtime) == expected
|