您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

1114 行
44 KiB

  1. import warnings
  2. from abc import ABCMeta
  3. from copy import deepcopy
  4. from enum import Enum
  5. from functools import partial
  6. from pathlib import Path
  7. from types import FunctionType, prepare_class, resolve_bases
  8. from typing import (
  9. TYPE_CHECKING,
  10. AbstractSet,
  11. Any,
  12. Callable,
  13. ClassVar,
  14. Dict,
  15. List,
  16. Mapping,
  17. Optional,
  18. Tuple,
  19. Type,
  20. TypeVar,
  21. Union,
  22. cast,
  23. no_type_check,
  24. overload,
  25. )
  26. from typing_extensions import dataclass_transform
  27. from pydantic.v1.class_validators import ValidatorGroup, extract_root_validators, extract_validators, inherit_validators
  28. from pydantic.v1.config import BaseConfig, Extra, inherit_config, prepare_config
  29. from pydantic.v1.error_wrappers import ErrorWrapper, ValidationError
  30. from pydantic.v1.errors import ConfigError, DictError, ExtraError, MissingError
  31. from pydantic.v1.fields import (
  32. MAPPING_LIKE_SHAPES,
  33. Field,
  34. ModelField,
  35. ModelPrivateAttr,
  36. PrivateAttr,
  37. Undefined,
  38. is_finalvar_with_default_val,
  39. )
  40. from pydantic.v1.json import custom_pydantic_encoder, pydantic_encoder
  41. from pydantic.v1.parse import Protocol, load_file, load_str_bytes
  42. from pydantic.v1.schema import default_ref_template, model_schema
  43. from pydantic.v1.types import PyObject, StrBytes
  44. from pydantic.v1.typing import (
  45. AnyCallable,
  46. get_args,
  47. get_origin,
  48. is_classvar,
  49. is_namedtuple,
  50. is_union,
  51. resolve_annotations,
  52. update_model_forward_refs,
  53. )
  54. from pydantic.v1.utils import (
  55. DUNDER_ATTRIBUTES,
  56. ROOT_KEY,
  57. ClassAttribute,
  58. GetterDict,
  59. Representation,
  60. ValueItems,
  61. generate_model_signature,
  62. is_valid_field,
  63. is_valid_private_name,
  64. lenient_issubclass,
  65. sequence_like,
  66. smart_deepcopy,
  67. unique_list,
  68. validate_field_name,
  69. )
  70. if TYPE_CHECKING:
  71. from inspect import Signature
  72. from pydantic.v1.class_validators import ValidatorListDict
  73. from pydantic.v1.types import ModelOrDc
  74. from pydantic.v1.typing import (
  75. AbstractSetIntStr,
  76. AnyClassMethod,
  77. CallableGenerator,
  78. DictAny,
  79. DictStrAny,
  80. MappingIntStrAny,
  81. ReprArgs,
  82. SetStr,
  83. TupleGenerator,
  84. )
  85. Model = TypeVar('Model', bound='BaseModel')
  86. __all__ = 'BaseModel', 'create_model', 'validate_model'
  87. _T = TypeVar('_T')
  88. def validate_custom_root_type(fields: Dict[str, ModelField]) -> None:
  89. if len(fields) > 1:
  90. raise ValueError(f'{ROOT_KEY} cannot be mixed with other fields')
  91. def generate_hash_function(frozen: bool) -> Optional[Callable[[Any], int]]:
  92. def hash_function(self_: Any) -> int:
  93. return hash(self_.__class__) + hash(tuple(self_.__dict__.values()))
  94. return hash_function if frozen else None
  95. # If a field is of type `Callable`, its default value should be a function and cannot to ignored.
  96. ANNOTATED_FIELD_UNTOUCHED_TYPES: Tuple[Any, ...] = (property, type, classmethod, staticmethod)
  97. # When creating a `BaseModel` instance, we bypass all the methods, properties... added to the model
  98. UNTOUCHED_TYPES: Tuple[Any, ...] = (FunctionType,) + ANNOTATED_FIELD_UNTOUCHED_TYPES
  99. # Note `ModelMetaclass` refers to `BaseModel`, but is also used to *create* `BaseModel`, so we need to add this extra
  100. # (somewhat hacky) boolean to keep track of whether we've created the `BaseModel` class yet, and therefore whether it's
  101. # 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
  102. # the `BaseModel` class, since that's defined immediately after the metaclass.
  103. _is_base_model_class_defined = False
  104. @dataclass_transform(kw_only_default=True, field_specifiers=(Field,))
  105. class ModelMetaclass(ABCMeta):
  106. @no_type_check # noqa C901
  107. def __new__(mcs, name, bases, namespace, **kwargs): # noqa C901
  108. fields: Dict[str, ModelField] = {}
  109. config = BaseConfig
  110. validators: 'ValidatorListDict' = {}
  111. pre_root_validators, post_root_validators = [], []
  112. private_attributes: Dict[str, ModelPrivateAttr] = {}
  113. base_private_attributes: Dict[str, ModelPrivateAttr] = {}
  114. slots: SetStr = namespace.get('__slots__', ())
  115. slots = {slots} if isinstance(slots, str) else set(slots)
  116. class_vars: SetStr = set()
  117. hash_func: Optional[Callable[[Any], int]] = None
  118. for base in reversed(bases):
  119. if _is_base_model_class_defined and issubclass(base, BaseModel) and base != BaseModel:
  120. fields.update(smart_deepcopy(base.__fields__))
  121. config = inherit_config(base.__config__, config)
  122. validators = inherit_validators(base.__validators__, validators)
  123. pre_root_validators += base.__pre_root_validators__
  124. post_root_validators += base.__post_root_validators__
  125. base_private_attributes.update(base.__private_attributes__)
  126. class_vars.update(base.__class_vars__)
  127. hash_func = base.__hash__
  128. resolve_forward_refs = kwargs.pop('__resolve_forward_refs__', True)
  129. allowed_config_kwargs: SetStr = {
  130. key
  131. for key in dir(config)
  132. if not (key.startswith('__') and key.endswith('__')) # skip dunder methods and attributes
  133. }
  134. config_kwargs = {key: kwargs.pop(key) for key in kwargs.keys() & allowed_config_kwargs}
  135. config_from_namespace = namespace.get('Config')
  136. if config_kwargs and config_from_namespace:
  137. raise TypeError('Specifying config in two places is ambiguous, use either Config attribute or class kwargs')
  138. config = inherit_config(config_from_namespace, config, **config_kwargs)
  139. validators = inherit_validators(extract_validators(namespace), validators)
  140. vg = ValidatorGroup(validators)
  141. for f in fields.values():
  142. f.set_config(config)
  143. extra_validators = vg.get_validators(f.name)
  144. if extra_validators:
  145. f.class_validators.update(extra_validators)
  146. # re-run prepare to add extra validators
  147. f.populate_validators()
  148. prepare_config(config, name)
  149. untouched_types = ANNOTATED_FIELD_UNTOUCHED_TYPES
  150. def is_untouched(v: Any) -> bool:
  151. return isinstance(v, untouched_types) or v.__class__.__name__ == 'cython_function_or_method'
  152. if (namespace.get('__module__'), namespace.get('__qualname__')) != ('pydantic.main', 'BaseModel'):
  153. annotations = resolve_annotations(namespace.get('__annotations__', {}), namespace.get('__module__', None))
  154. # annotation only fields need to come first in fields
  155. for ann_name, ann_type in annotations.items():
  156. if is_classvar(ann_type):
  157. class_vars.add(ann_name)
  158. elif is_finalvar_with_default_val(ann_type, namespace.get(ann_name, Undefined)):
  159. class_vars.add(ann_name)
  160. elif is_valid_field(ann_name):
  161. validate_field_name(bases, ann_name)
  162. value = namespace.get(ann_name, Undefined)
  163. allowed_types = get_args(ann_type) if is_union(get_origin(ann_type)) else (ann_type,)
  164. if (
  165. is_untouched(value)
  166. and ann_type != PyObject
  167. and not any(
  168. lenient_issubclass(get_origin(allowed_type), Type) for allowed_type in allowed_types
  169. )
  170. ):
  171. continue
  172. fields[ann_name] = ModelField.infer(
  173. name=ann_name,
  174. value=value,
  175. annotation=ann_type,
  176. class_validators=vg.get_validators(ann_name),
  177. config=config,
  178. )
  179. elif ann_name not in namespace and config.underscore_attrs_are_private:
  180. private_attributes[ann_name] = PrivateAttr()
  181. untouched_types = UNTOUCHED_TYPES + config.keep_untouched
  182. for var_name, value in namespace.items():
  183. can_be_changed = var_name not in class_vars and not is_untouched(value)
  184. if isinstance(value, ModelPrivateAttr):
  185. if not is_valid_private_name(var_name):
  186. raise NameError(
  187. f'Private attributes "{var_name}" must not be a valid field name; '
  188. f'Use sunder or dunder names, e. g. "_{var_name}" or "__{var_name}__"'
  189. )
  190. private_attributes[var_name] = value
  191. elif config.underscore_attrs_are_private and is_valid_private_name(var_name) and can_be_changed:
  192. private_attributes[var_name] = PrivateAttr(default=value)
  193. elif is_valid_field(var_name) and var_name not in annotations and can_be_changed:
  194. validate_field_name(bases, var_name)
  195. inferred = ModelField.infer(
  196. name=var_name,
  197. value=value,
  198. annotation=annotations.get(var_name, Undefined),
  199. class_validators=vg.get_validators(var_name),
  200. config=config,
  201. )
  202. if var_name in fields:
  203. if lenient_issubclass(inferred.type_, fields[var_name].type_):
  204. inferred.type_ = fields[var_name].type_
  205. else:
  206. raise TypeError(
  207. f'The type of {name}.{var_name} differs from the new default value; '
  208. f'if you wish to change the type of this field, please use a type annotation'
  209. )
  210. fields[var_name] = inferred
  211. _custom_root_type = ROOT_KEY in fields
  212. if _custom_root_type:
  213. validate_custom_root_type(fields)
  214. vg.check_for_unused()
  215. if config.json_encoders:
  216. json_encoder = partial(custom_pydantic_encoder, config.json_encoders)
  217. else:
  218. json_encoder = pydantic_encoder
  219. pre_rv_new, post_rv_new = extract_root_validators(namespace)
  220. if hash_func is None:
  221. hash_func = generate_hash_function(config.frozen)
  222. exclude_from_namespace = fields | private_attributes.keys() | {'__slots__'}
  223. new_namespace = {
  224. '__config__': config,
  225. '__fields__': fields,
  226. '__exclude_fields__': {
  227. name: field.field_info.exclude for name, field in fields.items() if field.field_info.exclude is not None
  228. }
  229. or None,
  230. '__include_fields__': {
  231. name: field.field_info.include for name, field in fields.items() if field.field_info.include is not None
  232. }
  233. or None,
  234. '__validators__': vg.validators,
  235. '__pre_root_validators__': unique_list(
  236. pre_root_validators + pre_rv_new,
  237. name_factory=lambda v: v.__name__,
  238. ),
  239. '__post_root_validators__': unique_list(
  240. post_root_validators + post_rv_new,
  241. name_factory=lambda skip_on_failure_and_v: skip_on_failure_and_v[1].__name__,
  242. ),
  243. '__schema_cache__': {},
  244. '__json_encoder__': staticmethod(json_encoder),
  245. '__custom_root_type__': _custom_root_type,
  246. '__private_attributes__': {**base_private_attributes, **private_attributes},
  247. '__slots__': slots | private_attributes.keys(),
  248. '__hash__': hash_func,
  249. '__class_vars__': class_vars,
  250. **{n: v for n, v in namespace.items() if n not in exclude_from_namespace},
  251. }
  252. cls = super().__new__(mcs, name, bases, new_namespace, **kwargs)
  253. # set __signature__ attr only for model class, but not for its instances
  254. cls.__signature__ = ClassAttribute('__signature__', generate_model_signature(cls.__init__, fields, config))
  255. if not _is_base_model_class_defined:
  256. # Cython does not understand the `if TYPE_CHECKING:` condition in the
  257. # BaseModel's body (where annotations are set), so clear them manually:
  258. getattr(cls, '__annotations__', {}).clear()
  259. if resolve_forward_refs:
  260. cls.__try_update_forward_refs__()
  261. # preserve `__set_name__` protocol defined in https://peps.python.org/pep-0487
  262. # for attributes not in `new_namespace` (e.g. private attributes)
  263. for name, obj in namespace.items():
  264. if name not in new_namespace:
  265. set_name = getattr(obj, '__set_name__', None)
  266. if callable(set_name):
  267. set_name(cls, name)
  268. return cls
  269. def __instancecheck__(self, instance: Any) -> bool:
  270. """
  271. Avoid calling ABC _abc_subclasscheck unless we're pretty sure.
  272. See #3829 and python/cpython#92810
  273. """
  274. return hasattr(instance, '__post_root_validators__') and super().__instancecheck__(instance)
  275. object_setattr = object.__setattr__
  276. class BaseModel(Representation, metaclass=ModelMetaclass):
  277. if TYPE_CHECKING:
  278. # populated by the metaclass, defined here to help IDEs only
  279. __fields__: ClassVar[Dict[str, ModelField]] = {}
  280. __include_fields__: ClassVar[Optional[Mapping[str, Any]]] = None
  281. __exclude_fields__: ClassVar[Optional[Mapping[str, Any]]] = None
  282. __validators__: ClassVar[Dict[str, AnyCallable]] = {}
  283. __pre_root_validators__: ClassVar[List[AnyCallable]]
  284. __post_root_validators__: ClassVar[List[Tuple[bool, AnyCallable]]]
  285. __config__: ClassVar[Type[BaseConfig]] = BaseConfig
  286. __json_encoder__: ClassVar[Callable[[Any], Any]] = lambda x: x
  287. __schema_cache__: ClassVar['DictAny'] = {}
  288. __custom_root_type__: ClassVar[bool] = False
  289. __signature__: ClassVar['Signature']
  290. __private_attributes__: ClassVar[Dict[str, ModelPrivateAttr]]
  291. __class_vars__: ClassVar[SetStr]
  292. __fields_set__: ClassVar[SetStr] = set()
  293. Config = BaseConfig
  294. __slots__ = ('__dict__', '__fields_set__')
  295. __doc__ = '' # Null out the Representation docstring
  296. def __init__(__pydantic_self__, **data: Any) -> None:
  297. """
  298. Create a new model by parsing and validating input data from keyword arguments.
  299. Raises ValidationError if the input data cannot be parsed to form a valid model.
  300. """
  301. # Uses something other than `self` the first arg to allow "self" as a settable attribute
  302. values, fields_set, validation_error = validate_model(__pydantic_self__.__class__, data)
  303. if validation_error:
  304. raise validation_error
  305. try:
  306. object_setattr(__pydantic_self__, '__dict__', values)
  307. except TypeError as e:
  308. raise TypeError(
  309. 'Model values must be a dict; you may not have returned a dictionary from a root validator'
  310. ) from e
  311. object_setattr(__pydantic_self__, '__fields_set__', fields_set)
  312. __pydantic_self__._init_private_attributes()
  313. @no_type_check
  314. def __setattr__(self, name, value): # noqa: C901 (ignore complexity)
  315. if name in self.__private_attributes__ or name in DUNDER_ATTRIBUTES:
  316. return object_setattr(self, name, value)
  317. if self.__config__.extra is not Extra.allow and name not in self.__fields__:
  318. raise ValueError(f'"{self.__class__.__name__}" object has no field "{name}"')
  319. elif not self.__config__.allow_mutation or self.__config__.frozen:
  320. raise TypeError(f'"{self.__class__.__name__}" is immutable and does not support item assignment')
  321. elif name in self.__fields__ and self.__fields__[name].final:
  322. raise TypeError(
  323. f'"{self.__class__.__name__}" object "{name}" field is final and does not support reassignment'
  324. )
  325. elif self.__config__.validate_assignment:
  326. new_values = {**self.__dict__, name: value}
  327. for validator in self.__pre_root_validators__:
  328. try:
  329. new_values = validator(self.__class__, new_values)
  330. except (ValueError, TypeError, AssertionError) as exc:
  331. raise ValidationError([ErrorWrapper(exc, loc=ROOT_KEY)], self.__class__)
  332. known_field = self.__fields__.get(name, None)
  333. if known_field:
  334. # We want to
  335. # - make sure validators are called without the current value for this field inside `values`
  336. # - keep other values (e.g. submodels) untouched (using `BaseModel.dict()` will change them into dicts)
  337. # - keep the order of the fields
  338. if not known_field.field_info.allow_mutation:
  339. raise TypeError(f'"{known_field.name}" has allow_mutation set to False and cannot be assigned')
  340. dict_without_original_value = {k: v for k, v in self.__dict__.items() if k != name}
  341. value, error_ = known_field.validate(value, dict_without_original_value, loc=name, cls=self.__class__)
  342. if error_:
  343. raise ValidationError([error_], self.__class__)
  344. else:
  345. new_values[name] = value
  346. errors = []
  347. for skip_on_failure, validator in self.__post_root_validators__:
  348. if skip_on_failure and errors:
  349. continue
  350. try:
  351. new_values = validator(self.__class__, new_values)
  352. except (ValueError, TypeError, AssertionError) as exc:
  353. errors.append(ErrorWrapper(exc, loc=ROOT_KEY))
  354. if errors:
  355. raise ValidationError(errors, self.__class__)
  356. # update the whole __dict__ as other values than just `value`
  357. # may be changed (e.g. with `root_validator`)
  358. object_setattr(self, '__dict__', new_values)
  359. else:
  360. self.__dict__[name] = value
  361. self.__fields_set__.add(name)
  362. def __getstate__(self) -> 'DictAny':
  363. private_attrs = ((k, getattr(self, k, Undefined)) for k in self.__private_attributes__)
  364. return {
  365. '__dict__': self.__dict__,
  366. '__fields_set__': self.__fields_set__,
  367. '__private_attribute_values__': {k: v for k, v in private_attrs if v is not Undefined},
  368. }
  369. def __setstate__(self, state: 'DictAny') -> None:
  370. object_setattr(self, '__dict__', state['__dict__'])
  371. object_setattr(self, '__fields_set__', state['__fields_set__'])
  372. for name, value in state.get('__private_attribute_values__', {}).items():
  373. object_setattr(self, name, value)
  374. def _init_private_attributes(self) -> None:
  375. for name, private_attr in self.__private_attributes__.items():
  376. default = private_attr.get_default()
  377. if default is not Undefined:
  378. object_setattr(self, name, default)
  379. def dict(
  380. self,
  381. *,
  382. include: Optional[Union['AbstractSetIntStr', 'MappingIntStrAny']] = None,
  383. exclude: Optional[Union['AbstractSetIntStr', 'MappingIntStrAny']] = None,
  384. by_alias: bool = False,
  385. skip_defaults: Optional[bool] = None,
  386. exclude_unset: bool = False,
  387. exclude_defaults: bool = False,
  388. exclude_none: bool = False,
  389. ) -> 'DictStrAny':
  390. """
  391. Generate a dictionary representation of the model, optionally specifying which fields to include or exclude.
  392. """
  393. if skip_defaults is not None:
  394. warnings.warn(
  395. f'{self.__class__.__name__}.dict(): "skip_defaults" is deprecated and replaced by "exclude_unset"',
  396. DeprecationWarning,
  397. )
  398. exclude_unset = skip_defaults
  399. return dict(
  400. self._iter(
  401. to_dict=True,
  402. by_alias=by_alias,
  403. include=include,
  404. exclude=exclude,
  405. exclude_unset=exclude_unset,
  406. exclude_defaults=exclude_defaults,
  407. exclude_none=exclude_none,
  408. )
  409. )
  410. def json(
  411. self,
  412. *,
  413. include: Optional[Union['AbstractSetIntStr', 'MappingIntStrAny']] = None,
  414. exclude: Optional[Union['AbstractSetIntStr', 'MappingIntStrAny']] = None,
  415. by_alias: bool = False,
  416. skip_defaults: Optional[bool] = None,
  417. exclude_unset: bool = False,
  418. exclude_defaults: bool = False,
  419. exclude_none: bool = False,
  420. encoder: Optional[Callable[[Any], Any]] = None,
  421. models_as_dict: bool = True,
  422. **dumps_kwargs: Any,
  423. ) -> str:
  424. """
  425. Generate a JSON representation of the model, `include` and `exclude` arguments as per `dict()`.
  426. `encoder` is an optional function to supply as `default` to json.dumps(), other arguments as per `json.dumps()`.
  427. """
  428. if skip_defaults is not None:
  429. warnings.warn(
  430. f'{self.__class__.__name__}.json(): "skip_defaults" is deprecated and replaced by "exclude_unset"',
  431. DeprecationWarning,
  432. )
  433. exclude_unset = skip_defaults
  434. encoder = cast(Callable[[Any], Any], encoder or self.__json_encoder__)
  435. # We don't directly call `self.dict()`, which does exactly this with `to_dict=True`
  436. # because we want to be able to keep raw `BaseModel` instances and not as `dict`.
  437. # This allows users to write custom JSON encoders for given `BaseModel` classes.
  438. data = dict(
  439. self._iter(
  440. to_dict=models_as_dict,
  441. by_alias=by_alias,
  442. include=include,
  443. exclude=exclude,
  444. exclude_unset=exclude_unset,
  445. exclude_defaults=exclude_defaults,
  446. exclude_none=exclude_none,
  447. )
  448. )
  449. if self.__custom_root_type__:
  450. data = data[ROOT_KEY]
  451. return self.__config__.json_dumps(data, default=encoder, **dumps_kwargs)
  452. @classmethod
  453. def _enforce_dict_if_root(cls, obj: Any) -> Any:
  454. if cls.__custom_root_type__ and (
  455. not (isinstance(obj, dict) and obj.keys() == {ROOT_KEY})
  456. and not (isinstance(obj, BaseModel) and obj.__fields__.keys() == {ROOT_KEY})
  457. or cls.__fields__[ROOT_KEY].shape in MAPPING_LIKE_SHAPES
  458. ):
  459. return {ROOT_KEY: obj}
  460. else:
  461. return obj
  462. @classmethod
  463. def parse_obj(cls: Type['Model'], obj: Any) -> 'Model':
  464. obj = cls._enforce_dict_if_root(obj)
  465. if not isinstance(obj, dict):
  466. try:
  467. obj = dict(obj)
  468. except (TypeError, ValueError) as e:
  469. exc = TypeError(f'{cls.__name__} expected dict not {obj.__class__.__name__}')
  470. raise ValidationError([ErrorWrapper(exc, loc=ROOT_KEY)], cls) from e
  471. return cls(**obj)
  472. @classmethod
  473. def parse_raw(
  474. cls: Type['Model'],
  475. b: StrBytes,
  476. *,
  477. content_type: str = None,
  478. encoding: str = 'utf8',
  479. proto: Protocol = None,
  480. allow_pickle: bool = False,
  481. ) -> 'Model':
  482. try:
  483. obj = load_str_bytes(
  484. b,
  485. proto=proto,
  486. content_type=content_type,
  487. encoding=encoding,
  488. allow_pickle=allow_pickle,
  489. json_loads=cls.__config__.json_loads,
  490. )
  491. except (ValueError, TypeError, UnicodeDecodeError) as e:
  492. raise ValidationError([ErrorWrapper(e, loc=ROOT_KEY)], cls)
  493. return cls.parse_obj(obj)
  494. @classmethod
  495. def parse_file(
  496. cls: Type['Model'],
  497. path: Union[str, Path],
  498. *,
  499. content_type: str = None,
  500. encoding: str = 'utf8',
  501. proto: Protocol = None,
  502. allow_pickle: bool = False,
  503. ) -> 'Model':
  504. obj = load_file(
  505. path,
  506. proto=proto,
  507. content_type=content_type,
  508. encoding=encoding,
  509. allow_pickle=allow_pickle,
  510. json_loads=cls.__config__.json_loads,
  511. )
  512. return cls.parse_obj(obj)
  513. @classmethod
  514. def from_orm(cls: Type['Model'], obj: Any) -> 'Model':
  515. if not cls.__config__.orm_mode:
  516. raise ConfigError('You must have the config attribute orm_mode=True to use from_orm')
  517. obj = {ROOT_KEY: obj} if cls.__custom_root_type__ else cls._decompose_class(obj)
  518. m = cls.__new__(cls)
  519. values, fields_set, validation_error = validate_model(cls, obj)
  520. if validation_error:
  521. raise validation_error
  522. object_setattr(m, '__dict__', values)
  523. object_setattr(m, '__fields_set__', fields_set)
  524. m._init_private_attributes()
  525. return m
  526. @classmethod
  527. def construct(cls: Type['Model'], _fields_set: Optional['SetStr'] = None, **values: Any) -> 'Model':
  528. """
  529. Creates a new model setting __dict__ and __fields_set__ from trusted or pre-validated data.
  530. Default values are respected, but no other validation is performed.
  531. Behaves as if `Config.extra = 'allow'` was set since it adds all passed values
  532. """
  533. m = cls.__new__(cls)
  534. fields_values: Dict[str, Any] = {}
  535. for name, field in cls.__fields__.items():
  536. if field.alt_alias and field.alias in values:
  537. fields_values[name] = values[field.alias]
  538. elif name in values:
  539. fields_values[name] = values[name]
  540. elif not field.required:
  541. fields_values[name] = field.get_default()
  542. fields_values.update(values)
  543. object_setattr(m, '__dict__', fields_values)
  544. if _fields_set is None:
  545. _fields_set = set(values.keys())
  546. object_setattr(m, '__fields_set__', _fields_set)
  547. m._init_private_attributes()
  548. return m
  549. def _copy_and_set_values(self: 'Model', values: 'DictStrAny', fields_set: 'SetStr', *, deep: bool) -> 'Model':
  550. if deep:
  551. # chances of having empty dict here are quite low for using smart_deepcopy
  552. values = deepcopy(values)
  553. cls = self.__class__
  554. m = cls.__new__(cls)
  555. object_setattr(m, '__dict__', values)
  556. object_setattr(m, '__fields_set__', fields_set)
  557. for name in self.__private_attributes__:
  558. value = getattr(self, name, Undefined)
  559. if value is not Undefined:
  560. if deep:
  561. value = deepcopy(value)
  562. object_setattr(m, name, value)
  563. return m
  564. def copy(
  565. self: 'Model',
  566. *,
  567. include: Optional[Union['AbstractSetIntStr', 'MappingIntStrAny']] = None,
  568. exclude: Optional[Union['AbstractSetIntStr', 'MappingIntStrAny']] = None,
  569. update: Optional['DictStrAny'] = None,
  570. deep: bool = False,
  571. ) -> 'Model':
  572. """
  573. Duplicate a model, optionally choose which fields to include, exclude and change.
  574. :param include: fields to include in new model
  575. :param exclude: fields to exclude from new model, as with values this takes precedence over include
  576. :param update: values to change/add in the new model. Note: the data is not validated before creating
  577. the new model: you should trust this data
  578. :param deep: set to `True` to make a deep copy of the model
  579. :return: new model instance
  580. """
  581. values = dict(
  582. self._iter(to_dict=False, by_alias=False, include=include, exclude=exclude, exclude_unset=False),
  583. **(update or {}),
  584. )
  585. # new `__fields_set__` can have unset optional fields with a set value in `update` kwarg
  586. if update:
  587. fields_set = self.__fields_set__ | update.keys()
  588. else:
  589. fields_set = set(self.__fields_set__)
  590. return self._copy_and_set_values(values, fields_set, deep=deep)
  591. @classmethod
  592. def schema(cls, by_alias: bool = True, ref_template: str = default_ref_template) -> 'DictStrAny':
  593. cached = cls.__schema_cache__.get((by_alias, ref_template))
  594. if cached is not None:
  595. return cached
  596. s = model_schema(cls, by_alias=by_alias, ref_template=ref_template)
  597. cls.__schema_cache__[(by_alias, ref_template)] = s
  598. return s
  599. @classmethod
  600. def schema_json(
  601. cls, *, by_alias: bool = True, ref_template: str = default_ref_template, **dumps_kwargs: Any
  602. ) -> str:
  603. from pydantic.v1.json import pydantic_encoder
  604. return cls.__config__.json_dumps(
  605. cls.schema(by_alias=by_alias, ref_template=ref_template), default=pydantic_encoder, **dumps_kwargs
  606. )
  607. @classmethod
  608. def __get_validators__(cls) -> 'CallableGenerator':
  609. yield cls.validate
  610. @classmethod
  611. def validate(cls: Type['Model'], value: Any) -> 'Model':
  612. if isinstance(value, cls):
  613. copy_on_model_validation = cls.__config__.copy_on_model_validation
  614. # whether to deep or shallow copy the model on validation, None means do not copy
  615. deep_copy: Optional[bool] = None
  616. if copy_on_model_validation not in {'deep', 'shallow', 'none'}:
  617. # Warn about deprecated behavior
  618. warnings.warn(
  619. "`copy_on_model_validation` should be a string: 'deep', 'shallow' or 'none'", DeprecationWarning
  620. )
  621. if copy_on_model_validation:
  622. deep_copy = False
  623. if copy_on_model_validation == 'shallow':
  624. # shallow copy
  625. deep_copy = False
  626. elif copy_on_model_validation == 'deep':
  627. # deep copy
  628. deep_copy = True
  629. if deep_copy is None:
  630. return value
  631. else:
  632. return value._copy_and_set_values(value.__dict__, value.__fields_set__, deep=deep_copy)
  633. value = cls._enforce_dict_if_root(value)
  634. if isinstance(value, dict):
  635. return cls(**value)
  636. elif cls.__config__.orm_mode:
  637. return cls.from_orm(value)
  638. else:
  639. try:
  640. value_as_dict = dict(value)
  641. except (TypeError, ValueError) as e:
  642. raise DictError() from e
  643. return cls(**value_as_dict)
  644. @classmethod
  645. def _decompose_class(cls: Type['Model'], obj: Any) -> GetterDict:
  646. if isinstance(obj, GetterDict):
  647. return obj
  648. return cls.__config__.getter_dict(obj)
  649. @classmethod
  650. @no_type_check
  651. def _get_value(
  652. cls,
  653. v: Any,
  654. to_dict: bool,
  655. by_alias: bool,
  656. include: Optional[Union['AbstractSetIntStr', 'MappingIntStrAny']],
  657. exclude: Optional[Union['AbstractSetIntStr', 'MappingIntStrAny']],
  658. exclude_unset: bool,
  659. exclude_defaults: bool,
  660. exclude_none: bool,
  661. ) -> Any:
  662. if isinstance(v, BaseModel):
  663. if to_dict:
  664. v_dict = v.dict(
  665. by_alias=by_alias,
  666. exclude_unset=exclude_unset,
  667. exclude_defaults=exclude_defaults,
  668. include=include,
  669. exclude=exclude,
  670. exclude_none=exclude_none,
  671. )
  672. if ROOT_KEY in v_dict:
  673. return v_dict[ROOT_KEY]
  674. return v_dict
  675. else:
  676. return v.copy(include=include, exclude=exclude)
  677. value_exclude = ValueItems(v, exclude) if exclude else None
  678. value_include = ValueItems(v, include) if include else None
  679. if isinstance(v, dict):
  680. return {
  681. k_: cls._get_value(
  682. v_,
  683. to_dict=to_dict,
  684. by_alias=by_alias,
  685. exclude_unset=exclude_unset,
  686. exclude_defaults=exclude_defaults,
  687. include=value_include and value_include.for_element(k_),
  688. exclude=value_exclude and value_exclude.for_element(k_),
  689. exclude_none=exclude_none,
  690. )
  691. for k_, v_ in v.items()
  692. if (not value_exclude or not value_exclude.is_excluded(k_))
  693. and (not value_include or value_include.is_included(k_))
  694. }
  695. elif sequence_like(v):
  696. seq_args = (
  697. cls._get_value(
  698. v_,
  699. to_dict=to_dict,
  700. by_alias=by_alias,
  701. exclude_unset=exclude_unset,
  702. exclude_defaults=exclude_defaults,
  703. include=value_include and value_include.for_element(i),
  704. exclude=value_exclude and value_exclude.for_element(i),
  705. exclude_none=exclude_none,
  706. )
  707. for i, v_ in enumerate(v)
  708. if (not value_exclude or not value_exclude.is_excluded(i))
  709. and (not value_include or value_include.is_included(i))
  710. )
  711. return v.__class__(*seq_args) if is_namedtuple(v.__class__) else v.__class__(seq_args)
  712. elif isinstance(v, Enum) and getattr(cls.Config, 'use_enum_values', False):
  713. return v.value
  714. else:
  715. return v
  716. @classmethod
  717. def __try_update_forward_refs__(cls, **localns: Any) -> None:
  718. """
  719. Same as update_forward_refs but will not raise exception
  720. when forward references are not defined.
  721. """
  722. update_model_forward_refs(cls, cls.__fields__.values(), cls.__config__.json_encoders, localns, (NameError,))
  723. @classmethod
  724. def update_forward_refs(cls, **localns: Any) -> None:
  725. """
  726. Try to update ForwardRefs on fields based on this Model, globalns and localns.
  727. """
  728. update_model_forward_refs(cls, cls.__fields__.values(), cls.__config__.json_encoders, localns)
  729. def __iter__(self) -> 'TupleGenerator':
  730. """
  731. so `dict(model)` works
  732. """
  733. yield from self.__dict__.items()
  734. def _iter(
  735. self,
  736. to_dict: bool = False,
  737. by_alias: bool = False,
  738. include: Optional[Union['AbstractSetIntStr', 'MappingIntStrAny']] = None,
  739. exclude: Optional[Union['AbstractSetIntStr', 'MappingIntStrAny']] = None,
  740. exclude_unset: bool = False,
  741. exclude_defaults: bool = False,
  742. exclude_none: bool = False,
  743. ) -> 'TupleGenerator':
  744. # Merge field set excludes with explicit exclude parameter with explicit overriding field set options.
  745. # The extra "is not None" guards are not logically necessary but optimizes performance for the simple case.
  746. if exclude is not None or self.__exclude_fields__ is not None:
  747. exclude = ValueItems.merge(self.__exclude_fields__, exclude)
  748. if include is not None or self.__include_fields__ is not None:
  749. include = ValueItems.merge(self.__include_fields__, include, intersect=True)
  750. allowed_keys = self._calculate_keys(
  751. include=include, exclude=exclude, exclude_unset=exclude_unset # type: ignore
  752. )
  753. if allowed_keys is None and not (to_dict or by_alias or exclude_unset or exclude_defaults or exclude_none):
  754. # huge boost for plain _iter()
  755. yield from self.__dict__.items()
  756. return
  757. value_exclude = ValueItems(self, exclude) if exclude is not None else None
  758. value_include = ValueItems(self, include) if include is not None else None
  759. for field_key, v in self.__dict__.items():
  760. if (allowed_keys is not None and field_key not in allowed_keys) or (exclude_none and v is None):
  761. continue
  762. if exclude_defaults:
  763. model_field = self.__fields__.get(field_key)
  764. if not getattr(model_field, 'required', True) and getattr(model_field, 'default', _missing) == v:
  765. continue
  766. if by_alias and field_key in self.__fields__:
  767. dict_key = self.__fields__[field_key].alias
  768. else:
  769. dict_key = field_key
  770. if to_dict or value_include or value_exclude:
  771. v = self._get_value(
  772. v,
  773. to_dict=to_dict,
  774. by_alias=by_alias,
  775. include=value_include and value_include.for_element(field_key),
  776. exclude=value_exclude and value_exclude.for_element(field_key),
  777. exclude_unset=exclude_unset,
  778. exclude_defaults=exclude_defaults,
  779. exclude_none=exclude_none,
  780. )
  781. yield dict_key, v
  782. def _calculate_keys(
  783. self,
  784. include: Optional['MappingIntStrAny'],
  785. exclude: Optional['MappingIntStrAny'],
  786. exclude_unset: bool,
  787. update: Optional['DictStrAny'] = None,
  788. ) -> Optional[AbstractSet[str]]:
  789. if include is None and exclude is None and exclude_unset is False:
  790. return None
  791. keys: AbstractSet[str]
  792. if exclude_unset:
  793. keys = self.__fields_set__.copy()
  794. else:
  795. keys = self.__dict__.keys()
  796. if include is not None:
  797. keys &= include.keys()
  798. if update:
  799. keys -= update.keys()
  800. if exclude:
  801. keys -= {k for k, v in exclude.items() if ValueItems.is_true(v)}
  802. return keys
  803. def __eq__(self, other: Any) -> bool:
  804. if isinstance(other, BaseModel):
  805. return self.dict() == other.dict()
  806. else:
  807. return self.dict() == other
  808. def __repr_args__(self) -> 'ReprArgs':
  809. return [
  810. (k, v)
  811. for k, v in self.__dict__.items()
  812. if k not in DUNDER_ATTRIBUTES and (k not in self.__fields__ or self.__fields__[k].field_info.repr)
  813. ]
  814. _is_base_model_class_defined = True
  815. @overload
  816. def create_model(
  817. __model_name: str,
  818. *,
  819. __config__: Optional[Type[BaseConfig]] = None,
  820. __base__: None = None,
  821. __module__: str = __name__,
  822. __validators__: Dict[str, 'AnyClassMethod'] = None,
  823. __cls_kwargs__: Dict[str, Any] = None,
  824. **field_definitions: Any,
  825. ) -> Type['BaseModel']:
  826. ...
  827. @overload
  828. def create_model(
  829. __model_name: str,
  830. *,
  831. __config__: Optional[Type[BaseConfig]] = None,
  832. __base__: Union[Type['Model'], Tuple[Type['Model'], ...]],
  833. __module__: str = __name__,
  834. __validators__: Dict[str, 'AnyClassMethod'] = None,
  835. __cls_kwargs__: Dict[str, Any] = None,
  836. **field_definitions: Any,
  837. ) -> Type['Model']:
  838. ...
  839. def create_model(
  840. __model_name: str,
  841. *,
  842. __config__: Optional[Type[BaseConfig]] = None,
  843. __base__: Union[None, Type['Model'], Tuple[Type['Model'], ...]] = None,
  844. __module__: str = __name__,
  845. __validators__: Dict[str, 'AnyClassMethod'] = None,
  846. __cls_kwargs__: Dict[str, Any] = None,
  847. __slots__: Optional[Tuple[str, ...]] = None,
  848. **field_definitions: Any,
  849. ) -> Type['Model']:
  850. """
  851. Dynamically create a model.
  852. :param __model_name: name of the created model
  853. :param __config__: config class to use for the new model
  854. :param __base__: base class for the new model to inherit from
  855. :param __module__: module of the created model
  856. :param __validators__: a dict of method names and @validator class methods
  857. :param __cls_kwargs__: a dict for class creation
  858. :param __slots__: Deprecated, `__slots__` should not be passed to `create_model`
  859. :param field_definitions: fields of the model (or extra fields if a base is supplied)
  860. in the format `<name>=(<type>, <default default>)` or `<name>=<default value>, e.g.
  861. `foobar=(str, ...)` or `foobar=123`, or, for complex use-cases, in the format
  862. `<name>=<Field>` or `<name>=(<type>, <FieldInfo>)`, e.g.
  863. `foo=Field(datetime, default_factory=datetime.utcnow, alias='bar')` or
  864. `foo=(str, FieldInfo(title='Foo'))`
  865. """
  866. if __slots__ is not None:
  867. # __slots__ will be ignored from here on
  868. warnings.warn('__slots__ should not be passed to create_model', RuntimeWarning)
  869. if __base__ is not None:
  870. if __config__ is not None:
  871. raise ConfigError('to avoid confusion __config__ and __base__ cannot be used together')
  872. if not isinstance(__base__, tuple):
  873. __base__ = (__base__,)
  874. else:
  875. __base__ = (cast(Type['Model'], BaseModel),)
  876. __cls_kwargs__ = __cls_kwargs__ or {}
  877. fields = {}
  878. annotations = {}
  879. for f_name, f_def in field_definitions.items():
  880. if not is_valid_field(f_name):
  881. warnings.warn(f'fields may not start with an underscore, ignoring "{f_name}"', RuntimeWarning)
  882. if isinstance(f_def, tuple):
  883. try:
  884. f_annotation, f_value = f_def
  885. except ValueError as e:
  886. raise ConfigError(
  887. 'field definitions should either be a tuple of (<type>, <default>) or just a '
  888. 'default value, unfortunately this means tuples as '
  889. 'default values are not allowed'
  890. ) from e
  891. else:
  892. f_annotation, f_value = None, f_def
  893. if f_annotation:
  894. annotations[f_name] = f_annotation
  895. fields[f_name] = f_value
  896. namespace: 'DictStrAny' = {'__annotations__': annotations, '__module__': __module__}
  897. if __validators__:
  898. namespace.update(__validators__)
  899. namespace.update(fields)
  900. if __config__:
  901. namespace['Config'] = inherit_config(__config__, BaseConfig)
  902. resolved_bases = resolve_bases(__base__)
  903. meta, ns, kwds = prepare_class(__model_name, resolved_bases, kwds=__cls_kwargs__)
  904. if resolved_bases is not __base__:
  905. ns['__orig_bases__'] = __base__
  906. namespace.update(ns)
  907. return meta(__model_name, resolved_bases, namespace, **kwds)
  908. _missing = object()
  909. def validate_model( # noqa: C901 (ignore complexity)
  910. model: Type[BaseModel], input_data: 'DictStrAny', cls: 'ModelOrDc' = None
  911. ) -> Tuple['DictStrAny', 'SetStr', Optional[ValidationError]]:
  912. """
  913. validate data against a model.
  914. """
  915. values = {}
  916. errors = []
  917. # input_data names, possibly alias
  918. names_used = set()
  919. # field names, never aliases
  920. fields_set = set()
  921. config = model.__config__
  922. check_extra = config.extra is not Extra.ignore
  923. cls_ = cls or model
  924. for validator in model.__pre_root_validators__:
  925. try:
  926. input_data = validator(cls_, input_data)
  927. except (ValueError, TypeError, AssertionError) as exc:
  928. return {}, set(), ValidationError([ErrorWrapper(exc, loc=ROOT_KEY)], cls_)
  929. for name, field in model.__fields__.items():
  930. value = input_data.get(field.alias, _missing)
  931. using_name = False
  932. if value is _missing and config.allow_population_by_field_name and field.alt_alias:
  933. value = input_data.get(field.name, _missing)
  934. using_name = True
  935. if value is _missing:
  936. if field.required:
  937. errors.append(ErrorWrapper(MissingError(), loc=field.alias))
  938. continue
  939. value = field.get_default()
  940. if not config.validate_all and not field.validate_always:
  941. values[name] = value
  942. continue
  943. else:
  944. fields_set.add(name)
  945. if check_extra:
  946. names_used.add(field.name if using_name else field.alias)
  947. v_, errors_ = field.validate(value, values, loc=field.alias, cls=cls_)
  948. if isinstance(errors_, ErrorWrapper):
  949. errors.append(errors_)
  950. elif isinstance(errors_, list):
  951. errors.extend(errors_)
  952. else:
  953. values[name] = v_
  954. if check_extra:
  955. if isinstance(input_data, GetterDict):
  956. extra = input_data.extra_keys() - names_used
  957. else:
  958. extra = input_data.keys() - names_used
  959. if extra:
  960. fields_set |= extra
  961. if config.extra is Extra.allow:
  962. for f in extra:
  963. values[f] = input_data[f]
  964. else:
  965. for f in sorted(extra):
  966. errors.append(ErrorWrapper(ExtraError(), loc=f))
  967. for skip_on_failure, validator in model.__post_root_validators__:
  968. if skip_on_failure and errors:
  969. continue
  970. try:
  971. values = validator(cls_, values)
  972. except (ValueError, TypeError, AssertionError) as exc:
  973. errors.append(ErrorWrapper(exc, loc=ROOT_KEY))
  974. if errors:
  975. return values, fields_set, ValidationError(errors, cls_)
  976. else:
  977. return values, fields_set, None