-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
main.py
695 lines (602 loc) · 27.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
"""
Logic for creating models, could perhaps be renamed to `models.py`.
"""
from __future__ import annotations as _annotations
import typing
import warnings
from abc import ABCMeta
from copy import deepcopy
from enum import Enum
from functools import partial
from types import prepare_class, resolve_bases
from typing import Any
import typing_extensions
from ._internal import _decorators, _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 .schema import default_ref_template, model_schema
if typing.TYPE_CHECKING:
from inspect import Signature
from pydantic_core import CoreSchema, SchemaSerializer, SchemaValidator
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'
_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, FieldInfo))
class ModelMetaclass(ABCMeta):
def __new__(mcs, cls_name: str, bases: tuple[type[Any], ...], namespace: dict[str, Any], **kwargs: Any) -> type:
if _base_class_defined:
config_new = build_config(cls_name, bases, namespace, kwargs)
namespace['model_config'] = config_new
namespace['__private_attributes__'] = private_attributes = _model_construction.inspect_namespace(namespace)
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, **kwargs: Any) -> None:
self_._init_private_attributes()
self_.model_post_init(**kwargs)
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']
validator_functions = _decorators.ValidationFunctions(bases)
namespace[validator_functions.model_attribute] = validator_functions
serializer_functions = _decorators.SerializationFunctions(bases)
namespace[serializer_functions.model_attribute] = serializer_functions
for name, value in namespace.items():
found_validator = validator_functions.extract_decorator(name, value)
if not found_validator:
serializer_functions.extract_decorator(name, value)
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)
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
_model_construction.complete_model_class(
cls,
cls_name,
validator_functions,
serializer_functions,
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_validator_functions__: typing.ClassVar[_decorators.ValidationFunctions]
__pydantic_serializer_functions__: typing.ClassVar[_decorators.SerializationFunctions]
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()
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 the function from tracebacks
"""
__tracebackhide__ = True
values, fields_set = __pydantic_self__.__pydantic_validator__.validate_python(data)
_object_setattr(__pydantic_self__, '__dict__', values)
_object_setattr(__pydantic_self__, '__fields_set__', fields_set)
if hasattr(__pydantic_self__, '__pydantic_post_init__'):
__pydantic_self__.__pydantic_post_init__(context=None)
@classmethod
def model_validate(cls: type[Model], obj: Any) -> Model:
values, fields_set = cls.__pydantic_validator__.validate_python(obj)
m = cls.__new__(cls)
_object_setattr(m, '__dict__', values)
_object_setattr(m, '__fields_set__', fields_set)
if hasattr(cls, '__pydantic_post_init__'):
cls.__pydantic_post_init__(context=None) # type: ignore[attr-defined]
return m
@classmethod
def model_validate_json(cls: type[Model], json_data: str | bytes | bytearray) -> Model:
values, fields_set = cls.__pydantic_validator__.validate_json(json_data)
m = cls.__new__(cls)
_object_setattr(m, '__dict__', values)
_object_setattr(m, '__fields_set__', fields_set)
if hasattr(cls, '__pydantic_post_init__'):
cls.__pydantic_post_init__(context=None) # type: ignore[attr-defined]
return m
if typing.TYPE_CHECKING:
# model_after_init is called after at the end of `__init__` if it's defined
def model_post_init(self, **kwargs: Any) -> None:
pass
@typing.no_type_check
def __setattr__(self, 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']:
values, fields_set = self.__pydantic_validator__.validate_assignment(name, value, self.__dict__)
_object_setattr(self, '__dict__', values)
self.__fields_set__ |= fields_set
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: typing.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
def copy(
self: Model,
*,
include: AbstractSetIntStr | MappingIntStrAny | None = None,
exclude: AbstractSetIntStr | MappingIntStrAny | None = None,
update: typing.Dict[str, Any] | None = None,
deep: bool = False,
) -> Model:
"""
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
"""
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
) -> typing.Dict[str, Any]:
cached = cls.__schema_cache__.get((by_alias, ref_template))
if cached is not None:
return cached
s = model_schema(cls, by_alias=by_alias, ref_template=ref_template)
cls.__schema_cache__[(by_alias, ref_template)] = s
return s
@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.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: typing.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.__pydantic_validator_functions__,
cls.__pydantic_serializer_functions__,
cls.__bases__,
raise_errors=raise_errors,
types_namespace=types_namespace,
)
def __iter__(self) -> 'TupleGenerator':
"""
so `dict(model)` works
"""
yield from self.__dict__.items()
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 or self.__exclude_fields__ is not None:
# exclude = _utils.ValueItems.merge(self.__exclude_fields__, exclude)
#
# if include is not None or self.__include_fields__ is not None:
# include = _utils.ValueItems.merge(self.__include_fields__, 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: typing.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 isinstance(other, BaseModel):
return self.model_dump() == other.model_dump()
else:
return self.model_dump() == other
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)
]
_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)