-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
builder.py
1422 lines (1323 loc) · 53.3 KB
/
builder.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
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import enum
import importlib
import inspect
import math
import types
import typing
import uuid
from contextlib import contextmanager
# noinspection PyProtectedMember
from dataclasses import _FIELDS # type: ignore
from dataclasses import MISSING, Field, is_dataclass
from functools import lru_cache
try:
from dataclasses import KW_ONLY # type: ignore
except ImportError:
KW_ONLY = object() # type: ignore
import typing_extensions
from mashumaro.config import (
ADD_DIALECT_SUPPORT,
ADD_SERIALIZATION_CONTEXT,
TO_DICT_ADD_BY_ALIAS_FLAG,
TO_DICT_ADD_OMIT_NONE_FLAG,
BaseConfig,
SerializationStrategyValueType,
)
from mashumaro.core.const import Sentinel
from mashumaro.core.helpers import ConfigValue
from mashumaro.core.meta.code.lines import CodeLines
from mashumaro.core.meta.helpers import (
evaluate_forward_ref,
get_args,
get_class_that_defines_field,
get_class_that_defines_method,
get_forward_ref_referencing_globals,
get_literal_values,
get_name_error_name,
get_type_annotations,
hash_type_args,
is_annotated,
is_class_var,
is_dataclass_dict_mixin,
is_dataclass_dict_mixin_subclass,
is_dialect_subclass,
is_hashable,
is_init_var,
is_literal,
is_local_type_name,
is_named_tuple,
is_optional,
is_type_var_any,
resolve_type_params,
substitute_type_params,
type_name,
)
from mashumaro.core.meta.types.common import (
FieldContext,
NoneType,
ValueSpec,
clean_id,
)
from mashumaro.core.meta.types.pack import PackerRegistry
from mashumaro.core.meta.types.unpack import (
SubtypeUnpackerBuilder,
UnpackerRegistry,
)
from mashumaro.dialect import Dialect
from mashumaro.exceptions import ( # noqa
BadDialect,
BadHookSignature,
ExtraKeysError,
InvalidFieldValue,
MissingDiscriminatorError,
MissingField,
SuitableVariantNotFoundError,
ThirdPartyModuleNotFoundError,
UnresolvedTypeReferenceError,
UnserializableDataError,
UnserializableField,
UnsupportedDeserializationEngine,
UnsupportedSerializationEngine,
)
from mashumaro.types import Alias, Discriminator
__PRE_SERIALIZE__ = "__pre_serialize__"
__PRE_DESERIALIZE__ = "__pre_deserialize__"
__POST_SERIALIZE__ = "__post_serialize__"
__POST_DESERIALIZE__ = "__post_deserialize__"
SIMPLE_TYPES = (int, float, bool, str, NoneType)
class InternalMethodName(str):
_PREFIX = "__mashumaro_"
_SUFFIX = "__"
@classmethod
def from_public(cls, value: str) -> "InternalMethodName":
return cls(f"{cls._PREFIX}{value}{cls._SUFFIX}")
@property
def public(self) -> str:
return self[len(self._PREFIX) : -len(self._SUFFIX)]
class CodeBuilder:
def __init__(
self,
cls: typing.Type,
type_args: typing.Tuple[typing.Type, ...] = (),
dialect: typing.Optional[typing.Type[Dialect]] = None,
first_method: str = "from_dict",
allow_postponed_evaluation: bool = True,
format_name: str = "dict",
decoder: typing.Optional[typing.Any] = None,
encoder: typing.Optional[typing.Any] = None,
encoder_kwargs: typing.Optional[typing.Dict[str, typing.Any]] = None,
default_dialect: typing.Optional[typing.Type[Dialect]] = None,
attrs: typing.Any = None,
attrs_registry: typing.Optional[
typing.Dict[typing.Any, typing.Any]
] = None,
):
self.cls = cls
self.lines: CodeLines = CodeLines()
self.globals: typing.Dict[str, typing.Any] = {}
self.resolved_type_params: typing.Dict[
typing.Type, typing.Dict[typing.Type, typing.Type]
] = {}
self.field_classes: typing.Dict = {}
self.initial_type_args = type_args
if dialect is not None and not is_dialect_subclass(dialect):
raise BadDialect(
'Keyword argument "dialect" must be a subclass of Dialect '
f"in {type_name(self.cls)}.{first_method}"
)
self.dialect = dialect
self.default_dialect = default_dialect
self.allow_postponed_evaluation = allow_postponed_evaluation
self.format_name = format_name
self.decoder = decoder
self.encoder = encoder
self.encoder_kwargs = encoder_kwargs or {}
if attrs is not None:
self.attrs = attrs
else:
self.attrs = cls
if attrs_registry is not None:
self.attrs_registry = attrs_registry
else:
self.attrs_registry = {}
def reset(self) -> None:
self.lines.reset()
self.globals = globals().copy()
self.resolved_type_params = resolve_type_params(
self.cls, self.initial_type_args
)
self.field_classes = {}
@property
def namespace(self) -> typing.Mapping[typing.Any, typing.Any]:
return self.cls.__dict__
@property
def annotations(self) -> typing.Dict[str, typing.Any]:
return self.namespace.get("__annotations__", {})
@property
def is_nailed(self) -> bool:
return self.attrs is self.cls
def __get_field_types(
self, recursive: bool = True, include_extras: bool = False
) -> typing.Dict[str, typing.Any]:
fields = {}
try:
field_type_hints = typing_extensions.get_type_hints(
self.cls, include_extras=include_extras
)
except NameError as e:
name = get_name_error_name(e)
raise UnresolvedTypeReferenceError(self.cls, name) from None
for fname, ftype in field_type_hints.items():
if is_class_var(ftype) or is_init_var(ftype) or ftype is KW_ONLY:
continue
if recursive or fname in self.annotations:
fields[fname] = ftype
return fields
def _get_field_class(self, field_name: str) -> typing.Any:
try:
cls = self.field_classes[field_name]
except KeyError:
cls = get_class_that_defines_field(field_name, self.cls)
self.field_classes[field_name] = cls
return cls
def get_real_type(
self, field_name: str, field_type: typing.Type
) -> typing.Type:
cls = self._get_field_class(field_name)
return substitute_type_params(
field_type, self.resolved_type_params[cls]
)
def get_field_resolved_type_params(
self, field_name: str
) -> typing.Dict[typing.Type, typing.Type]:
cls = self._get_field_class(field_name)
return self.resolved_type_params[cls]
def get_field_types(
self, include_extras: bool = False
) -> typing.Dict[str, typing.Any]:
return self.__get_field_types(include_extras=include_extras)
def get_type_name_identifier(
self,
typ: typing.Optional[typing.Type],
resolved_type_params: typing.Optional[
typing.Dict[typing.Type, typing.Type]
] = None,
) -> str:
field_type = type_name(typ, resolved_type_params=resolved_type_params)
if is_local_type_name(field_type):
field_type = clean_id(field_type)
self.ensure_object_imported(typ, field_type)
return field_type
@property
@lru_cache()
def dataclass_fields(self) -> typing.Dict[str, Field]:
d = {}
for ancestor in self.cls.__mro__[-1:0:-1]:
if is_dataclass(ancestor):
for field in getattr(ancestor, _FIELDS).values():
d[field.name] = field
for name in self.__get_field_types(recursive=False):
field = self.namespace.get(name, MISSING)
if isinstance(field, Field):
d[name] = field
else:
field = self.namespace.get(_FIELDS, {}).get(name, MISSING)
if isinstance(field, Field):
d[name] = field
else:
d.pop(name, None)
return d
@property
def metadatas(self) -> typing.Dict[str, typing.Mapping[str, typing.Any]]:
return {
name: field.metadata
for name, field in self.dataclass_fields.items()
}
@lru_cache(None)
def get_field_default(
self, name: str, call_factory: bool = False
) -> typing.Any:
field = self.dataclass_fields.get(name)
if field:
if field.default is not MISSING:
return field.default
else:
if call_factory and field.default_factory is not MISSING:
return field.default_factory()
else:
return field.default_factory
else:
return self.namespace.get(name, MISSING)
def add_type_modules(self, *types_: typing.Type) -> None:
for t in types_:
module = inspect.getmodule(t)
if not module:
continue
self.ensure_module_imported(module)
if is_literal(t):
literal_args = get_literal_values(t)
self.add_type_modules(*literal_args)
else:
args = get_args(t)
if args:
self.add_type_modules(*args)
constraints = getattr(t, "__constraints__", ())
if constraints:
self.add_type_modules(*constraints)
bound = getattr(t, "__bound__", ())
if bound:
self.add_type_modules(bound)
def ensure_module_imported(self, module: types.ModuleType) -> None:
self.globals.setdefault(module.__name__, module)
package = module.__name__.split(".")[0]
self.globals.setdefault(package, importlib.import_module(package))
def ensure_object_imported(
self,
obj: typing.Any,
name: typing.Optional[str] = None,
) -> None:
self.globals.setdefault(name or obj.__name__, obj)
def add_line(self, line: str) -> None:
self.lines.append(line)
@contextmanager
def indent(
self,
expr: typing.Optional[str] = None,
) -> typing.Generator[None, None, None]:
with self.lines.indent(expr):
yield
def compile(self) -> None:
code = self.lines.as_text()
if self.get_config().debug:
if self.dialect is not None:
print(f"{type_name(self.cls)}[{type_name(self.dialect)}]:")
else:
print(f"{type_name(self.cls)}:")
print(code)
exec(code, self.globals, self.__dict__)
def evaluate_forward_ref(
self,
typ: typing.ForwardRef,
owner: typing.Optional[typing.Type],
) -> typing.Optional[typing.Type]:
globalns = get_forward_ref_referencing_globals(
typ, owner, self.globals
)
return evaluate_forward_ref(typ, globalns, self.__dict__)
def get_declared_hook(self, method_name: str) -> typing.Any:
cls = get_class_that_defines_method(method_name, self.cls)
if cls is not None and not is_dataclass_dict_mixin(cls):
return cls.__dict__[method_name]
def _add_unpack_method_lines_lazy(self, method_name: str) -> None:
if self.default_dialect is not None:
self.add_type_modules(self.default_dialect)
self.add_line(
f"CodeBuilder("
f"cls,"
f"first_method='{method_name}',"
f"allow_postponed_evaluation=False,"
f"format_name='{self.format_name}',"
f"decoder={type_name(self.decoder)},"
f"default_dialect={type_name(self.default_dialect)}"
f").add_unpack_method()"
)
unpacker_args = [
"d",
self.get_unpack_method_flags(pass_decoder=True),
]
unpacker_args_s = ", ".join(filter(None, unpacker_args))
self.add_line(f"return cls.{method_name}({unpacker_args_s})")
def _add_unpack_method_lines(self, method_name: str) -> None:
config = self.get_config()
if (
config.lazy_compilation
and self.allow_postponed_evaluation
and self.is_nailed
):
self._add_unpack_method_lines_lazy(method_name)
return
try:
field_types = self.get_field_types(include_extras=True)
except UnresolvedTypeReferenceError:
if (
not self.allow_postponed_evaluation
or not config.allow_postponed_evaluation
):
raise
self._add_unpack_method_lines_lazy(method_name)
else:
if self.decoder is not None:
self.add_line("d = decoder(d)")
discr = self.get_discriminator()
if discr:
if not discr.include_subtypes:
raise ValueError(
"Config based discriminator must have "
"'include_subtypes' enabled"
)
discr = Discriminator(
# prevent RecursionError
field=discr.field,
include_subtypes=discr.include_subtypes,
variant_tagger_fn=discr.variant_tagger_fn,
)
self.add_type_modules(self.cls)
method = SubtypeUnpackerBuilder(discr).build(
spec=ValueSpec(
type=self.cls,
expression="d",
builder=self,
field_ctx=FieldContext("", {}),
)
)
self.add_line(f"return {method}")
return
pre_deserialize = self.get_declared_hook(__PRE_DESERIALIZE__)
if pre_deserialize:
if not isinstance(pre_deserialize, classmethod):
raise BadHookSignature(
f"`{__PRE_DESERIALIZE__}` must be a class method with "
"Callable[[Dict[Any, Any]], Dict[Any, Any]] signature"
)
else:
self.add_line(f"d = cls.{__PRE_DESERIALIZE__}(d)")
post_deserialize = self.get_declared_hook(__POST_DESERIALIZE__)
if post_deserialize:
if not isinstance(post_deserialize, classmethod):
raise BadHookSignature(
f"`{__POST_DESERIALIZE__}` must be a class method "
f"with Callable[[{type_name(self.cls)}], "
f"{type_name(self.cls)}] signature"
)
filtered_fields = []
pos_args = []
kw_args = []
missing_kw_only = False
add_kwargs = False
kw_only_fields = set()
field_blocks = []
for fname, ftype in field_types.items():
field = self.dataclass_fields.get(fname)
if field and not field.init:
continue
if missing_kw_only:
kw_only_fields.add(fname)
elif field:
kw_only = getattr(field, "kw_only", MISSING)
if kw_only is MISSING:
missing_kw_only = True
kw_only_fields.add(fname)
elif kw_only:
kw_only_fields.add(fname)
else:
missing_kw_only = True
kw_only_fields.add(fname)
metadata = self.metadatas.get(fname, {})
alias = self.__get_field_alias(fname, ftype, metadata, config)
filtered_fields.append((fname, alias, ftype))
if filtered_fields:
if config.forbid_extra_keys:
allowed_keys = {f[1] or f[0] for f in filtered_fields}
# If a discriminator with a field is set via config,
# we should allow this field to be present in the input
# This will not work for annotated discriminators though...
discr = self.get_discriminator(look_in_parents=True)
if discr and discr.field:
allowed_keys.add(discr.field)
if config.allow_deserialization_not_by_alias:
allowed_keys |= {f[0] for f in filtered_fields}
allowed_keys_str = "'" + "', '".join(allowed_keys) + "'"
self.add_line("d_keys = set(d.keys())")
self.add_line(
f"forbidden_keys = d_keys - {{{allowed_keys_str}}}"
)
with self.indent("if forbidden_keys:"):
self.add_line(
"raise ExtraKeysError(forbidden_keys,cls) "
"from None"
)
with self.indent("try:"):
for fname, alias, ftype in filtered_fields:
self.add_type_modules(ftype)
metadata = self.metadatas.get(fname, {})
field_block = FieldUnpackerCodeBlockBuilder(
self, self.lines.branch_off()
).build(
fname=fname,
ftype=ftype,
metadata=metadata,
alias=alias,
)
if field_block.in_kwargs:
add_kwargs = True
field_blocks.append(field_block)
if add_kwargs:
self.add_line("kwargs = {}")
in_kwargs = False
for field_block in field_blocks:
self.lines.extend(field_block.lines)
if field_block.in_kwargs:
in_kwargs = True
else:
if (
field_block.fname in kw_only_fields
or in_kwargs
):
kw_args.append(field_block.fname)
else:
pos_args.append(field_block.fname)
with self.indent("except AttributeError:"):
with self.indent("if not isinstance(d, dict):"):
self.add_line(
"raise ValueError('Argument for "
f"{type_name(self.cls)}.{method_name} method "
"should be a dict instance') from None"
)
with self.indent("else:"):
self.add_line("raise")
args = [f"__{f}" for f in pos_args]
for kw_arg in kw_args:
args.append(f"{kw_arg}=__{kw_arg}")
if add_kwargs:
args.append("**kwargs")
cls_inst = f"cls({', '.join(args)})"
if post_deserialize:
self.add_line(f"return cls.{__POST_DESERIALIZE__}({cls_inst})")
else:
self.add_line(f"return {cls_inst}")
def _add_unpack_method_with_dialect_lines(self, method_name: str) -> None:
if self.decoder is not None:
self.add_line("d = decoder(d)")
unpacker_args = ", ".join(
filter(None, ("cls", "d", self.get_unpack_method_flags()))
)
cache_name = f"__dialect_{self.format_name}_unpacker_cache__"
self.add_line(f"unpacker = cls.{cache_name}.get(dialect)")
with self.indent("if unpacker is not None:"):
self.add_line(f"return unpacker({unpacker_args})")
if self.default_dialect:
self.add_type_modules(self.default_dialect)
self.add_line(
"CodeBuilder("
"cls,dialect=dialect,"
f"first_method='{method_name}',"
f"format_name='{self.format_name}',"
f"default_dialect={type_name(self.default_dialect)}"
").add_unpack_method()"
)
self.add_line(f"return cls.{cache_name}[dialect]({unpacker_args})")
def add_unpack_method(self) -> None:
self.reset()
method_name = self.get_unpack_method_name(
type_args=self.initial_type_args,
format_name=self.format_name,
decoder=self.decoder,
)
if self.decoder is not None:
self.add_type_modules(self.decoder)
dialects_feature = self.is_code_generation_option_enabled(
ADD_DIALECT_SUPPORT
)
cache_name = f"__dialect_{self.format_name}_unpacker_cache__"
if dialects_feature:
with self.indent(f"if not '{cache_name}' in cls.__dict__:"):
self.add_line(f"cls.{cache_name} = {{}}")
if self.dialect is None and self.is_nailed:
self.add_line("@classmethod")
self._add_unpack_method_definition(method_name)
with self.indent():
if dialects_feature and self.dialect is None:
with self.indent("if dialect is None:"):
self._add_unpack_method_lines(method_name)
with self.indent("else:"):
self._add_unpack_method_with_dialect_lines(method_name)
else:
self._add_unpack_method_lines(method_name)
self._add_setattr_method(method_name, cache_name)
self.compile()
def _add_unpack_method_definition(self, method_name: str) -> None:
kwargs = ""
default_kwargs = self.get_unpack_method_default_flag_values(
pass_decoder=True
)
if default_kwargs:
kwargs += f", {default_kwargs}"
if self.is_nailed:
self.add_line(f"def {method_name}(cls, d{kwargs}):")
else:
self.add_line(f"def {method_name}(d{kwargs}):")
@lru_cache()
@typing.no_type_check
def get_config(
self,
cls: typing.Optional[typing.Type] = None,
look_in_parents: bool = True,
) -> typing.Type[BaseConfig]:
if cls is None:
cls = self.cls
if look_in_parents:
config_cls = getattr(cls, "Config", BaseConfig)
else:
config_cls = cls.__dict__.get("Config", BaseConfig)
if not issubclass(config_cls, BaseConfig):
config_cls = type(
"Config",
(BaseConfig, config_cls),
{**BaseConfig.__dict__, **config_cls.__dict__},
)
return config_cls
def get_discriminator(
self, look_in_parents: bool = False
) -> typing.Optional[Discriminator]:
if look_in_parents:
classes = self.cls.__mro__
else:
classes = (self.cls,)
for cls in classes:
discriminator = self.get_config(
cls, look_in_parents=False
).discriminator
if discriminator:
return discriminator
return None
def get_pack_method_flags(
self,
cls: typing.Optional[typing.Type] = None,
pass_encoder: bool = False,
) -> str:
pluggable_flags = []
if pass_encoder and self.encoder is not None:
pluggable_flags.append("encoder=encoder")
for value in self._get_encoder_kwargs(cls).values():
pluggable_flags.append(f"{value[0]}={value[0]}")
for option, flag in (
(TO_DICT_ADD_OMIT_NONE_FLAG, "omit_none"),
(TO_DICT_ADD_BY_ALIAS_FLAG, "by_alias"),
(ADD_DIALECT_SUPPORT, "dialect"),
(ADD_SERIALIZATION_CONTEXT, "context"),
):
if self.is_code_generation_option_enabled(option, cls):
if self.is_code_generation_option_enabled(option):
pluggable_flags.append(f"{flag}={flag}")
return ", ".join(pluggable_flags)
def get_unpack_method_flags(
self,
cls: typing.Optional[typing.Type] = None,
pass_decoder: bool = False,
) -> str:
pluggable_flags = []
if pass_decoder and self.decoder is not None:
pluggable_flags.append("decoder=decoder")
for option, flag in ((ADD_DIALECT_SUPPORT, "dialect"),):
if self.is_code_generation_option_enabled(option, cls):
if self.is_code_generation_option_enabled(option):
pluggable_flags.append(f"{flag}={flag}")
return ", ".join(pluggable_flags)
def get_pack_method_default_flag_values(
self,
cls: typing.Optional[typing.Type] = None,
pass_encoder: bool = False,
) -> str:
pos_param_names = []
pos_param_values = []
kw_param_names = []
kw_param_values = []
if pass_encoder and self.encoder is not None:
pos_param_names.append("encoder")
pos_param_values.append(type_name(self.encoder))
for value in self._get_encoder_kwargs(cls).values():
kw_param_names.append(value[0])
kw_param_values.append(value[1])
omit_none_feature = self.is_code_generation_option_enabled(
TO_DICT_ADD_OMIT_NONE_FLAG, cls
)
if omit_none_feature:
omit_none = self.get_dialect_or_config_option("omit_none", False)
kw_param_names.append("omit_none")
kw_param_values.append("True" if omit_none else "False")
by_alias_feature = self.is_code_generation_option_enabled(
TO_DICT_ADD_BY_ALIAS_FLAG, cls
)
if by_alias_feature:
serialize_by_alias = self.get_dialect_or_config_option(
"serialize_by_alias", False, cls
)
kw_param_names.append("by_alias")
kw_param_values.append("True" if serialize_by_alias else "False")
dialects_feature = self.is_code_generation_option_enabled(
ADD_DIALECT_SUPPORT, cls
)
if dialects_feature:
kw_param_names.append("dialect")
kw_param_values.append("None")
context_feature = self.is_code_generation_option_enabled(
ADD_SERIALIZATION_CONTEXT, cls
)
if context_feature:
kw_param_names.append("context")
kw_param_values.append("None")
if pos_param_names:
pluggable_flags_str = ", ".join(
[f"{n}={v}" for n, v in zip(pos_param_names, pos_param_values)]
)
else:
pluggable_flags_str = ""
if kw_param_names:
if pos_param_names:
pluggable_flags_str += ", "
pluggable_flags_str += "*, " + ", ".join(
[f"{n}={v}" for n, v in zip(kw_param_names, kw_param_values)]
)
return pluggable_flags_str
def get_unpack_method_default_flag_values(
self, pass_decoder: bool = False
) -> str:
pos_param_names = []
pos_param_values = []
kw_param_names = []
kw_param_values = []
if pass_decoder and self.decoder is not None:
pos_param_names.append("decoder")
pos_param_values.append(type_name(self.decoder))
kw_param_names.append("dialect")
kw_param_values.append("None")
if pos_param_names:
pluggable_flags_str = ", ".join(
[f"{n}={v}" for n, v in zip(pos_param_names, pos_param_values)]
)
else:
pluggable_flags_str = ""
if kw_param_names:
if pos_param_names:
pluggable_flags_str += ", "
pluggable_flags_str += "*, " + ", ".join(
[f"{n}={v}" for n, v in zip(kw_param_names, kw_param_values)]
)
return pluggable_flags_str
def is_code_generation_option_enabled(
self, option: str, cls: typing.Optional[typing.Type] = None
) -> bool:
if cls is None:
cls = self.cls
return option in self.get_config(cls).code_generation_options
@classmethod
def get_unpack_method_name(
cls,
type_args: typing.Iterable = (),
format_name: str = "dict",
decoder: typing.Optional[typing.Any] = None,
) -> InternalMethodName:
if format_name != "dict" and decoder is not None:
return InternalMethodName.from_public(f"from_{format_name}")
else:
method_name = "from_dict"
if format_name != "dict":
method_name += f"_{format_name}"
if type_args:
method_name += f"_{hash_type_args(type_args)}"
return InternalMethodName.from_public(method_name)
@classmethod
def get_pack_method_name(
cls,
type_args: typing.Tuple[typing.Type, ...] = (),
format_name: str = "dict",
encoder: typing.Optional[typing.Any] = None,
) -> InternalMethodName:
if format_name != "dict" and encoder is not None:
return InternalMethodName.from_public(f"to_{format_name}")
else:
method_name = "to_dict"
if format_name != "dict":
method_name += f"_{format_name}"
if type_args:
method_name += f"_{hash_type_args(type_args)}"
return InternalMethodName.from_public(f"{method_name}")
def _add_pack_method_lines_lazy(self, method_name: str) -> None:
if self.default_dialect is not None:
self.add_type_modules(self.default_dialect)
self.add_line(
"CodeBuilder("
"self.__class__,"
f"first_method='{method_name}',"
"allow_postponed_evaluation=False,"
f"format_name='{self.format_name}',"
f"encoder={type_name(self.encoder)},"
f"encoder_kwargs={self._get_encoder_kwargs()},"
f"default_dialect={type_name(self.default_dialect)}"
").add_pack_method()"
)
packer_args = self.get_pack_method_flags(pass_encoder=True)
self.add_line(f"return self.{method_name}({packer_args})")
def _add_pack_method_lines(self, method_name: str) -> None:
config = self.get_config()
if (
config.lazy_compilation
and self.allow_postponed_evaluation
and self.is_nailed
):
self._add_pack_method_lines_lazy(method_name)
return
try:
field_types = self.get_field_types(include_extras=True)
except UnresolvedTypeReferenceError:
if (
not self.allow_postponed_evaluation
or not config.allow_postponed_evaluation
):
raise
self._add_pack_method_lines_lazy(method_name)
else:
pre_serialize = self.get_declared_hook(__PRE_SERIALIZE__)
if pre_serialize:
if self.is_code_generation_option_enabled(
ADD_SERIALIZATION_CONTEXT
):
pre_serialize_args = "context=context"
else:
pre_serialize_args = ""
self.add_line(
f"self = self.{__PRE_SERIALIZE__}({pre_serialize_args})"
)
by_alias_feature = self.is_code_generation_option_enabled(
TO_DICT_ADD_BY_ALIAS_FLAG
)
omit_none_feature = self.is_code_generation_option_enabled(
TO_DICT_ADD_OMIT_NONE_FLAG
)
serialize_by_alias = self.get_dialect_or_config_option(
"serialize_by_alias", False
)
omit_none = self.get_dialect_or_config_option("omit_none", False)
omit_default = self.get_dialect_or_config_option(
"omit_default", False
)
force_value = omit_default
packers = {}
aliases = {}
nullable_fields = set()
nontrivial_nullable_fields = set()
fnames_and_types: typing.Iterable[
typing.Tuple[str, typing.Any]
] = field_types.items()
if self.get_config().sort_keys:
fnames_and_types = sorted(fnames_and_types, key=lambda x: x[0])
for fname, ftype in fnames_and_types:
if self.metadatas.get(fname, {}).get("serialize") == "omit":
continue
packer, alias, could_be_none = self._get_field_packer(
fname, ftype, config, force_value
)
packers[fname] = packer
if alias:
aliases[fname] = alias
if could_be_none:
nullable_fields.add(fname)
if packer != "value":
nontrivial_nullable_fields.add(fname)
if (
nontrivial_nullable_fields
or nullable_fields
and (omit_none or omit_none_feature)
or by_alias_feature
and aliases
or omit_default
):
kwargs = "kwargs"
self.add_line("kwargs = {}")
for fname, packer in packers.items():
if force_value:
self.add_line(f"value = self.{fname}")
alias = aliases.get(fname)
if omit_default:
# do not call default_factory if we don't need to
default = self.get_field_default(
fname, call_factory=True
)
else:
default = None
if fname in nullable_fields:
if (
packer == "value"
and not omit_none
and not omit_none_feature
and not (omit_default and default is None)
):
self._pack_method_set_value(
fname=fname,
alias=alias,
by_alias_feature=by_alias_feature,
packed_value=(
"value" if force_value else f"self.{fname}"
),
omit_default=omit_default,
)
continue
if not force_value: # to add it only once
self.add_line(f"value = self.{fname}")
with self.indent("if value is not None:"):
self._pack_method_set_value(
fname=fname,
alias=alias,
by_alias_feature=by_alias_feature,
packed_value=packer,
omit_default=(
omit_default and default is not None
),
)
if omit_none and not omit_none_feature:
continue
elif omit_default and default is None:
continue
with self.indent("else:"):
if omit_none_feature:
with self.indent("if not omit_none:"):
self._pack_method_set_value(
fname=fname,
alias=alias,
by_alias_feature=by_alias_feature,
packed_value="None",
omit_default=False,
)
else:
self._pack_method_set_value(
fname=fname,
alias=alias,
by_alias_feature=by_alias_feature,
packed_value="None",
omit_default=False,
)
else:
self._pack_method_set_value(
fname=fname,
alias=alias,
by_alias_feature=by_alias_feature,
packed_value=packer,
omit_default=omit_default,
)
else:
kwargs_parts = []
for fname, packer in packers.items():
if serialize_by_alias:
fname_or_alias = aliases.get(fname, fname)
else:
fname_or_alias = fname
kwargs_parts.append(
(
fname_or_alias,
packer if packer != "value" else f"self.{fname}",
)
)
kwargs = ", ".join(f"'{k}': {v}" for k, v in kwargs_parts)
kwargs = f"{{{kwargs}}}"
post_serialize = self.get_declared_hook(__POST_SERIALIZE__)
if self.encoder is not None:
if self.encoder_kwargs:
encoder_options = ", ".join(
f"{k}={v[0]}" for k, v in self.encoder_kwargs.items()
)
return_statement = (
f"return encoder({{}}, {encoder_options})"
)
else:
return_statement = "return encoder({})"
else:
return_statement = "return {}"