-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathserialization.py
More file actions
347 lines (284 loc) · 12.7 KB
/
Copy pathserialization.py
File metadata and controls
347 lines (284 loc) · 12.7 KB
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# This file was auto-generated by Fern from our API Definition.
import collections
import inspect
import typing
import pydantic
import typing_extensions
class FieldMetadata:
"""
Metadata class used to annotate fields to provide additional information.
Example:
class MyDict(TypedDict):
field: typing.Annotated[str, FieldMetadata(alias="field_name")]
Will serialize: `{"field": "value"}`
To: `{"field_name": "value"}`
"""
alias: str
def __init__(self, *, alias: str) -> None:
self.alias = alias
# Resolving type hints (typing.get_type_hints) is expensive because it eval/compiles
# forward-reference annotations. The result is constant for a given type, so we cache it.
# This is critical for hot paths like SSE event parsing, where the same (often large
# discriminated-union) type is converted on every single event.
_type_hints_cache: typing.Dict[typing.Any, typing.Dict[str, typing.Any]] = {}
def _get_cached_type_hints(expected_type: typing.Any) -> typing.Dict[str, typing.Any]:
try:
cached = _type_hints_cache.get(expected_type)
except TypeError:
# Unhashable type; resolve without caching.
return _resolve_type_hints(expected_type)
if cached is None:
cached = _resolve_type_hints(expected_type)
_type_hints_cache[expected_type] = cached
return cached
def _resolve_type_hints(expected_type: typing.Any) -> typing.Dict[str, typing.Any]:
try:
return typing_extensions.get_type_hints(expected_type, include_extras=True)
except NameError:
# The type contains a circular reference, so we use the __annotations__ attribute directly.
return getattr(expected_type, "__annotations__", {})
# Whether convert_and_respect_annotation_metadata can possibly rewrite anything for a given
# annotation, i.e. whether any reachable model/TypedDict field carries a FieldMetadata alias.
# This is constant per type, so we cache it and use it to short-circuit the recursive walk.
_requires_conversion_cache: typing.Dict[typing.Any, bool] = {}
def _requires_conversion(type_: typing.Any) -> bool:
try:
cached = _requires_conversion_cache.get(type_)
except TypeError:
# Unhashable annotation; compute without caching.
return _compute_requires_conversion(type_, set())
if cached is None:
cached = _compute_requires_conversion(type_, set())
_requires_conversion_cache[type_] = cached
return cached
def _compute_requires_conversion(type_: typing.Any, seen: typing.Set[typing.Any]) -> bool:
clean_type = _remove_annotations(type_)
try:
if clean_type in seen:
return False
seen = seen | {clean_type}
except TypeError:
# Unhashable type; skip cycle tracking (the type graph is finite in practice).
pass
# Models / TypedDicts: a field alias here means we must dealias; otherwise recurse into fields.
if (inspect.isclass(clean_type) and issubclass(clean_type, pydantic.BaseModel)) or typing_extensions.is_typeddict(
clean_type
):
annotations = _get_cached_type_hints(clean_type)
if _get_alias_to_field_name(annotations):
return True
return any(_compute_requires_conversion(hint, seen) for hint in annotations.values())
# Containers / unions: recurse into the type arguments (List/Set/Sequence/Dict/Union/etc.).
return any(_compute_requires_conversion(arg, seen) for arg in typing_extensions.get_args(clean_type))
def convert_and_respect_annotation_metadata(
*,
object_: typing.Any,
annotation: typing.Any,
inner_type: typing.Optional[typing.Any] = None,
direction: typing.Literal["read", "write"],
) -> typing.Any:
"""
Respect the metadata annotations on a field, such as aliasing. This function effectively
manipulates the dict-form of an object to respect the metadata annotations. This is primarily used for
TypedDicts, which cannot support aliasing out of the box, and can be extended for additional
utilities, such as defaults.
Parameters
----------
object_ : typing.Any
annotation : type
The type we're looking to apply typing annotations from
inner_type : typing.Optional[type]
Returns
-------
typing.Any
"""
if object_ is None:
return None
if inner_type is None:
inner_type = annotation
# The only thing this function ever rewrites is keys that carry a FieldMetadata
# alias. If nothing in the (cached) type graph has such an alias, the conversion is
# a content-identity transform, so we can skip the entire recursive walk. This is
# the hot path for SSE streaming, where a large discriminated union would otherwise
# be traversed on every single event.
if not _requires_conversion(annotation):
return object_
clean_type = _remove_annotations(inner_type)
# Pydantic models
if (
inspect.isclass(clean_type)
and issubclass(clean_type, pydantic.BaseModel)
and isinstance(object_, typing.Mapping)
):
return _convert_mapping(object_, clean_type, direction)
# TypedDicts
if typing_extensions.is_typeddict(clean_type) and isinstance(object_, typing.Mapping):
return _convert_mapping(object_, clean_type, direction)
if (
typing_extensions.get_origin(clean_type) == typing.Dict
or typing_extensions.get_origin(clean_type) == dict
or clean_type == typing.Dict
) and isinstance(object_, typing.Dict):
key_type = typing_extensions.get_args(clean_type)[0]
value_type = typing_extensions.get_args(clean_type)[1]
return {
key: convert_and_respect_annotation_metadata(
object_=value,
annotation=annotation,
inner_type=value_type,
direction=direction,
)
for key, value in object_.items()
}
# If you're iterating on a string, do not bother to coerce it to a sequence.
if not isinstance(object_, str):
if (
typing_extensions.get_origin(clean_type) == typing.Set
or typing_extensions.get_origin(clean_type) == set
or clean_type == typing.Set
) and isinstance(object_, typing.Set):
inner_type = typing_extensions.get_args(clean_type)[0]
return {
convert_and_respect_annotation_metadata(
object_=item,
annotation=annotation,
inner_type=inner_type,
direction=direction,
)
for item in object_
}
elif (
(
typing_extensions.get_origin(clean_type) == typing.List
or typing_extensions.get_origin(clean_type) == list
or clean_type == typing.List
)
and isinstance(object_, typing.List)
) or (
(
typing_extensions.get_origin(clean_type) == typing.Sequence
or typing_extensions.get_origin(clean_type) == collections.abc.Sequence
or clean_type == typing.Sequence
)
and isinstance(object_, typing.Sequence)
):
inner_type = typing_extensions.get_args(clean_type)[0]
return [
convert_and_respect_annotation_metadata(
object_=item,
annotation=annotation,
inner_type=inner_type,
direction=direction,
)
for item in object_
]
if typing_extensions.get_origin(clean_type) == typing.Union:
# We should be able to ~relatively~ safely try to convert keys against all
# member types in the union, the edge case here is if one member aliases a field
# of the same name to a different name from another member
# Or if another member aliases a field of the same name that another member does not.
for member in typing_extensions.get_args(clean_type):
object_ = convert_and_respect_annotation_metadata(
object_=object_,
annotation=annotation,
inner_type=member,
direction=direction,
)
return object_
annotated_type = _get_annotation(annotation)
if annotated_type is None:
return object_
# If the object is not a TypedDict, a Union, or other container (list, set, sequence, etc.)
# Then we can safely call it on the recursive conversion.
return object_
def _convert_mapping(
object_: typing.Mapping[str, object],
expected_type: typing.Any,
direction: typing.Literal["read", "write"],
) -> typing.Mapping[str, object]:
converted_object: typing.Dict[str, object] = {}
annotations = _get_cached_type_hints(expected_type)
aliases_to_field_names = _get_alias_to_field_name(annotations)
for key, value in object_.items():
if direction == "read" and key in aliases_to_field_names:
dealiased_key = aliases_to_field_names.get(key)
if dealiased_key is not None:
type_ = annotations.get(dealiased_key)
else:
type_ = annotations.get(key)
# Note you can't get the annotation by the field name if you're in read mode, so you must check the aliases map
#
# So this is effectively saying if we're in write mode, and we don't have a type, or if we're in read mode and we don't have an alias
# then we can just pass the value through as is
if type_ is None:
converted_object[key] = value
elif direction == "read" and key not in aliases_to_field_names:
converted_object[key] = convert_and_respect_annotation_metadata(
object_=value, annotation=type_, direction=direction
)
else:
converted_object[_alias_key(key, type_, direction, aliases_to_field_names)] = (
convert_and_respect_annotation_metadata(object_=value, annotation=type_, direction=direction)
)
return converted_object
def _get_annotation(type_: typing.Any) -> typing.Optional[typing.Any]:
maybe_annotated_type = typing_extensions.get_origin(type_)
if maybe_annotated_type is None:
return None
if maybe_annotated_type == typing_extensions.NotRequired:
type_ = typing_extensions.get_args(type_)[0]
maybe_annotated_type = typing_extensions.get_origin(type_)
if maybe_annotated_type == typing_extensions.Annotated:
return type_
return None
def _remove_annotations(type_: typing.Any) -> typing.Any:
maybe_annotated_type = typing_extensions.get_origin(type_)
if maybe_annotated_type is None:
return type_
if maybe_annotated_type == typing_extensions.NotRequired:
return _remove_annotations(typing_extensions.get_args(type_)[0])
if maybe_annotated_type == typing_extensions.Annotated:
return _remove_annotations(typing_extensions.get_args(type_)[0])
return type_
def get_alias_to_field_mapping(type_: typing.Any) -> typing.Dict[str, str]:
annotations = _get_cached_type_hints(type_)
return _get_alias_to_field_name(annotations)
def get_field_to_alias_mapping(type_: typing.Any) -> typing.Dict[str, str]:
annotations = _get_cached_type_hints(type_)
return _get_field_to_alias_name(annotations)
def _get_alias_to_field_name(
field_to_hint: typing.Dict[str, typing.Any],
) -> typing.Dict[str, str]:
aliases = {}
for field, hint in field_to_hint.items():
maybe_alias = _get_alias_from_type(hint)
if maybe_alias is not None:
aliases[maybe_alias] = field
return aliases
def _get_field_to_alias_name(
field_to_hint: typing.Dict[str, typing.Any],
) -> typing.Dict[str, str]:
aliases = {}
for field, hint in field_to_hint.items():
maybe_alias = _get_alias_from_type(hint)
if maybe_alias is not None:
aliases[field] = maybe_alias
return aliases
def _get_alias_from_type(type_: typing.Any) -> typing.Optional[str]:
maybe_annotated_type = _get_annotation(type_)
if maybe_annotated_type is not None:
# The actual annotations are 1 onward, the first is the annotated type
annotations = typing_extensions.get_args(maybe_annotated_type)[1:]
for annotation in annotations:
if isinstance(annotation, FieldMetadata) and annotation.alias is not None:
return annotation.alias
return None
def _alias_key(
key: str,
type_: typing.Any,
direction: typing.Literal["read", "write"],
aliases_to_field_names: typing.Dict[str, str],
) -> str:
if direction == "read":
return aliases_to_field_names.get(key, key)
return _get_alias_from_type(type_=type_) or key