-
Notifications
You must be signed in to change notification settings - Fork 6
/
test_serialization.py
executable file
·223 lines (171 loc) · 5.82 KB
/
test_serialization.py
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
#!/usr/bin/env python3
from dataclasses import dataclass
from typing import Union, NamedTuple
def test_dataclasses_json():
# pip install dataclasses-json
from dataclasses_json import dataclass_json
@dataclass
class Inner:
value: int
@dataclass
class Outer:
inner: Inner
### issue 1: requires @dataclass_json annotation on all involved dataclasses
obj = Outer(inner=Inner(value=123))
# we don't control the types that are passed to us, so we can't use the @dataclass_json
# but we can just call the decorator directly
# HOWEVER: this modifies the original class, Outer!!
OuterJson = dataclass_json(Outer)
# it adds 'from_dict', 'from_json', 'schema', 'to_dict', 'to_json' attributes to it
# now if you try
# print(OuterJson.schema().dump(obj))
# you get a warning that it wants you to add annotations to Inner classes too.
# this isn't really an option for us.
###
### issue 2: can't dump anything unless the top level type is a dataclass?
### could wrap into a dummy dataclass or something, but is wasteful in terms of performance
###
### nice thing: correctly serializes Union types, even if they share the same attributes
@dataclass_json
@dataclass
class City:
name: str
@dataclass_json
@dataclass
class Country:
name: str
@dataclass_json
@dataclass
class WithUnion:
union: Union[City | Country]
objs = [
WithUnion(union=City(name='London')),
WithUnion(union=Country(name='UK')),
]
schema = WithUnion.schema()
json = schema.dumps(objs, many=True)
objs2 = schema.loads(json, many=True)
print("objects ", objs)
print("json ", json)
# NOTE: it dumps [{"union": {"name": "London", "__type": "City"}}, {"union": {"name": "UK", "__type": "Country"}}]
# so types are correctly distinguished
print("restored ", objs2)
assert objs == objs2, (objs, objs2)
###
def test_marshmallow_dataclass():
# pip3 install --user marshmallow-dataclass[union]
import marshmallow_dataclass
### issue 1: the top level type has to be a dataclass?
### although possible that we could use regular marshmallow for that instead
###
### issue 2: doesn't handle unions correctly
@dataclass
class City:
name: str
@dataclass
class Country:
name: str
@dataclass
class WithUnion:
union: Union[City | Country]
objs = [
WithUnion(union=City(name="London")),
WithUnion(union=Country(name="UK")),
]
# NOTE: good, doesn't require adding annotations on the original classes
schema = marshmallow_dataclass.class_schema(WithUnion)()
json = schema.dumps(objs, many=True)
objs2 = schema.loads(json, many=True)
print("objects ", objs)
print("json ", json)
# NOTE: it dumps [{"union": {"value": 123}}, {"union": {"value": 123}}]
# so it doesn't distingush based on types => won't deserialize correctly
print("restored ", objs2)
# assert objs == objs2, (objs, objs2)
# ^ this assert fails!
###
def test_pydantic():
from pydantic import TypeAdapter
### issue: doesn't handle Unions correctly
@dataclass
class City:
name: str
@dataclass
class Country:
name: str
@dataclass
class WithUnion:
union: Union[City | Country]
objs = [
WithUnion(union=City(name="London")),
WithUnion(union=Country(name="UK")),
]
# NOTE: nice, doesn't require annotating the original classes with anything
Schema = TypeAdapter(list[WithUnion])
json = Schema.dump_python(
objs,
# round_rtip: Whether to output the serialized data in a way that is compatible with deserialization
# not sure, doesn't seem to impact anything..
round_trip=True,
)
objs2 = Schema.validate_python(json)
print("objects ", objs)
print("json ", json)
print("restored ", objs2)
#assert objs == objs2, (objs, objs2)
# ^ this assert fails!
# created an issue https://github.com/pydantic/pydantic/issues/7391
###
def test_cattrs():
from cattrs import Converter
from cattrs.strategies import configure_tagged_union
converter = Converter()
### issue: NamedTuples aren't unstructured? asked here https://github.com/python-attrs/cattrs/issues/425
class X(NamedTuple):
value: int
d = converter.unstructure(X(value=123), X)
# NOTE: this assert doesn't pass!
# assert isinstance(d, dict)
###
### good: handles Union correctly (although some extra configuring required)
@dataclass
class City:
name: str
@dataclass
class Country:
name: str
@dataclass
class WithUnion:
union: Union[City | Country]
objs = [
WithUnion(union=City(name="London")),
WithUnion(union=Country(name="UK")),
]
configure_tagged_union(
union=City | Country,
converter=converter,
)
# NOTE: nice -- doesn't require decorating original classes
json = converter.unstructure(objs, list[WithUnion])
assert isinstance(json, list)
objs2 = converter.structure(json, list[WithUnion])
print("objects ", objs)
# NOTE: dumps it as [{'union': {'name': 'London', '_type': 'City'}}, {'union': {'name': 'UK', '_type': 'Country'}}]
print("json ", json)
print("restored ", objs2)
assert objs == objs2, (objs, objs2)
###
### issue: unions of simple types aren't supported?
# see https://github.com/python-attrs/cattrs/issues/423
mixed: list[int | str] = [
123,
'Jakarta',
]
json = converter.unstructure(mixed, list[int | str])
# NOTE: this fails
# mixed2 = converter.structure(json , list[int | str])
###
test_dataclasses_json()
test_marshmallow_dataclass()
test_pydantic()
test_cattrs()