-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
main.py
1003 lines (870 loc) · 41.5 KB
/
main.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
"""
Logic for creating models, could perhaps be renamed to `models.py`.
"""
from __future__ import annotations as _annotations
import sys
import typing
import warnings
from abc import ABCMeta
from copy import copy, deepcopy
from enum import Enum
from functools import partial
from inspect import getdoc
from types import prepare_class, resolve_bases
from typing import Any, Generic, TypeVar, overload
import typing_extensions
from pydantic_core import CoreConfig, SchemaValidator
from ._internal import (
_decorators,
_forward_ref,
_generate_schema,
_generics,
_model_construction,
_repr,
_typing_extra,
_utils,
)
from ._internal._fields import Undefined
from .config import BaseConfig, ConfigDict, Extra, build_config, get_config
from .errors import PydanticUserError
from .fields import Field, FieldInfo, ModelPrivateAttr
from .json import custom_pydantic_encoder, pydantic_encoder
from .json_schema import DEFAULT_REF_TEMPLATE, GenerateJsonSchema, JsonSchemaMetadata
if typing.TYPE_CHECKING:
from inspect import Signature
from pydantic_core import CoreSchema, SchemaSerializer
from ._internal._utils import AbstractSetIntStr, MappingIntStrAny
AnyClassMethod = classmethod[Any]
TupleGenerator = typing.Generator[tuple[str, Any], None, None]
Model = typing.TypeVar('Model', bound='BaseModel')
# should be `set[int] | set[str] | dict[int, IncEx] | dict[str, IncEx] | None`, but mypy can't cope
IncEx = set[int] | set[str] | dict[int, Any] | dict[str, Any] | None
__all__ = 'BaseModel', 'create_model', 'Validator'
_object_setattr = _model_construction.object_setattr
# Note `ModelMetaclass` refers to `BaseModel`, but is also used to *create* `BaseModel`, so we need to add this extra
# (somewhat hacky) boolean to keep track of whether we've created the `BaseModel` class yet, and therefore whether it's
# safe to refer to it. If it *hasn't* been created, we assume that the `__new__` call we're in the middle of is for
# the `BaseModel` class, since that's defined immediately after the metaclass.
_base_class_defined = False
@typing_extensions.dataclass_transform(kw_only_default=True, field_specifiers=(Field,))
class ModelMetaclass(ABCMeta):
def __new__(
mcs,
cls_name: str,
bases: tuple[type[Any], ...],
namespace: dict[str, Any],
__pydantic_generic_origin__: type[BaseModel] | None = None,
__pydantic_generic_args__: tuple[Any, ...] | None = None,
__pydantic_generic_parameters__: tuple[Any, ...] | None = None,
**kwargs: Any,
) -> type:
if _base_class_defined:
base_field_names, class_vars, base_private_attributes = _collect_bases_data(bases)
config_new = build_config(cls_name, bases, namespace, kwargs)
namespace['model_config'] = config_new
private_attributes = _model_construction.inspect_namespace(
namespace, config_new.get('ignored_types', ()), class_vars, base_field_names
)
if private_attributes:
slots: set[str] = set(namespace.get('__slots__', ()))
namespace['__slots__'] = slots | private_attributes.keys()
if 'model_post_init' in namespace:
# if there are private_attributes and a model_post_init function, we wrap them both
# in a single function
namespace['_init_private_attributes'] = _model_construction.init_private_attributes
def __pydantic_post_init__(self_: Any, context: Any) -> None:
self_._init_private_attributes(context)
self_.model_post_init(context)
namespace['__pydantic_post_init__'] = __pydantic_post_init__
else:
namespace['__pydantic_post_init__'] = _model_construction.init_private_attributes
elif 'model_post_init' in namespace:
namespace['__pydantic_post_init__'] = namespace['model_post_init']
namespace['__class_vars__'] = class_vars
namespace['__private_attributes__'] = {**base_private_attributes, **private_attributes}
if config_new['json_encoders']:
json_encoder = partial(custom_pydantic_encoder, config_new['json_encoders'])
else:
json_encoder = pydantic_encoder # type: ignore[assignment]
namespace['__json_encoder__'] = staticmethod(json_encoder)
namespace['__schema_cache__'] = {}
if '__hash__' not in namespace and config_new['frozen']:
def hash_func(self_: Any) -> int:
return hash(self_.__class__) + hash(tuple(self_.__dict__.values()))
namespace['__hash__'] = hash_func
cls: type[BaseModel] = super().__new__(mcs, cls_name, bases, namespace, **kwargs) # type: ignore
cls.__pydantic_decorators__ = _decorators.gather_decorator_functions(cls)
cls.__pydantic_generic_args__ = __pydantic_generic_args__
cls.__pydantic_generic_origin__ = __pydantic_generic_origin__
cls.__pydantic_generic_parameters__ = __pydantic_generic_parameters__ or getattr(
cls, '__parameters__', None
)
cls.__pydantic_generic_defaults__ = None if not cls.__pydantic_generic_parameters__ else {}
cls.__pydantic_generic_typevars_map__ = (
None
if __pydantic_generic_origin__ is None
else dict(
zip(_generics.iter_contained_typevars(__pydantic_generic_origin__), __pydantic_generic_args__ or ())
)
)
cls.__pydantic_model_complete__ = False # Ensure this specific class gets completed
# preserve `__set_name__` protocol defined in https://peps.python.org/pep-0487
# for attributes not in `new_namespace` (e.g. private attributes)
for name, obj in private_attributes.items():
set_name = getattr(obj, '__set_name__', None)
if callable(set_name):
set_name(cls, name)
_model_construction.complete_model_class(
cls,
cls_name,
bases,
types_namespace=_typing_extra.parent_frame_namespace(),
raise_errors=False,
)
return cls
else:
# this is the BaseModel class itself being created, no logic required
return super().__new__(mcs, cls_name, bases, namespace, **kwargs)
def __instancecheck__(self, instance: Any) -> bool:
"""
Avoid calling ABC _abc_subclasscheck unless we're pretty sure.
See #3829 and python/cpython#92810
"""
return hasattr(instance, '__pydantic_validator__') and super().__instancecheck__(instance)
class BaseModel(_repr.Representation, metaclass=ModelMetaclass):
if typing.TYPE_CHECKING:
# populated by the metaclass, defined here to help IDEs only
__pydantic_validator__: typing.ClassVar[SchemaValidator]
__pydantic_core_schema__: typing.ClassVar[CoreSchema]
__pydantic_serializer__: typing.ClassVar[SchemaSerializer]
__pydantic_decorators__: typing.ClassVar[_decorators.DecoratorInfos]
"""metadata for `@validator`, `@root_validator` and `@serializer` decorators"""
model_fields: typing.ClassVar[dict[str, FieldInfo]] = {}
__json_encoder__: typing.ClassVar[typing.Callable[[Any], Any]] = lambda x: x # noqa: E731
__schema_cache__: typing.ClassVar[dict[Any, Any]] = {}
__signature__: typing.ClassVar[Signature]
__private_attributes__: typing.ClassVar[dict[str, ModelPrivateAttr]]
__class_vars__: typing.ClassVar[set[str]]
__fields_set__: set[str] = set()
__pydantic_generic_args__: typing.ClassVar[tuple[Any, ...] | None]
__pydantic_generic_defaults__: typing.ClassVar[dict[str, Any] | None]
__pydantic_generic_origin__: typing.ClassVar[type[BaseModel] | None]
__pydantic_generic_parameters__: typing.ClassVar[tuple[_typing_extra.TypeVarType, ...] | None]
__pydantic_generic_typevars_map__: typing.ClassVar[dict[_typing_extra.TypeVarType, Any] | None]
else:
__pydantic_validator__ = _model_construction.MockValidator(
'Pydantic models should inherit from BaseModel, BaseModel cannot be instantiated directly'
)
model_config = ConfigDict()
__slots__ = '__dict__', '__fields_set__'
__doc__ = '' # Null out the Representation docstring
__pydantic_model_complete__ = False
def __init__(__pydantic_self__, **data: Any) -> None:
"""
Create a new model by parsing and validating input data from keyword arguments.
Raises ValidationError if the input data cannot be parsed to form a valid model.
Uses something other than `self` for the first arg to allow "self" as a field name.
"""
# `__tracebackhide__` tells pytest and some other tools to omit this function from tracebacks
__tracebackhide__ = True
__pydantic_self__.__pydantic_validator__.validate_python(data, self_instance=__pydantic_self__)
@classmethod
def model_validate(cls: type[Model], obj: Any) -> Model:
# `__tracebackhide__` tells pytest and some other tools to omit this function from tracebacks
__tracebackhide__ = True
return cls.__pydantic_validator__.validate_python(obj)
@classmethod
def model_validate_json(cls: type[Model], json_data: str | bytes | bytearray) -> Model:
# `__tracebackhide__` tells pytest and some other tools to omit this function from tracebacks
__tracebackhide__ = True
return cls.__pydantic_validator__.validate_json(json_data)
if typing.TYPE_CHECKING:
# model_after_init is called after at the end of `__init__` if it's defined
def model_post_init(self, _context: Any) -> None:
pass
@typing.no_type_check
def __setattr__(self, name, value):
if name in self.__class_vars__:
raise AttributeError(
f'"{name}" is a ClassVar of `{self.__class__.__name__}` and cannot be set on an instance. '
f'If you want to set a value on the class, use `{self.__class__.__name__}.{name} = value`.'
)
if name.startswith('_'):
_object_setattr(self, name, value)
elif self.model_config['frozen']:
raise TypeError(f'"{self.__class__.__name__}" is frozen and does not support item assignment')
elif self.model_config['validate_assignment']:
self.__pydantic_validator__.validate_assignment(self, name, value)
elif self.model_config['extra'] is not Extra.allow and name not in self.model_fields:
# TODO - matching error
raise ValueError(f'"{self.__class__.__name__}" object has no field "{name}"')
else:
self.__dict__[name] = value
self.__fields_set__.add(name)
def __getstate__(self) -> dict[Any, Any]:
private_attrs = ((k, getattr(self, k, Undefined)) for k in self.__private_attributes__)
return {
'__dict__': self.__dict__,
'__fields_set__': self.__fields_set__,
'__private_attribute_values__': {k: v for k, v in private_attrs if v is not Undefined},
}
def __setstate__(self, state: dict[Any, Any]) -> None:
_object_setattr(self, '__dict__', state['__dict__'])
_object_setattr(self, '__fields_set__', state['__fields_set__'])
for name, value in state.get('__private_attribute_values__', {}).items():
_object_setattr(self, name, value)
def model_dump(
self,
*,
mode: typing_extensions.Literal['json', 'python'] | str = 'python',
include: IncEx = None,
exclude: IncEx = None,
by_alias: bool = False,
exclude_unset: bool = False,
exclude_defaults: bool = False,
exclude_none: bool = False,
round_trip: bool = False,
warnings: bool = True,
) -> dict[str, Any]:
"""
Generate a dictionary representation of the model, optionally specifying which fields to include or exclude.
"""
return self.__pydantic_serializer__.to_python(
self,
mode=mode,
by_alias=by_alias,
include=include,
exclude=exclude,
exclude_unset=exclude_unset,
exclude_defaults=exclude_defaults,
exclude_none=exclude_none,
round_trip=round_trip,
warnings=warnings,
)
def model_dump_json(
self,
*,
indent: int | None = None,
include: IncEx = None,
exclude: IncEx = None,
by_alias: bool = False,
exclude_unset: bool = False,
exclude_defaults: bool = False,
exclude_none: bool = False,
round_trip: bool = False,
warnings: bool = True,
) -> bytes:
"""
Generate a JSON representation of the model, `include` and `exclude` arguments as per `dict()`.
"""
return self.__pydantic_serializer__.to_json(
self,
indent=indent,
include=include,
exclude=exclude,
by_alias=by_alias,
exclude_unset=exclude_unset,
exclude_defaults=exclude_defaults,
exclude_none=exclude_none,
round_trip=round_trip,
warnings=warnings,
)
@classmethod
def from_orm(cls: type[Model], obj: Any) -> Model:
# TODO remove
return cls.model_validate(obj)
@classmethod
def model_construct(cls: type[Model], _fields_set: set[str] | None = None, **values: Any) -> Model:
"""
Creates a new model setting __dict__ and __fields_set__ from trusted or pre-validated data.
Default values are respected, but no other validation is performed.
Behaves as if `Config.extra = 'allow'` was set since it adds all passed values
"""
m = cls.__new__(cls)
fields_values: dict[str, Any] = {}
for name, field in cls.model_fields.items():
if field.alias and field.alias in values:
fields_values[name] = values[field.alias]
elif name in values:
fields_values[name] = values[name]
elif not field.is_required():
fields_values[name] = field.get_default()
fields_values.update(values)
_object_setattr(m, '__dict__', fields_values)
if _fields_set is None:
_fields_set = set(values.keys())
_object_setattr(m, '__fields_set__', _fields_set)
if hasattr(m, '__pydantic_post_init__'):
m.__pydantic_post_init__(context=None)
return m
def _copy_and_set_values(self: Model, values: dict[str, Any], fields_set: set[str], *, deep: bool) -> Model:
if deep:
# chances of having empty dict here are quite low for using smart_deepcopy
values = deepcopy(values)
cls = self.__class__
m = cls.__new__(cls)
_object_setattr(m, '__dict__', values)
_object_setattr(m, '__fields_set__', fields_set)
for name in self.__private_attributes__:
value = getattr(self, name, Undefined)
if value is not Undefined:
if deep:
value = deepcopy(value)
_object_setattr(m, name, value)
return m
# @typing_extensions.deprecated('This method is now deprecated; use `model_copy` instead')
def copy(
self: Model,
*,
include: AbstractSetIntStr | MappingIntStrAny | None = None,
exclude: AbstractSetIntStr | MappingIntStrAny | None = None,
update: dict[str, Any] | None = None,
deep: bool = False,
) -> Model:
"""
This method is now deprecated; use `model_copy` instead. If you need include / exclude, use:
data = self.model_dump(include=include, exclude=exclude, round_trip=True)
data = {**data, **(update or {})}
copied = self.model_validate(data)
Duplicate a model, optionally choose which fields to include, exclude and change.
:param include: fields to include in new model
:param exclude: fields to exclude from new model, as with values this takes precedence over include
:param update: values to change/add in the new model. Note: the data is not validated before creating
the new model: you should trust this data
:param deep: set to `True` to make a deep copy of the model
:return: new model instance
"""
warnings.warn(
'The `copy` method is deprecated; use `model_copy` instead. '
'See the docstring of `BaseModel.copy` for details about how to handle include / exclude / update.',
DeprecationWarning,
)
values = dict(
self._iter(to_dict=False, by_alias=False, include=include, exclude=exclude, exclude_unset=False),
**(update or {}),
)
# new `__fields_set__` can have unset optional fields with a set value in `update` kwarg
if update:
fields_set = self.__fields_set__ | update.keys()
else:
fields_set = set(self.__fields_set__)
# removing excluded fields from `__fields_set__`
if exclude:
fields_set -= set(exclude)
return self._copy_and_set_values(values, fields_set, deep=deep)
@classmethod
def model_json_schema(
cls,
by_alias: bool = True,
ref_template: str = DEFAULT_REF_TEMPLATE,
schema_generator: type[GenerateJsonSchema] = GenerateJsonSchema,
) -> dict[str, Any]:
"""
To override the logic used to generate the JSON schema, you can create a subclass of GenerateJsonSchema
with your desired modifications, then override this method on a custom base class and set the default
value of `schema_generator` to be your subclass.
"""
cached = cls.__schema_cache__.get((by_alias, ref_template))
if cached is not None:
return cached
s = schema_generator(by_alias=by_alias, ref_template=ref_template).generate(cls.__pydantic_core_schema__)
cls.__schema_cache__[(by_alias, ref_template)] = s
return s
@classmethod
def model_json_schema_metadata(cls) -> JsonSchemaMetadata | None:
"""
Overriding this method provides a simple way to modify certain aspects of the JSON schema generation.
This is a convenience method primarily intended to control how the "generic" properties
of the JSON schema are populated, or apply minor transformations through `extra_updates` or
`modify_js_function`. See https://json-schema.org/understanding-json-schema/reference/generic.html
and the comments surrounding the definition of `JsonSchemaMetadata` for more details.
If you want to make more sweeping changes to how the JSON schema is generated, you will probably
want to subclass `GenerateJsonSchema` and pass your subclass in the `schema_generator` argument to the
`model_json_schema` method.
"""
title = cls.model_config['title'] or cls.__name__
description = getdoc(cls) or None
return {'title': title, 'description': description}
@classmethod
def schema_json(
cls, *, by_alias: bool = True, ref_template: str = DEFAULT_REF_TEMPLATE, **dumps_kwargs: Any
) -> str:
from .json import pydantic_encoder
return cls.model_config['json_dumps'](
cls.model_json_schema(by_alias=by_alias, ref_template=ref_template),
default=pydantic_encoder,
**dumps_kwargs,
)
@classmethod
# @typing_extensions.deprecated('This method is only used for _iter, which is deprecated')
@typing.no_type_check
def _get_value(
cls,
v: Any,
to_dict: bool,
by_alias: bool,
include: AbstractSetIntStr | MappingIntStrAny | None,
exclude: AbstractSetIntStr | MappingIntStrAny | None,
exclude_unset: bool,
exclude_defaults: bool,
exclude_none: bool,
) -> Any:
if isinstance(v, BaseModel):
if to_dict:
return v.model_dump(
by_alias=by_alias,
exclude_unset=exclude_unset,
exclude_defaults=exclude_defaults,
include=include,
exclude=exclude,
exclude_none=exclude_none,
)
else:
return v.copy(include=include, exclude=exclude)
value_exclude = _utils.ValueItems(v, exclude) if exclude else None
value_include = _utils.ValueItems(v, include) if include else None
if isinstance(v, dict):
return {
k_: cls._get_value(
v_,
to_dict=to_dict,
by_alias=by_alias,
exclude_unset=exclude_unset,
exclude_defaults=exclude_defaults,
include=value_include and value_include.for_element(k_),
exclude=value_exclude and value_exclude.for_element(k_),
exclude_none=exclude_none,
)
for k_, v_ in v.items()
if (not value_exclude or not value_exclude.is_excluded(k_))
and (not value_include or value_include.is_included(k_))
}
elif _utils.sequence_like(v):
seq_args = (
cls._get_value(
v_,
to_dict=to_dict,
by_alias=by_alias,
exclude_unset=exclude_unset,
exclude_defaults=exclude_defaults,
include=value_include and value_include.for_element(i),
exclude=value_exclude and value_exclude.for_element(i),
exclude_none=exclude_none,
)
for i, v_ in enumerate(v)
if (not value_exclude or not value_exclude.is_excluded(i))
and (not value_include or value_include.is_included(i))
)
return v.__class__(*seq_args) if _typing_extra.is_namedtuple(v.__class__) else v.__class__(seq_args)
elif isinstance(v, Enum) and getattr(cls.model_config, 'use_enum_values', False):
return v.value
else:
return v
@classmethod
def model_rebuild(
cls,
*,
force: bool = False,
raise_errors: bool = True,
types_namespace: dict[str, Any] | None = None,
typevars_map: dict[str, Any] | None = None,
) -> bool | None:
"""
Try to (Re)construct the model schema.
"""
if not force and cls.__pydantic_model_complete__:
return None
else:
parents_namespace = _typing_extra.parent_frame_namespace()
if types_namespace and parents_namespace:
types_namespace = {**parents_namespace, **types_namespace}
elif parents_namespace:
types_namespace = parents_namespace
return _model_construction.complete_model_class(
cls,
cls.__name__,
cls.__bases__,
raise_errors=raise_errors,
types_namespace=types_namespace,
typevars_map=typevars_map,
)
def __iter__(self) -> TupleGenerator:
"""
so `dict(model)` works
"""
yield from self.__dict__.items()
# @typing_extensions.deprecated('This private method is only used for `BaseModel.copy`, which is deprecated')
def _iter(
self,
to_dict: bool = False,
by_alias: bool = False,
include: AbstractSetIntStr | MappingIntStrAny | None = None,
exclude: AbstractSetIntStr | MappingIntStrAny | None = None,
exclude_unset: bool = False,
exclude_defaults: bool = False,
exclude_none: bool = False,
) -> TupleGenerator:
# Merge field set excludes with explicit exclude parameter with explicit overriding field set options.
# The extra "is not None" guards are not logically necessary but optimizes performance for the simple case.
if exclude is not None:
exclude = _utils.ValueItems.merge(
{k: v.exclude for k, v in self.model_fields.items() if v.exclude is not None}, exclude
)
if include is not None:
include = _utils.ValueItems.merge(
{k: v.include for k, v in self.model_fields.items()}, include, intersect=True
)
allowed_keys = self._calculate_keys(
include=include, exclude=exclude, exclude_unset=exclude_unset # type: ignore
)
if allowed_keys is None and not (to_dict or by_alias or exclude_unset or exclude_defaults or exclude_none):
# huge boost for plain _iter()
yield from self.__dict__.items()
return
value_exclude = _utils.ValueItems(self, exclude) if exclude is not None else None
value_include = _utils.ValueItems(self, include) if include is not None else None
for field_key, v in self.__dict__.items():
if (allowed_keys is not None and field_key not in allowed_keys) or (exclude_none and v is None):
continue
if exclude_defaults:
try:
field = self.model_fields[field_key]
except KeyError:
pass
else:
if not field.is_required() and field.default == v:
continue
if by_alias and field_key in self.model_fields:
dict_key = self.model_fields[field_key].alias or field_key
else:
dict_key = field_key
if to_dict or value_include or value_exclude:
v = self._get_value(
v,
to_dict=to_dict,
by_alias=by_alias,
include=value_include and value_include.for_element(field_key),
exclude=value_exclude and value_exclude.for_element(field_key),
exclude_unset=exclude_unset,
exclude_defaults=exclude_defaults,
exclude_none=exclude_none,
)
yield dict_key, v
def _calculate_keys(
self,
include: MappingIntStrAny | None,
exclude: MappingIntStrAny | None,
exclude_unset: bool,
update: dict[str, Any] | None = None,
) -> typing.AbstractSet[str] | None:
if include is None and exclude is None and exclude_unset is False:
return None
keys: typing.AbstractSet[str]
if exclude_unset:
keys = self.__fields_set__.copy()
else:
keys = self.__dict__.keys()
if include is not None:
keys &= include.keys()
if update:
keys -= update.keys()
if exclude:
keys -= {k for k, v in exclude.items() if _utils.ValueItems.is_true(v)}
return keys
def __eq__(self, other: Any) -> bool:
if not isinstance(other, BaseModel):
return False
# When comparing instances of generic types for equality, as long as all field values are equal,
# only require their generic origin types to be equal, rather than exact type equality.
# This prevents headaches like MyGeneric(x=1) != MyGeneric[Any](x=1).
self_type = getattr(self, '__pydantic_generic_origin__', None) or self.__class__
other_type = getattr(other, '__pydantic_generic_origin__', None) or other.__class__
if self_type != other_type:
return False
if self.__dict__ != other.__dict__:
return False
# If the types and field values match, check for equality of private attributes
for k in self.__private_attributes__:
if getattr(self, k, Undefined) != getattr(other, k, Undefined):
return False
return True
def model_copy(self: Model, *, update: dict[str, Any] | None = None, deep: bool = False) -> Model:
"""
Returns a copy of the model.
:param update: values to change/add in the new model. Note: the data is not validated before creating
the new model: you should trust this data
:param deep: set to `True` to make a deep copy of the model
:return: new model instance
"""
copied = self.__deepcopy__() if deep else self.__copy__()
if update:
copied.__dict__.update(update)
copied.__fields_set__.update(update.keys())
return copied
def __copy__(self: Model) -> Model:
"""
Returns a shallow copy of the model
"""
cls = type(self)
m = cls.__new__(cls)
_object_setattr(m, '__dict__', copy(self.__dict__))
_object_setattr(m, '__fields_set__', copy(self.__fields_set__))
for name in self.__private_attributes__:
value = getattr(self, name, Undefined)
if value is not Undefined:
_object_setattr(m, name, value)
return m
def __deepcopy__(self: Model, memo: dict[int, Any] | None = None) -> Model:
"""
Returns a deep copy of the model
"""
cls = type(self)
m = cls.__new__(cls)
_object_setattr(m, '__dict__', deepcopy(self.__dict__, memo=memo))
# This next line doesn't need a deepcopy because __fields_set__ is a set[str],
# and attempting a deepcopy would be marginally slower.
_object_setattr(m, '__fields_set__', copy(self.__fields_set__))
for name in self.__private_attributes__:
value = getattr(self, name, Undefined)
if value is not Undefined:
_object_setattr(m, name, deepcopy(value, memo=memo))
return m
def __repr_args__(self) -> _repr.ReprArgs:
return [
(k, v)
for k, v in self.__dict__.items()
if not k.startswith('_') and (k not in self.model_fields or self.model_fields[k].repr)
]
def __class_getitem__(
cls, typevar_values: type[Any] | tuple[type[Any], ...]
) -> type[BaseModel] | _forward_ref.PydanticForwardRef:
cached = _generics.get_cached_generic_type_early(cls, typevar_values)
if cached is not None:
return cached
if cls is BaseModel:
raise TypeError('Type parameters should be placed on typing.Generic, not BaseModel')
if not hasattr(cls, '__parameters__'):
raise TypeError(f'{cls} cannot be parametrized because it does not inherit from typing.Generic')
if not cls.__pydantic_generic_parameters__ and Generic not in cls.__bases__:
raise TypeError(f'{cls} is not a generic class')
if not isinstance(typevar_values, tuple):
typevar_values = (typevar_values,)
_generics.check_parameters_count(cls, typevar_values)
# Build map from generic typevars to passed params
typevars_map: dict[_typing_extra.TypeVarType, type[Any]] = dict(
zip(cls.__pydantic_generic_parameters__ or (), typevar_values)
)
if _utils.all_identical(typevars_map.keys(), typevars_map.values()) and typevars_map:
submodel = cls # if arguments are equal to parameters it's the same object
_generics.set_cached_generic_type(cls, typevar_values, submodel)
else:
parent_args = cls.__pydantic_generic_args__
if not parent_args:
args = typevar_values
else:
args = tuple(_generics.replace_types(arg, typevars_map) for arg in parent_args)
origin = cls.__pydantic_generic_origin__ or cls
model_name = origin.model_parametrized_name(args)
params = tuple(
{param: None for param in _generics.iter_contained_typevars(typevars_map.values())}
) # use dict as ordered set
with _generics.generic_recursion_self_type(origin, args) as maybe_self_type:
if maybe_self_type is not None:
return maybe_self_type
cached = _generics.get_cached_generic_type_late(cls, typevar_values, origin, args)
if cached is not None:
return cached
submodel = _generics.create_generic_submodel(model_name, origin, args, params)
# Update cache
_generics.set_cached_generic_type(cls, typevar_values, submodel, origin, args)
# Doing the rebuild _after_ populating the cache prevents infinite recursion
submodel.model_rebuild(force=True, raise_errors=False, typevars_map=typevars_map)
return submodel
@classmethod
def model_parametrized_name(cls, params: tuple[type[Any], ...]) -> str:
"""
Compute class name for parametrizations of generic classes.
:param params: Tuple of types of the class . Given a generic class
`Model` with 2 type variables and a concrete model `Model[str, int]`,
the value `(str, int)` would be passed to `params`.
:return: String representing the new class where `params` are
passed to `cls` as type variables.
This method can be overridden to achieve a custom naming scheme for generic BaseModels.
"""
if not issubclass(cls, Generic): # type: ignore[arg-type]
raise TypeError('Concrete names should only be generated for generic models.')
# Any strings received should represent forward references, so we handle them specially below.
# If we eventually move toward wrapping them in a ForwardRef in __class_getitem__ in the future,
# we may be able to remove this special case.
param_names = [param if isinstance(param, str) else _repr.display_as_type(param) for param in params]
params_component = ', '.join(param_names)
return f'{cls.__name__}[{params_component}]'
_base_class_defined = True
@typing.overload
def create_model(
__model_name: str,
*,
__config__: ConfigDict | type[BaseConfig] | None = None,
__base__: None = None,
__module__: str = __name__,
__validators__: dict[str, AnyClassMethod] = None,
__cls_kwargs__: dict[str, Any] = None,
**field_definitions: Any,
) -> type[Model]:
...
@typing.overload
def create_model(
__model_name: str,
*,
__config__: ConfigDict | type[BaseConfig] | None = None,
__base__: type[Model] | tuple[type[Model], ...],
__module__: str = __name__,
__validators__: dict[str, AnyClassMethod] = None,
__cls_kwargs__: dict[str, Any] = None,
**field_definitions: Any,
) -> type[Model]:
...
def create_model(
__model_name: str,
*,
__config__: ConfigDict | type[BaseConfig] | None = None,
__base__: type[Model] | tuple[type[Model], ...] | None = None,
__module__: str = __name__,
__validators__: dict[str, AnyClassMethod] = None,
__cls_kwargs__: dict[str, Any] = None,
__slots__: tuple[str, ...] | None = None,
**field_definitions: Any,
) -> type[Model]:
"""
Dynamically create a model.
:param __model_name: name of the created model
:param __config__: config dict/class to use for the new model
:param __base__: base class for the new model to inherit from
:param __module__: module of the created model
:param __validators__: a dict of method names and @validator class methods
:param __cls_kwargs__: a dict for class creation
:param __slots__: Deprecated, `__slots__` should not be passed to `create_model`
:param field_definitions: fields of the model (or extra fields if a base is supplied)
in the format `<name>=(<type>, <default value>)` or `<name>=<default value>, e.g.
`foobar=(str, ...)` or `foobar=123`, or, for complex use-cases, in the format
`<name>=<Field>` or `<name>=(<type>, <FieldInfo>)`, e.g.
`foo=Field(datetime, default_factory=datetime.utcnow, alias='bar')` or
`foo=(str, FieldInfo(title='Foo'))`
"""
if __slots__ is not None:
# __slots__ will be ignored from here on
warnings.warn('__slots__ should not be passed to create_model', RuntimeWarning)
if __base__ is not None:
if __config__ is not None:
raise PydanticUserError('to avoid confusion __config__ and __base__ cannot be used together')
if not isinstance(__base__, tuple):
__base__ = (__base__,)
else:
__base__ = (typing.cast(typing.Type['Model'], BaseModel),)
__cls_kwargs__ = __cls_kwargs__ or {}
fields = {}
annotations = {}
for f_name, f_def in field_definitions.items():
if f_name.startswith('_'):
warnings.warn(f'fields may not start with an underscore, ignoring "{f_name}"', RuntimeWarning)
if isinstance(f_def, tuple):
try:
f_annotation, f_value = f_def
except ValueError as e:
raise PydanticUserError(
'field definitions should either be a tuple of (<type>, <default>) or just a '
'default value, unfortunately this means tuples as '
'default values are not allowed'
) from e
else:
f_annotation, f_value = None, f_def
if f_annotation:
annotations[f_name] = f_annotation
fields[f_name] = f_value
namespace: dict[str, Any] = {'__annotations__': annotations, '__module__': __module__}
if __validators__:
namespace.update(__validators__)
namespace.update(fields)
if __config__:
namespace['model_config'] = get_config(__config__)
resolved_bases = resolve_bases(__base__)
meta, ns, kwds = prepare_class(__model_name, resolved_bases, kwds=__cls_kwargs__)
if resolved_bases is not __base__:
ns['__orig_bases__'] = __base__
namespace.update(ns)
return meta(__model_name, resolved_bases, namespace, **kwds)
T = TypeVar('T')
class Validator(Generic[T]):
@overload
def __init__(self, __type: type[T], *, config: CoreConfig | None = None) -> None:
...
# Adding this overload ensures you can use special forms without getting mypy errors.
# For example:
# v: Validator[int | str] = Validator(int | str)
# Type checkers don't consider special forms like `int | str` (or `Union[int, str]`) to satisfy a Type[T]
@overload
def __init__(self, __type: Any, *, config: CoreConfig | None = None) -> None:
...
def __init__(self, __type: Any, *, config: CoreConfig | None = None) -> None:
self._type = __type
merged_config: CoreConfig = {
**(config or {}), # type: ignore[misc]
**getattr(__type, 'model_config', {}),
}
arbitrary_types = bool((config or {}).get('arbitrary_types_allowed', False))
local_ns = _typing_extra.parent_frame_namespace(parent_depth=2)
# BaseModel uses it's own __module__ to find out where it was defined
# and then look for symbols to resolve forward references in those globals
# On the other hand Validator() can be called with arbitrary objects,
# including type aliases where __module__ (always `typing.py`) is not useful
# So instead we look at the globals in our parent stack frame
# This works for the case where Validator() is called in a module that
# has the target of forward references in its scope but
# does not work for more complex cases
# for example, take the following:
#
# a.py
# ```python
# from typing import List, Dict
# IntList = List[int]
# OuterDict = Dict[str, 'IntList']
# ```
#
# b.py
# ```python
# from pydantic import Validator
# from a import OuterDict
# IntList = int # replaces the symbol the forward reference is looking for
# v = Validator(OuterDict)
# v({"x": 1}) # should fail but doesn't
# ```
#
# If OuterDict were a BaseModel this would work because it would resolve
# the forward reference within the `a.py` namespace.
# But Validator(OuterDict) can't know what module OuterDict came from.
# In other words, the assumption that _all_ forward references exist in the
# module we are being called from is not technically always true
# Although most of the time it is and it works fine for recursive models and such/
# BaseModel's behavior isn't perfect either and _can_ break in similar ways,
# so there is no right or wrong between the two.
# But at the very least this behavior is _subtly_ different from BaseModel's.
global_ns = sys._getframe(1).f_globals.copy()
global_ns.update(local_ns or {})
gen = _generate_schema.GenerateSchema(
arbitrary_types=arbitrary_types, types_namespace=global_ns, typevars_map={}
)
schema = gen.generate_schema(__type)
self._validator = SchemaValidator(schema, config=merged_config)
def __call__(self, __input: Any) -> T:
return self._validator.validate_python(__input)
def _collect_bases_data(bases: tuple[type[Any], ...]) -> tuple[set[str], set[str], dict[str, ModelPrivateAttr]]:
field_names: set[str] = set()
class_vars: set[str] = set()
private_attributes: dict[str, ModelPrivateAttr] = {}
for base in bases:
if _base_class_defined and issubclass(base, BaseModel) and base != BaseModel:
# model_fields might not be defined yet in the case of generics, so we use getattr here:
field_names.update(getattr(base, 'model_fields', {}).keys())