/
unpack.py
824 lines (759 loc) · 29.8 KB
/
unpack.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
import collections
import collections.abc
import datetime
import enum
import ipaddress
import os
import pathlib
import types
import typing
import uuid
from base64 import decodebytes
from contextlib import suppress
from decimal import Decimal
from fractions import Fraction
from typing import Any, Callable, Iterable, List, Optional, Tuple, Type, Union
import typing_extensions
from mashumaro.core.const import PY_39_MIN, PY_311_MIN
from mashumaro.core.helpers import parse_timezone
from mashumaro.core.meta.code.lines import CodeLines
from mashumaro.core.meta.helpers import (
get_args,
get_class_that_defines_method,
get_function_arg_annotation,
get_literal_values,
is_dataclass_dict_mixin_subclass,
is_literal,
is_named_tuple,
is_new_type,
is_not_required,
is_optional,
is_required,
is_self,
is_special_typing_primitive,
is_type_var,
is_type_var_any,
is_type_var_tuple,
is_typed_dict,
is_union,
is_unpack,
not_none_type_arg,
resolve_type_params,
type_name,
)
from mashumaro.core.meta.types.common import (
Expression,
NoneType,
Registry,
ValueSpec,
ensure_generic_collection,
ensure_generic_collection_subclass,
ensure_generic_mapping,
expr_or_maybe_none,
)
from mashumaro.exceptions import (
ThirdPartyModuleNotFoundError,
UnserializableDataError,
UnserializableField,
UnsupportedDeserializationEngine,
)
from mashumaro.helper import pass_through
from mashumaro.types import (
GenericSerializableType,
SerializableType,
SerializationStrategy,
)
if PY_39_MIN:
import zoneinfo
try:
import ciso8601
except ImportError: # pragma no cover
ciso8601: Optional[types.ModuleType] = None # type: ignore
try:
import pendulum
except ImportError: # pragma no cover
pendulum: Optional[types.ModuleType] = None # type: ignore
__all__ = ["UnpackerRegistry"]
UnpackerRegistry = Registry()
register = UnpackerRegistry.register
def get_overridden_deserialization_method(
spec: ValueSpec,
) -> Optional[Union[Callable, str]]:
deserialize_option = spec.field_ctx.metadata.get("deserialize")
if deserialize_option is not None:
return deserialize_option
for strategy in spec.builder.iter_serialization_strategies(
spec.field_ctx.metadata, spec.type
):
if strategy is pass_through:
return pass_through
elif isinstance(strategy, dict):
deserialize_option = strategy.get("deserialize")
elif isinstance(strategy, SerializationStrategy):
deserialize_option = strategy.deserialize
if deserialize_option is not None:
return deserialize_option
@register
def unpack_type_with_overridden_deserialization(
spec: ValueSpec,
) -> Optional[Expression]:
deserialization_method = get_overridden_deserialization_method(spec)
if deserialization_method is pass_through:
return spec.expression
elif callable(deserialization_method):
overridden_fn = (
f"__{spec.field_ctx.name}_deserialize_{uuid.uuid4().hex}"
)
setattr(spec.builder.cls, overridden_fn, deserialization_method)
return f"cls.{overridden_fn}({spec.expression})"
def _unpack_annotated_serializable_type(
spec: ValueSpec,
) -> Optional[Expression]:
try:
# noinspection PyProtectedMember
# noinspection PyUnresolvedReferences
value_type = get_function_arg_annotation(
spec.origin_type._deserialize,
arg_pos=0,
)
except (KeyError, ValueError):
raise UnserializableField(
field_name=spec.field_ctx.name,
field_type=spec.type,
holder_class=spec.builder.cls,
msg='Method _deserialize must have annotated "value" argument',
) from None
if is_self(value_type):
return f"{type_name(spec.type)}._deserialize({spec.expression})"
type_args = get_args(value_type)
resolved = resolve_type_params(spec.origin_type, get_args(spec.type))[
spec.origin_type
]
new_type_args = []
for type_arg in type_args:
new_type_args.append(resolved.get(type_arg, type_arg))
with suppress(TypeError):
# noinspection PyUnresolvedReferences
value_type = value_type[tuple(new_type_args)]
unpacker = UnpackerRegistry.get(spec.copy(type=value_type))
return f"{type_name(spec.type)}._deserialize({unpacker})"
@register
def unpack_serializable_type(spec: ValueSpec) -> Optional[Expression]:
try:
if not issubclass(spec.origin_type, SerializableType):
return None
except TypeError:
return None
if spec.origin_type.__use_annotations__:
return _unpack_annotated_serializable_type(spec)
else:
return f"{type_name(spec.type)}._deserialize({spec.expression})"
@register
def unpack_generic_serializable_type(spec: ValueSpec) -> Optional[Expression]:
with suppress(TypeError):
if issubclass(spec.origin_type, GenericSerializableType):
type_arg_names = ", ".join(
list(map(type_name, get_args(spec.type)))
)
return (
f"{type_name(spec.type)}._deserialize({spec.expression}, "
f"[{type_arg_names}])"
)
@register
def unpack_dataclass_dict_mixin_subclass(
spec: ValueSpec,
) -> Optional[Expression]:
if is_dataclass_dict_mixin_subclass(spec.origin_type):
type_args = get_args(spec.type)
method_name = spec.builder.get_unpack_method_name(
type_args, spec.builder.format_name
)
if get_class_that_defines_method(
method_name, spec.origin_type
) != spec.origin_type and (
spec.origin_type != spec.builder.cls
or spec.builder.get_unpack_method_name(
type_args=type_args,
format_name=spec.builder.format_name,
decoder=spec.builder.decoder,
)
!= method_name
):
builder = spec.builder.__class__(
spec.origin_type,
type_args,
dialect=spec.builder.dialect,
format_name=spec.builder.format_name,
default_dialect=spec.builder.default_dialect,
)
builder.add_unpack_method()
method_args = ", ".join(
filter(
None,
(
spec.expression,
spec.builder.get_unpack_method_flags(spec.type),
),
)
)
return f"{type_name(spec.origin_type)}.{method_name}({method_args})"
@register
def unpack_any(spec: ValueSpec) -> Optional[Expression]:
if spec.type is Any:
return spec.expression
def unpack_union(
spec: ValueSpec, args: Tuple[Type, ...], prefix: str = "union"
) -> Expression:
lines = CodeLines()
method_name = (
f"__unpack_{prefix}_{spec.builder.cls.__name__}_"
f"{spec.field_ctx.name}__{str(uuid.uuid4().hex)}"
)
default_kwargs = spec.builder.get_unpack_method_default_flag_values()
lines.append("@classmethod")
if default_kwargs:
lines.append(f"def {method_name}(cls, value, {default_kwargs}):")
else:
lines.append(f"def {method_name}(cls, value):")
with lines.indent():
for unpacker in (
UnpackerRegistry.get(spec.copy(type=type_arg, expression="value"))
for type_arg in args
):
lines.append("try:")
with lines.indent():
lines.append(f"return {unpacker}")
lines.append("except:")
with lines.indent():
lines.append("pass")
field_type = type_name(
spec.type,
resolved_type_params=spec.builder.get_field_resolved_type_params(
spec.field_ctx.name
),
)
lines.append(
f"raise InvalidFieldValue('{spec.field_ctx.name}',{field_type},"
f"value,cls)"
)
lines.append(f"setattr(cls, '{method_name}', {method_name})")
if spec.builder.get_config().debug:
print(f"{type_name(spec.builder.cls)}:")
print(lines.as_text())
exec(lines.as_text(), spec.builder.globals, spec.builder.__dict__)
method_args = ", ".join(
filter(None, (spec.expression, spec.builder.get_unpack_method_flags()))
)
return f"cls.{method_name}({method_args})"
def unpack_literal(spec: ValueSpec) -> Expression:
spec.builder.add_type_modules(spec.type)
lines = CodeLines()
method_name = (
f"__unpack_literal_{spec.builder.cls.__name__}_{spec.field_ctx.name}__"
f"{str(uuid.uuid4().hex)}"
)
default_kwargs = spec.builder.get_unpack_method_default_flag_values()
lines.append("@classmethod")
if default_kwargs:
lines.append(f"def {method_name}(cls, value, {default_kwargs}):")
else:
lines.append(f"def {method_name}(cls, value):")
with lines.indent():
for literal_value in get_literal_values(spec.type):
if isinstance(literal_value, enum.Enum):
enum_type_name = type_name(type(literal_value))
lines.append(
f"if value == {enum_type_name}.{literal_value.name}.value:"
)
with lines.indent():
lines.append(
f"return {enum_type_name}.{literal_value.name}"
)
elif isinstance(literal_value, bytes):
unpacker = UnpackerRegistry.get(
spec.copy(type=bytes, expression="value")
)
lines.append("try:")
with lines.indent():
lines.append(f"if {unpacker} == {literal_value!r}:")
with lines.indent():
lines.append(f"return {literal_value!r}")
lines.append("except:")
with lines.indent():
lines.append("pass")
elif isinstance( # type: ignore
literal_value,
(int, str, bool, NoneType), # type: ignore
):
lines.append(f"if value == {literal_value!r}:")
with lines.indent():
lines.append(f"return {literal_value!r}")
lines.append(f"raise ValueError({spec.expression})")
lines.append(f"setattr(cls, '{method_name}', {method_name})")
if spec.builder.get_config().debug:
print(f"{type_name(spec.builder.cls)}:")
print(lines.as_text())
exec(lines.as_text(), spec.builder.globals, spec.builder.__dict__)
method_args = ", ".join(
filter(None, (spec.expression, spec.builder.get_unpack_method_flags()))
)
return f"cls.{method_name}({method_args})"
@register
def unpack_special_typing_primitive(spec: ValueSpec) -> Optional[Expression]:
if is_special_typing_primitive(spec.origin_type):
if is_union(spec.type):
resolved_type_params = spec.builder.get_field_resolved_type_params(
spec.field_ctx.name
)
if is_optional(spec.type, resolved_type_params):
arg = not_none_type_arg(
get_args(spec.type), resolved_type_params
)
uv = UnpackerRegistry.get(spec.copy(type=arg))
return expr_or_maybe_none(spec, uv)
else:
return unpack_union(spec, get_args(spec.type))
elif spec.origin_type is typing.AnyStr:
raise UnserializableDataError(
"AnyStr is not supported by mashumaro"
)
elif is_type_var_any(spec.type):
return spec.expression
elif is_type_var(spec.type):
constraints = getattr(spec.type, "__constraints__")
if constraints:
return unpack_union(spec, constraints, "type_var")
else:
bound = getattr(spec.type, "__bound__")
# act as if it was Optional[bound]
uv = UnpackerRegistry.get(spec.copy(type=bound))
return expr_or_maybe_none(spec, uv)
elif is_new_type(spec.type):
return UnpackerRegistry.get(
spec.copy(type=spec.type.__supertype__)
)
elif is_literal(spec.type):
return unpack_literal(spec)
elif is_self(spec.type):
method_name = spec.builder.get_unpack_method_name(
format_name=spec.builder.format_name
)
if (
get_class_that_defines_method(method_name, spec.builder.cls)
!= spec.builder.cls
# not hasattr(spec.builder.cls, method_name)
and spec.builder.get_unpack_method_name(
format_name=spec.builder.format_name,
decoder=spec.builder.decoder,
)
!= method_name
):
builder = spec.builder.__class__(
spec.builder.cls,
dialect=spec.builder.dialect,
format_name=spec.builder.format_name,
default_dialect=spec.builder.default_dialect,
)
builder.add_unpack_method()
method_args = ", ".join(
filter(
None,
(
spec.expression,
spec.builder.get_unpack_method_flags(spec.builder.cls),
),
)
)
spec.builder.add_type_modules(spec.builder.cls)
return (
f"{type_name(spec.builder.cls)}.{method_name}({method_args})"
)
elif is_required(spec.type) or is_not_required(spec.type):
return UnpackerRegistry.get(spec.copy(type=get_args(spec.type)[0]))
elif is_unpack(spec.type):
unpacker = UnpackerRegistry.get(
spec.copy(type=get_args(spec.type)[0])
)
return f"*{unpacker}"
elif is_type_var_tuple(spec.type):
return UnpackerRegistry.get(spec.copy(type=Tuple[Any, ...]))
else:
raise UnserializableDataError(
f"{spec.type} as a field type is not supported by mashumaro"
)
@register
def unpack_number(spec: ValueSpec) -> Optional[Expression]:
if spec.origin_type in (int, float):
return f"{type_name(spec.origin_type)}({spec.expression})"
@register
def unpack_bool_and_none(spec: ValueSpec) -> Optional[Expression]:
if spec.origin_type in (bool, NoneType, None):
return spec.expression
@register
def unpack_date_objects(spec: ValueSpec) -> Optional[Expression]:
if spec.origin_type in (datetime.datetime, datetime.date, datetime.time):
deserialize_option = get_overridden_deserialization_method(spec)
if deserialize_option is not None:
if deserialize_option == "ciso8601":
if ciso8601:
spec.builder.ensure_module_imported(ciso8601)
datetime_parser = "ciso8601.parse_datetime"
else:
raise ThirdPartyModuleNotFoundError(
"ciso8601", spec.field_ctx.name, spec.builder.cls
) # pragma no cover
elif deserialize_option == "pendulum":
if pendulum:
spec.builder.ensure_module_imported(pendulum)
datetime_parser = "pendulum.parse"
else:
raise ThirdPartyModuleNotFoundError(
"pendulum", spec.field_ctx.name, spec.builder.cls
) # pragma no cover
else:
raise UnsupportedDeserializationEngine(
spec.field_ctx.name,
spec.type,
spec.builder.cls,
deserialize_option,
)
suffix = ""
if spec.origin_type is datetime.date:
suffix = ".date()"
elif spec.origin_type is datetime.time:
suffix = ".time()"
return f"{datetime_parser}({spec.expression}){suffix}"
method = f"__datetime_{spec.origin_type.__name__}_fromisoformat"
spec.builder.ensure_object_imported(
getattr(datetime, spec.origin_type.__name__).fromisoformat,
method,
)
return f"{method}({spec.expression})"
@register
def unpack_timedelta(spec: ValueSpec) -> Optional[Expression]:
if spec.origin_type is datetime.timedelta:
method = "__datetime_timedelta"
spec.builder.ensure_object_imported(datetime.timedelta, method)
return f"{method}(seconds={spec.expression})"
@register
def unpack_timezone(spec: ValueSpec) -> Optional[Expression]:
if spec.origin_type is datetime.timezone:
spec.builder.ensure_object_imported(parse_timezone)
return f"parse_timezone({spec.expression})"
@register
def unpack_zone_info(spec: ValueSpec) -> Optional[Expression]:
if PY_39_MIN and spec.origin_type is zoneinfo.ZoneInfo:
method = "__zoneinfo_ZoneInfo"
spec.builder.ensure_object_imported(zoneinfo.ZoneInfo, method)
return f"{method}({spec.expression})"
@register
def unpack_uuid(spec: ValueSpec) -> Optional[Expression]:
if spec.origin_type is uuid.UUID:
method = "__uuid_UUID"
spec.builder.ensure_object_imported(uuid.UUID, method)
return f"{method}({spec.expression})"
@register
def unpack_ipaddress(spec: ValueSpec) -> Optional[Expression]:
if spec.origin_type in (
ipaddress.IPv4Address,
ipaddress.IPv6Address,
ipaddress.IPv4Network,
ipaddress.IPv6Network,
ipaddress.IPv4Interface,
ipaddress.IPv6Interface,
):
method = f"__ipaddress_{spec.origin_type.__name__}"
spec.builder.ensure_object_imported(spec.origin_type, method)
return f"{method}({spec.expression})"
@register
def unpack_decimal(spec: ValueSpec) -> Optional[Expression]:
if spec.origin_type is Decimal:
spec.builder.ensure_object_imported(Decimal)
return f"Decimal({spec.expression})"
@register
def unpack_fraction(spec: ValueSpec) -> Optional[Expression]:
if spec.origin_type is Fraction:
spec.builder.ensure_object_imported(Fraction)
return f"Fraction({spec.expression})"
def unpack_tuple(spec: ValueSpec, args: Tuple[Type, ...]) -> Expression:
if not args:
if spec.type in (Tuple, tuple):
args = [typing.Any, ...] # type: ignore
else:
return "()"
elif len(args) == 1 and args[0] == ():
if not PY_311_MIN:
return "()"
if len(args) == 2 and args[1] is Ellipsis:
unpacker = UnpackerRegistry.get(
spec.copy(type=args[0], expression="value", could_be_none=True)
)
return f"tuple([{unpacker} for value in {spec.expression}])"
else:
arg_indexes: List[Union[int, Tuple[int, Union[int, None]]]] = []
unpack_idx: Optional[int] = None
for arg_idx, type_arg in enumerate(args):
if is_unpack(type_arg):
if unpack_idx is not None:
raise TypeError(
"Multiple unpacks are disallowed within a single type "
f"parameter list for {type_name(spec.type)}"
)
unpack_idx = arg_idx
if len(args) == 1:
arg_indexes.append((arg_idx, None))
elif arg_idx < len(args) - 1:
arg_indexes.append((arg_idx, arg_idx + 1 - len(args)))
else:
arg_indexes.append((arg_idx, None))
else:
if unpack_idx is None:
arg_indexes.append(arg_idx)
else:
arg_indexes.append(arg_idx - len(args))
unpackers: List[Expression] = []
for _idx, _arg_idx in enumerate(arg_indexes):
if isinstance(_arg_idx, tuple):
u_expr = f"{spec.expression}[{_arg_idx[0]}:{_arg_idx[1]}]"
else:
u_expr = f"{spec.expression}[{_arg_idx}]"
unpacker = UnpackerRegistry.get(
spec.copy(
type=args[_idx],
expression=u_expr,
could_be_none=True,
)
)
if unpacker != "*()": # workaround for empty tuples
unpackers.append(unpacker)
return f"tuple([{', '.join(unpackers)}])"
def unpack_named_tuple(spec: ValueSpec) -> Expression:
resolved = resolve_type_params(spec.origin_type, get_args(spec.type))[
spec.origin_type
]
annotations = {
k: resolved.get(v, v)
for k, v in getattr(spec.origin_type, "__annotations__", {}).items()
}
fields = getattr(spec.type, "_fields", ())
defaults = getattr(spec.type, "_field_defaults", {})
unpackers = []
as_dict = spec.builder.get_config().namedtuple_as_dict
deserialize_option = get_overridden_deserialization_method(spec)
if deserialize_option is not None:
if deserialize_option == "as_dict":
as_dict = True
elif deserialize_option == "as_list":
as_dict = False
else:
raise UnsupportedDeserializationEngine(
field_name=spec.field_ctx.name,
field_type=spec.type,
holder_class=spec.builder.cls,
engine=deserialize_option,
)
field_indices: Iterable[Any]
if as_dict:
field_indices = zip((f"'{name}'" for name in fields), fields)
else:
field_indices = enumerate(fields)
if not defaults:
packed_value = spec.expression
else:
packed_value = "value"
for idx, field in field_indices:
unpacker = UnpackerRegistry.get(
spec.copy(
type=annotations.get(field, Any),
expression=f"{packed_value}[{idx}]",
could_be_none=True,
)
)
unpackers.append(unpacker)
if not defaults:
return f"{type_name(spec.type)}({', '.join(unpackers)})"
lines = CodeLines()
method_name = (
f"__unpack_named_tuple_{spec.builder.cls.__name__}_"
f"{spec.field_ctx.name}__{str(uuid.uuid4().hex)}"
)
lines.append("@classmethod")
default_kwargs = spec.builder.get_unpack_method_default_flag_values()
if default_kwargs:
lines.append(f"def {method_name}(cls, value, {default_kwargs}):")
else:
lines.append(f"def {method_name}(cls, value):")
with lines.indent():
lines.append("fields = []")
lines.append("try:")
with lines.indent():
for unpacker in unpackers:
lines.append(f"fields.append({unpacker})")
lines.append("except IndexError:")
with lines.indent():
lines.append("pass")
lines.append(f"return {type_name(spec.type)}(*fields)")
lines.append(f"setattr(cls, '{method_name}', {method_name})")
if spec.builder.get_config().debug:
print(f"{type_name(spec.builder.cls)}:")
print(lines.as_text())
exec(lines.as_text(), spec.builder.globals, spec.builder.__dict__)
method_args = ", ".join(
filter(None, (spec.expression, spec.builder.get_unpack_method_flags()))
)
return f"cls.{method_name}({method_args})"
def unpack_typed_dict(spec: ValueSpec) -> Expression:
resolved = resolve_type_params(spec.origin_type, get_args(spec.type))[
spec.origin_type
]
annotations = {
k: resolved.get(v, v)
for k, v in spec.origin_type.__annotations__.items()
}
all_keys = list(annotations.keys())
required_keys = getattr(spec.type, "__required_keys__", all_keys)
optional_keys = getattr(spec.type, "__optional_keys__", [])
lines = CodeLines()
method_name = (
f"__unpack_typed_dict_{spec.builder.cls.__name__}_"
f"{spec.field_ctx.name}__{str(uuid.uuid4().hex)}"
)
default_kwargs = spec.builder.get_unpack_method_default_flag_values()
lines.append("@classmethod")
if default_kwargs:
lines.append(f"def {method_name}(cls, value, {default_kwargs}):")
else:
lines.append(f"def {method_name}(cls, value):")
with lines.indent():
lines.append("d = {}")
for key in sorted(required_keys, key=all_keys.index):
unpacker = UnpackerRegistry.get(
spec.copy(
type=annotations[key],
expression=f"value['{key}']",
could_be_none=True,
)
)
lines.append(f"d['{key}'] = {unpacker}")
for key in sorted(optional_keys, key=all_keys.index):
lines.append(f"key_value = value.get('{key}', MISSING)")
lines.append("if key_value is not MISSING:")
with lines.indent():
unpacker = UnpackerRegistry.get(
spec.copy(
type=annotations[key],
expression="key_value",
could_be_none=True,
)
)
lines.append(f"d['{key}'] = {unpacker}")
lines.append("return d")
lines.append(f"setattr(cls, '{method_name}', {method_name})")
if spec.builder.get_config().debug:
print(f"{type_name(spec.builder.cls)}:")
print(lines.as_text())
exec(lines.as_text(), spec.builder.globals, spec.builder.__dict__)
method_args = ", ".join(
filter(None, (spec.expression, spec.builder.get_unpack_method_flags()))
)
return f"cls.{method_name}({method_args})"
@register
def unpack_collection(spec: ValueSpec) -> Optional[Expression]:
if not issubclass(spec.origin_type, typing.Collection):
return None
elif issubclass(spec.origin_type, enum.Enum):
return None
args = get_args(spec.type)
def inner_expr(
arg_num: int = 0, v_name: str = "value", v_type: Optional[Type] = None
) -> Expression:
if v_type:
return UnpackerRegistry.get(
spec.copy(type=v_type, expression=v_name)
)
else:
if args and len(args) > arg_num:
type_arg: Any = args[arg_num]
else:
type_arg = Any
return UnpackerRegistry.get(
spec.copy(
type=type_arg,
expression=v_name,
could_be_none=True,
field_ctx=spec.field_ctx.copy(metadata={}),
)
)
if issubclass(spec.origin_type, typing.ByteString):
if spec.origin_type is bytes:
spec.builder.ensure_object_imported(decodebytes)
return f"decodebytes({spec.expression}.encode())"
elif spec.origin_type is bytearray:
spec.builder.ensure_object_imported(decodebytes)
return f"bytearray(decodebytes({spec.expression}.encode()))"
elif issubclass(spec.origin_type, str):
return spec.expression
elif ensure_generic_collection_subclass(spec, List):
return f"[{inner_expr()} for value in {spec.expression}]"
elif ensure_generic_collection_subclass(spec, typing.Deque):
spec.builder.ensure_module_imported(collections)
return (
f"collections.deque([{inner_expr()} "
f"for value in {spec.expression}])"
)
elif issubclass(spec.origin_type, Tuple): # type: ignore
if is_named_tuple(spec.origin_type):
return unpack_named_tuple(spec)
elif ensure_generic_collection(spec):
return unpack_tuple(spec, args)
elif ensure_generic_collection_subclass(spec, typing.FrozenSet):
return f"frozenset([{inner_expr()} for value in {spec.expression}])"
elif ensure_generic_collection_subclass(spec, typing.AbstractSet):
return f"set([{inner_expr()} for value in {spec.expression}])"
elif ensure_generic_mapping(spec, args, typing.ChainMap):
spec.builder.ensure_module_imported(collections)
return (
f'collections.ChainMap(*[{{{inner_expr(0, "key")}:{inner_expr(1)} '
f"for key, value in m.items()}} for m in {spec.expression}])"
)
elif ensure_generic_mapping(spec, args, typing_extensions.OrderedDict):
spec.builder.ensure_module_imported(collections)
return (
f'collections.OrderedDict({{{inner_expr(0, "key")}: '
f"{inner_expr(1)} for key, value in {spec.expression}.items()}})"
)
elif ensure_generic_mapping(spec, args, typing.DefaultDict):
spec.builder.ensure_module_imported(collections)
default_type = type_name(args[1] if args else None)
return (
f"collections.defaultdict({default_type}, "
f'{{{inner_expr(0, "key")}: '
f"{inner_expr(1)} for key, value in {spec.expression}.items()}})"
)
elif ensure_generic_mapping(spec, args, typing.Counter):
spec.builder.ensure_module_imported(collections)
return (
f'collections.Counter({{{inner_expr(0, "key")}: '
f"{inner_expr(1, v_type=int)} "
f"for key, value in {spec.expression}.items()}})"
)
elif is_typed_dict(spec.origin_type):
return unpack_typed_dict(spec)
elif ensure_generic_mapping(spec, args, typing.Mapping):
return (
f'{{{inner_expr(0, "key")}: {inner_expr(1)} '
f"for key, value in {spec.expression}.items()}}"
)
elif ensure_generic_collection_subclass(spec, typing.Sequence):
return f"[{inner_expr()} for value in {spec.expression}]"
@register
def unpack_pathlike(spec: ValueSpec) -> Optional[Expression]:
if spec.origin_type is os.PathLike:
spec.builder.ensure_module_imported(pathlib)
return f"{type_name(pathlib.PurePath)}({spec.expression})"
elif issubclass(spec.origin_type, os.PathLike):
return f"{type_name(spec.origin_type)}({spec.expression})"
@register
def unpack_enum(spec: ValueSpec) -> Optional[Expression]:
if issubclass(spec.origin_type, enum.Enum):
return f"{type_name(spec.origin_type)}({spec.expression})"