forked from temporalio/sdk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
converter.py
937 lines (792 loc) · 31.9 KB
/
converter.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
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
"""Base converter and implementations for data conversion."""
from __future__ import annotations
import collections
import collections.abc
import dataclasses
import inspect
import json
import sys
from abc import ABC, abstractmethod
from dataclasses import dataclass
from datetime import datetime
from enum import IntEnum
from typing import (
Any,
Callable,
Dict,
List,
Mapping,
Optional,
Sequence,
Tuple,
Type,
TypeVar,
Union,
get_type_hints,
)
import google.protobuf.json_format
import google.protobuf.message
import google.protobuf.symbol_database
from typing_extensions import Literal
import temporalio.api.common.v1
import temporalio.common
if sys.version_info < (3, 11):
# Python's datetime.fromisoformat doesn't support certain formats pre-3.11
from dateutil import parser
class PayloadConverter(ABC):
"""Base payload converter to/from multiple payloads/values."""
@abstractmethod
def to_payloads(
self, values: Sequence[Any]
) -> List[temporalio.api.common.v1.Payload]:
"""Encode values into payloads.
Args:
values: Values to be converted.
Returns:
Converted payloads. Note, this does not have to be the same number
as values given, but must be at least one and cannot be more than
was given.
Raises:
Exception: Any issue during conversion.
"""
raise NotImplementedError
@abstractmethod
def from_payloads(
self,
payloads: Sequence[temporalio.api.common.v1.Payload],
type_hints: Optional[List[Type]] = None,
) -> List[Any]:
"""Decode payloads into values.
Args:
payloads: Payloads to convert to Python values.
type_hints: Types that are expected if any. This may not have any
types if there are no annotations on the target. If this is
present, it must have the exact same length as payloads even if
the values are just "object".
Returns:
Collection of Python values. Note, this does not have to be the same
number as values given, but at least one must be present.
Raises:
Exception: Any issue during conversion.
"""
raise NotImplementedError
def to_payloads_wrapper(
self, values: Sequence[Any]
) -> temporalio.api.common.v1.Payloads:
""":py:meth:`to_payloads` for the
:py:class:`temporalio.api.common.v1.Payloads` wrapper.
"""
return temporalio.api.common.v1.Payloads(payloads=self.to_payloads(values))
def from_payloads_wrapper(
self, payloads: Optional[temporalio.api.common.v1.Payloads]
) -> List[Any]:
""":py:meth:`from_payloads` for the
:py:class:`temporalio.api.common.v1.Payloads` wrapper.
"""
if not payloads or not payloads.payloads:
return []
return self.from_payloads(payloads.payloads)
class EncodingPayloadConverter(ABC):
"""Base converter to/from single payload/value with a known encoding for use in CompositePayloadConverter."""
@property
@abstractmethod
def encoding(self) -> str:
"""Encoding for the payload this converter works with."""
raise NotImplementedError
@abstractmethod
def to_payload(self, value: Any) -> Optional[temporalio.api.common.v1.Payload]:
"""Encode a single value to a payload or None.
Args:
value: Value to be converted.
Returns:
Payload of the value or None if unable to convert.
Raises:
TypeError: Value is not the expected type.
ValueError: Value is of the expected type but otherwise incorrect.
RuntimeError: General error during encoding.
"""
raise NotImplementedError
@abstractmethod
def from_payload(
self,
payload: temporalio.api.common.v1.Payload,
type_hint: Optional[Type] = None,
) -> Any:
"""Decode a single payload to a Python value or raise exception.
Args:
payload: Payload to convert to Python value.
type_hint: Type that is expected if any. This may not have a type if
there are no annotations on the target.
Return:
The decoded value from the payload. Since the encoding is checked by
the caller, this should raise an exception if the payload cannot be
converted.
Raises:
RuntimeError: General error during decoding.
"""
raise NotImplementedError
class CompositePayloadConverter(PayloadConverter):
"""Composite payload converter that delegates to a list of encoding payload converters.
Encoding/decoding are attempted on each payload converter successively until
it succeeds.
Attributes:
converters: List of payload converters to delegate to, in order.
"""
converters: Mapping[bytes, EncodingPayloadConverter]
def __init__(self, *converters: EncodingPayloadConverter) -> None:
"""Initializes the data converter.
Args:
converters: Payload converters to delegate to, in order.
"""
# Insertion order preserved here since Python 3.7
self.converters = {c.encoding.encode(): c for c in converters}
def to_payloads(
self, values: Sequence[Any]
) -> List[temporalio.api.common.v1.Payload]:
"""Encode values trying each converter.
See base class. Always returns the same number of payloads as values.
Raises:
RuntimeError: No known converter
"""
payloads = []
for index, value in enumerate(values):
# We intentionally attempt these serially just in case a stateful
# converter may rely on the previous values
payload = None
for converter in self.converters.values():
payload = converter.to_payload(value)
if payload is not None:
break
if payload is None:
raise RuntimeError(
f"Value at index {index} of type {type(value)} has no known converter"
)
payloads.append(payload)
return payloads
def from_payloads(
self,
payloads: Sequence[temporalio.api.common.v1.Payload],
type_hints: Optional[List[Type]] = None,
) -> List[Any]:
"""Decode values trying each converter.
See base class. Always returns the same number of values as payloads.
Raises:
KeyError: Unknown payload encoding
RuntimeError: Error during decode
"""
values = []
for index, payload in enumerate(payloads):
encoding = payload.metadata.get("encoding", b"<unknown>")
converter = self.converters.get(encoding)
if converter is None:
raise KeyError(f"Unknown payload encoding {encoding.decode()}")
type_hint = None
if type_hints is not None:
type_hint = type_hints[index]
try:
values.append(converter.from_payload(payload, type_hint))
except RuntimeError as err:
raise RuntimeError(
f"Payload at index {index} with encoding {encoding.decode()} could not be converted"
) from err
return values
class DefaultPayloadConverter(CompositePayloadConverter):
"""Default payload converter compatible with other Temporal SDKs.
This handles None, bytes, all protobuf message types, and any type that
:py:func:`json.dump` accepts. In addition, this supports encoding
:py:mod:`dataclasses` and also decoding them provided the data class is in
the type hint.
"""
def __init__(self) -> None:
"""Create a default payload converter."""
super().__init__(
BinaryNullPayloadConverter(),
BinaryPlainPayloadConverter(),
JSONProtoPayloadConverter(),
BinaryProtoPayloadConverter(),
JSONPlainPayloadConverter(),
)
class BinaryNullPayloadConverter(EncodingPayloadConverter):
"""Converter for 'binary/null' payloads supporting None values."""
@property
def encoding(self) -> str:
"""See base class."""
return "binary/null"
def to_payload(self, value: Any) -> Optional[temporalio.api.common.v1.Payload]:
"""See base class."""
if value is None:
return temporalio.api.common.v1.Payload(
metadata={"encoding": self.encoding.encode()}
)
return None
def from_payload(
self,
payload: temporalio.api.common.v1.Payload,
type_hint: Optional[Type] = None,
) -> Any:
"""See base class."""
if len(payload.data) > 0:
raise RuntimeError("Expected empty data set for binary/null")
return None
class BinaryPlainPayloadConverter(EncodingPayloadConverter):
"""Converter for 'binary/plain' payloads supporting bytes values."""
@property
def encoding(self) -> str:
"""See base class."""
return "binary/plain"
def to_payload(self, value: Any) -> Optional[temporalio.api.common.v1.Payload]:
"""See base class."""
if isinstance(value, bytes):
return temporalio.api.common.v1.Payload(
metadata={"encoding": self.encoding.encode()}, data=value
)
return None
def from_payload(
self,
payload: temporalio.api.common.v1.Payload,
type_hint: Optional[Type] = None,
) -> Any:
"""See base class."""
return payload.data
_sym_db = google.protobuf.symbol_database.Default()
class JSONProtoPayloadConverter(EncodingPayloadConverter):
"""Converter for 'json/protobuf' payloads supporting protobuf Message values."""
@property
def encoding(self) -> str:
"""See base class."""
return "json/protobuf"
def to_payload(self, value: Any) -> Optional[temporalio.api.common.v1.Payload]:
"""See base class."""
if (
isinstance(value, google.protobuf.message.Message)
and value.DESCRIPTOR is not None
):
# We have to convert to dict then to JSON because MessageToJson does
# not have a compact option removing spaces and newlines
json_str = json.dumps(
google.protobuf.json_format.MessageToDict(value),
separators=(",", ":"),
sort_keys=True,
)
return temporalio.api.common.v1.Payload(
metadata={
"encoding": self.encoding.encode(),
"messageType": value.DESCRIPTOR.full_name.encode(),
},
data=json_str.encode(),
)
return None
def from_payload(
self,
payload: temporalio.api.common.v1.Payload,
type_hint: Optional[Type] = None,
) -> Any:
"""See base class."""
message_type = payload.metadata.get("messageType", b"<unknown>").decode()
try:
value = _sym_db.GetSymbol(message_type)()
return google.protobuf.json_format.Parse(payload.data, value)
except KeyError as err:
raise RuntimeError(f"Unknown Protobuf type {message_type}") from err
except google.protobuf.json_format.ParseError as err:
raise RuntimeError("Failed parsing") from err
class BinaryProtoPayloadConverter(EncodingPayloadConverter):
"""Converter for 'binary/protobuf' payloads supporting protobuf Message values."""
@property
def encoding(self) -> str:
"""See base class."""
return "binary/protobuf"
def to_payload(self, value: Any) -> Optional[temporalio.api.common.v1.Payload]:
"""See base class."""
if (
isinstance(value, google.protobuf.message.Message)
and value.DESCRIPTOR is not None
):
return temporalio.api.common.v1.Payload(
metadata={
"encoding": self.encoding.encode(),
"messageType": value.DESCRIPTOR.full_name.encode(),
},
data=value.SerializeToString(),
)
return None
def from_payload(
self,
payload: temporalio.api.common.v1.Payload,
type_hint: Optional[Type] = None,
) -> Any:
"""See base class."""
message_type = payload.metadata.get("messageType", b"<unknown>").decode()
try:
value = _sym_db.GetSymbol(message_type)()
value.ParseFromString(payload.data)
return value
except KeyError as err:
raise RuntimeError(f"Unknown Protobuf type {message_type}") from err
except google.protobuf.message.DecodeError as err:
raise RuntimeError("Failed parsing") from err
class AdvancedJSONEncoder(json.JSONEncoder):
"""Advanced JSON encoder.
This encoder supports dataclasses, classes with dict() functions, and
all iterables as lists.
"""
def default(self, o: Any) -> Any:
"""Override JSON encoding default.
See :py:meth:`json.JSONEncoder.default`.
"""
# Dataclass support
if dataclasses.is_dataclass(o):
return dataclasses.asdict(o)
# Support for models with "dict" function like Pydantic
dict_fn = getattr(o, "dict", None)
if callable(dict_fn):
return dict_fn()
# Support for non-list iterables like set
if not isinstance(o, list) and isinstance(o, collections.abc.Iterable):
return list(o)
return super().default(o)
class JSONPlainPayloadConverter(EncodingPayloadConverter):
"""Converter for 'json/plain' payloads supporting common Python values.
For encoding, this supports all values that :py:func:`json.dump` supports
and by default adds extra encoding support for dataclasses, classes with
``dict()`` methods, and all iterables.
For decoding, this uses type hints to attempt to rebuild the type from the
type hint.
"""
_encoder: Optional[Type[json.JSONEncoder]]
_decoder: Optional[Type[json.JSONDecoder]]
_encoding: str
def __init__(
self,
*,
encoder: Optional[Type[json.JSONEncoder]] = AdvancedJSONEncoder,
decoder: Optional[Type[json.JSONDecoder]] = None,
encoding: str = "json/plain",
) -> None:
"""Initialize a JSON data converter.
Args:
encoder: Custom encoder class object to use.
decoder: Custom decoder class object to use.
encoding: Encoding name to use.
"""
super().__init__()
self._encoder = encoder
self._decoder = decoder
self._encoding = encoding
@property
def encoding(self) -> str:
"""See base class."""
return self._encoding
def to_payload(self, value: Any) -> Optional[temporalio.api.common.v1.Payload]:
"""See base class."""
# We let JSON conversion errors be thrown to caller
return temporalio.api.common.v1.Payload(
metadata={"encoding": self._encoding.encode()},
data=json.dumps(
value, cls=self._encoder, separators=(",", ":"), sort_keys=True
).encode(),
)
def from_payload(
self,
payload: temporalio.api.common.v1.Payload,
type_hint: Optional[Type] = None,
) -> Any:
"""See base class."""
try:
obj = json.loads(payload.data, cls=self._decoder)
if type_hint:
obj = value_to_type(type_hint, obj)
return obj
except json.JSONDecodeError as err:
raise RuntimeError("Failed parsing") from err
class PayloadCodec(ABC):
"""Codec for encoding/decoding to/from bytes.
Commonly used for compression or encryption.
"""
@abstractmethod
async def encode(
self, payloads: Sequence[temporalio.api.common.v1.Payload]
) -> List[temporalio.api.common.v1.Payload]:
"""Encode the given payloads.
Args:
payloads: Payloads to encode. This value should not be mutated.
Returns:
Encoded payloads. Note, this does not have to be the same number as
payloads given, but must be at least one and cannot be more than was
given.
"""
raise NotImplementedError
@abstractmethod
async def decode(
self, payloads: Sequence[temporalio.api.common.v1.Payload]
) -> List[temporalio.api.common.v1.Payload]:
"""Decode the given payloads.
Args:
payloads: Payloads to decode. This value should not be mutated.
Returns:
Decoded payloads. Note, this does not have to be the same number as
payloads given, but must be at least one and cannot be more than was
given.
"""
raise NotImplementedError
async def encode_wrapper(self, payloads: temporalio.api.common.v1.Payloads) -> None:
""":py:meth:`encode` for the
:py:class:`temporalio.api.common.v1.Payloads` wrapper.
This replaces the payloads within the wrapper.
"""
new_payloads = await self.encode(payloads.payloads)
del payloads.payloads[:]
# TODO(cretz): Copy too expensive?
payloads.payloads.extend(new_payloads)
async def decode_wrapper(self, payloads: temporalio.api.common.v1.Payloads) -> None:
""":py:meth:`decode` for the
:py:class:`temporalio.api.common.v1.Payloads` wrapper.
This replaces the payloads within.
"""
new_payloads = await self.decode(payloads.payloads)
del payloads.payloads[:]
# TODO(cretz): Copy too expensive?
payloads.payloads.extend(new_payloads)
@dataclass(frozen=True)
class DataConverter:
"""Data converter for converting and encoding payloads to/from Python values.
This combines :py:class:`PayloadConverter` which converts values with
:py:class:`PayloadCodec` which encodes bytes.
"""
payload_converter_class: Type[PayloadConverter] = DefaultPayloadConverter
"""Class to instantiate for payload conversion."""
payload_codec: Optional[PayloadCodec] = None
"""Optional codec for encoding payload bytes."""
payload_converter: PayloadConverter = dataclasses.field(init=False)
def __post_init__(self) -> None: # noqa: D105
object.__setattr__(self, "payload_converter", self.payload_converter_class())
async def encode(
self, values: Sequence[Any]
) -> List[temporalio.api.common.v1.Payload]:
"""Encode values into payloads.
First converts values to payloads then encodes payloads using codec.
Args:
values: Values to be converted and encoded.
Returns:
Converted and encoded payloads. Note, this does not have to be the
same number as values given, but must be at least one and cannot be
more than was given.
"""
payloads = self.payload_converter.to_payloads(values)
if self.payload_codec:
payloads = await self.payload_codec.encode(payloads)
return payloads
async def decode(
self,
payloads: Sequence[temporalio.api.common.v1.Payload],
type_hints: Optional[List[Type]] = None,
) -> List[Any]:
"""Decode payloads into values.
First decodes payloads using codec then converts payloads to values.
Args:
payloads: Payloads to be decoded and converted.
Returns:
Decoded and converted values.
"""
if self.payload_codec:
payloads = await self.payload_codec.decode(payloads)
return self.payload_converter.from_payloads(payloads, type_hints)
async def encode_wrapper(
self, values: Sequence[Any]
) -> temporalio.api.common.v1.Payloads:
""":py:meth:`encode` for the
:py:class:`temporalio.api.common.v1.Payloads` wrapper.
"""
return temporalio.api.common.v1.Payloads(payloads=(await self.encode(values)))
async def decode_wrapper(
self,
payloads: Optional[temporalio.api.common.v1.Payloads],
type_hints: Optional[List[Type]] = None,
) -> List[Any]:
""":py:meth:`decode` for the
:py:class:`temporalio.api.common.v1.Payloads` wrapper.
"""
if not payloads or not payloads.payloads:
return []
return await self.decode(payloads.payloads, type_hints)
_default: Optional[DataConverter] = None
def default() -> DataConverter:
"""Default data converter."""
global _default
if not _default:
_default = DataConverter()
return _default
def encode_search_attributes(
attributes: temporalio.common.SearchAttributes,
api: temporalio.api.common.v1.SearchAttributes,
) -> None:
"""Convert search attributes into an API message.
Args:
attributes: Search attributes to convert.
api: API message to set converted attributes on.
"""
for k, v in attributes.items():
api.indexed_fields[k].CopyFrom(encode_search_attribute_values(v))
def encode_search_attribute_values(
vals: temporalio.common.SearchAttributeValues,
) -> temporalio.api.common.v1.Payload:
"""Convert search attribute values into a payload.
Args:
vals: List of values to convert.
"""
if not isinstance(vals, list):
raise TypeError("Search attribute values must be lists")
# Confirm all types are the same
val_type: Optional[Type] = None
# Convert dates to strings
safe_vals = []
for v in vals:
if isinstance(v, datetime):
if v.tzinfo is None:
raise ValueError(
"Timezone must be present on all search attribute dates"
)
v = v.isoformat()
elif not isinstance(v, (str, int, float, bool)):
raise TypeError(
f"Search attribute value of type {type(v).__name__} not one of str, int, float, bool, or datetime"
)
elif val_type and type(v) is not val_type:
raise TypeError(
f"Search attribute values must have the same type for the same key"
)
elif not val_type:
val_type = type(v)
safe_vals.append(v)
return default().payload_converter.to_payloads([safe_vals])[0]
def _get_iso_datetime_parser() -> Callable[[str], datetime]:
"""Isolates system version check and returns relevant datetime passer
Returns:
A callable to parse date strings into datetimes.
"""
if sys.version_info >= (3, 11):
return datetime.fromisoformat # noqa
else:
# Isolate import for py > 3.11, as dependency only installed for < 3.11
return parser.isoparse
def decode_search_attributes(
api: temporalio.api.common.v1.SearchAttributes,
) -> temporalio.common.SearchAttributes:
"""Decode API search attributes to values.
Args:
api: API message with search attribute values to convert.
Returns:
Converted search attribute values.
"""
conv = default().payload_converter
ret = {}
for k, v in api.indexed_fields.items():
val = conv.from_payloads([v])[0]
# If a value did not come back as a list, make it a single-item list
if not isinstance(val, list):
val = [val]
# Convert each item to datetime if necessary
if v.metadata.get("type") == b"Datetime":
parser = _get_iso_datetime_parser()
val = [parser(v) for v in val]
ret[k] = val
return ret
def value_to_type(hint: Type, value: Any) -> Any:
"""Convert a given value to the given type hint.
This is used internally to convert a raw JSON loaded value to a specific
type hint.
Args:
hint: Type hint to convert the value to.
value: Raw value (e.g. primitive, dict, or list) to convert from.
Returns:
Converted value.
Raises:
TypeError: Unable to convert to the given hint.
"""
# Any or primitives
if hint is Any:
return value
elif hint is int or hint is float:
if not isinstance(value, (int, float)):
raise TypeError(f"Expected value to be int|float, was {type(value)}")
return hint(value)
elif hint is bool:
if not isinstance(value, bool):
raise TypeError(f"Expected value to be bool, was {type(value)}")
return bool(value)
elif hint is str:
if not isinstance(value, str):
raise TypeError(f"Expected value to be str, was {type(value)}")
return str(value)
elif hint is bytes:
if not isinstance(value, (str, bytes, list)):
raise TypeError(f"Expected value to be bytes, was {type(value)}")
# In some other SDKs, this is serialized as a base64 string, but in
# Python this is a numeric array.
return bytes(value) # type: ignore
elif hint is type(None):
if value is not None:
raise TypeError(f"Expected None, got value of type {type(value)}")
return None
# NewType. Note we cannot simply check isinstance NewType here because it's
# only been a class since 3.10. Instead we'll just check for the presence
# of a supertype.
supertype = getattr(hint, "__supertype__", None)
if supertype:
return value_to_type(supertype, value)
# Load origin for other checks
origin = getattr(hint, "__origin__", hint)
type_args: Tuple = getattr(hint, "__args__", ())
# Literal
if origin is Literal:
if value not in type_args:
raise TypeError(f"Value {value} not in literal values {type_args}")
return value
# Union
if origin is Union:
# Try each one. Note, Optional is just a union w/ none.
for arg in type_args:
try:
return value_to_type(arg, value)
except Exception:
pass
raise TypeError(f"Failed converting to {hint} from {value}")
# Mapping
if inspect.isclass(origin) and issubclass(origin, collections.abc.Mapping):
if not isinstance(value, collections.abc.Mapping):
raise TypeError(f"Expected {hint}, value was {type(value)}")
ret_dict = {}
# If there are required or optional keys that means we are a TypedDict
# and therefore can extract per-key types
per_key_types: Optional[Dict[str, Type]] = None
if getattr(origin, "__required_keys__", None) or getattr(
origin, "__optional_keys__", None
):
per_key_types = get_type_hints(origin)
key_type = (
type_args[0]
if len(type_args) > 0
and type_args[0] is not Any
and not isinstance(type_args[0], TypeVar)
else None
)
value_type = (
type_args[1]
if len(type_args) > 1
and type_args[1] is not Any
and not isinstance(type_args[1], TypeVar)
else None
)
# Convert each key/value
for key, value in value.items():
if key_type:
try:
key = value_to_type(key_type, key)
except Exception as err:
raise TypeError(f"Failed converting key {key} on {hint}") from err
# If there are per-key types, use it instead of single type
this_value_type = value_type
if per_key_types:
# TODO(cretz): Strict mode would fail an unknown key
this_value_type = per_key_types.get(key)
if this_value_type:
try:
value = value_to_type(this_value_type, value)
except Exception as err:
raise TypeError(
f"Failed converting value for key {key} on {hint}"
) from err
ret_dict[key] = value
# If there are per-key types, it's a typed dict and we want to attempt
# instantiation to get its validation
if per_key_types:
ret_dict = hint(**ret_dict)
return ret_dict
# Dataclass
if dataclasses.is_dataclass(hint):
if not isinstance(value, dict):
raise TypeError(
f"Cannot convert to dataclass {hint}, value is {type(value)} not dict"
)
# Obtain dataclass fields and check that all dict fields are there and
# that no required fields are missing. Unknown fields are silently
# ignored.
fields = dataclasses.fields(hint)
field_hints = get_type_hints(hint)
field_values = {}
for field in fields:
field_value = value.get(field.name, dataclasses.MISSING)
# We do not check whether field is required here. Rather, we let the
# attempted instantiation of the dataclass raise if a field is
# missing
if field_value is not dataclasses.MISSING:
try:
field_values[field.name] = value_to_type(
field_hints[field.name], field_value
)
except Exception as err:
raise TypeError(
f"Failed converting field {field.name} on dataclass {hint}"
) from err
# Simply instantiate the dataclass. This will fail as expected when
# missing required fields.
# TODO(cretz): Want way to convert snake case to camel case?
return hint(**field_values)
# If there is a @staticmethod or @classmethod parse_obj, we will use it.
# This covers Pydantic models.
parse_obj_attr = inspect.getattr_static(hint, "parse_obj", None)
if isinstance(parse_obj_attr, classmethod) or isinstance(
parse_obj_attr, staticmethod
):
if not isinstance(value, dict):
raise TypeError(
f"Cannot convert to {hint}, value is {type(value)} not dict"
)
return getattr(hint, "parse_obj")(value)
# IntEnum
if inspect.isclass(hint) and issubclass(hint, IntEnum):
if not isinstance(value, int):
raise TypeError(
f"Cannot convert to enum {hint}, value not an integer, value is {type(value)}"
)
return hint(value)
# Iterable. We intentionally put this last as it catches several others.
if inspect.isclass(origin) and issubclass(origin, collections.abc.Iterable):
if not isinstance(value, collections.abc.Iterable):
raise TypeError(f"Expected {hint}, value was {type(value)}")
ret_list = []
# If there is no type arg, just return value as is
if not type_args or (
len(type_args) == 1
and (isinstance(type_args[0], TypeVar) or type_args[0] is Ellipsis)
):
ret_list = list(value)
else:
# Otherwise convert
for i, item in enumerate(value):
# Non-tuples use first type arg, tuples use arg set or one
# before ellipsis if that's set
if origin is not tuple:
arg_type = type_args[0]
elif len(type_args) > i and type_args[i] is not Ellipsis:
arg_type = type_args[i]
elif type_args[-1] is Ellipsis:
# Ellipsis means use the second to last one
arg_type = type_args[-2]
else:
raise TypeError(
f"Type {hint} only expecting {len(type_args)} values, got at least {i + 1}"
)
try:
ret_list.append(value_to_type(arg_type, item))
except Exception as err:
raise TypeError(f"Failed converting {hint} index {i}") from err
# If tuple, set, or deque convert back to that type
if origin is tuple:
return tuple(ret_list)
elif origin is set:
return set(ret_list)
elif origin is collections.deque:
return collections.deque(ret_list)
return ret_list
raise TypeError(f"Unserializable type during conversion: {hint}")