_model_construction.py 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  1. """Private logic for creating models."""
  2. from __future__ import annotations as _annotations
  3. import operator
  4. import sys
  5. import typing
  6. import warnings
  7. import weakref
  8. from abc import ABCMeta
  9. from functools import cache, partial, wraps
  10. from types import FunctionType
  11. from typing import TYPE_CHECKING, Any, Callable, Generic, Literal, NoReturn, TypeVar, cast
  12. from pydantic_core import PydanticUndefined, SchemaSerializer
  13. from typing_extensions import TypeAliasType, dataclass_transform, deprecated, get_args, get_origin
  14. from typing_inspection import typing_objects
  15. from ..errors import PydanticUndefinedAnnotation, PydanticUserError
  16. from ..plugin._schema_validator import create_schema_validator
  17. from ..warnings import GenericBeforeBaseModelWarning, PydanticDeprecatedSince20
  18. from ._config import ConfigWrapper
  19. from ._decorators import DecoratorInfos, PydanticDescriptorProxy, get_attribute_from_bases, unwrap_wrapped_function
  20. from ._fields import collect_model_fields, is_valid_field_name, is_valid_privateattr_name, rebuild_model_fields
  21. from ._generate_schema import GenerateSchema, InvalidSchemaError
  22. from ._generics import PydanticGenericMetadata, get_model_typevars_map
  23. from ._import_utils import import_cached_base_model, import_cached_field_info
  24. from ._mock_val_ser import set_model_mocks
  25. from ._namespace_utils import NsResolver
  26. from ._signature import generate_pydantic_signature
  27. from ._typing_extra import (
  28. _make_forward_ref,
  29. eval_type_backport,
  30. is_classvar_annotation,
  31. parent_frame_namespace,
  32. )
  33. from ._utils import LazyClassAttribute, SafeGetItemProxy
  34. if TYPE_CHECKING:
  35. from ..fields import Field as PydanticModelField
  36. from ..fields import FieldInfo, ModelPrivateAttr
  37. from ..fields import PrivateAttr as PydanticModelPrivateAttr
  38. from ..main import BaseModel
  39. else:
  40. PydanticModelField = object()
  41. PydanticModelPrivateAttr = object()
  42. object_setattr = object.__setattr__
  43. class _ModelNamespaceDict(dict):
  44. """A dictionary subclass that intercepts attribute setting on model classes and
  45. warns about overriding of decorators.
  46. """
  47. def __setitem__(self, k: str, v: object) -> None:
  48. existing: Any = self.get(k, None)
  49. if existing and v is not existing and isinstance(existing, PydanticDescriptorProxy):
  50. warnings.warn(
  51. f'`{k}` overrides an existing Pydantic `{existing.decorator_info.decorator_repr}` decorator',
  52. stacklevel=2,
  53. )
  54. return super().__setitem__(k, v)
  55. def NoInitField(
  56. *,
  57. init: Literal[False] = False,
  58. ) -> Any:
  59. """Only for typing purposes. Used as default value of `__pydantic_fields_set__`,
  60. `__pydantic_extra__`, `__pydantic_private__`, so they could be ignored when
  61. synthesizing the `__init__` signature.
  62. """
  63. # For ModelMetaclass.register():
  64. _T = TypeVar('_T')
  65. @dataclass_transform(kw_only_default=True, field_specifiers=(PydanticModelField, PydanticModelPrivateAttr, NoInitField))
  66. class ModelMetaclass(ABCMeta):
  67. def __new__(
  68. mcs,
  69. cls_name: str,
  70. bases: tuple[type[Any], ...],
  71. namespace: dict[str, Any],
  72. __pydantic_generic_metadata__: PydanticGenericMetadata | None = None,
  73. __pydantic_reset_parent_namespace__: bool = True,
  74. _create_model_module: str | None = None,
  75. **kwargs: Any,
  76. ) -> type:
  77. """Metaclass for creating Pydantic models.
  78. Args:
  79. cls_name: The name of the class to be created.
  80. bases: The base classes of the class to be created.
  81. namespace: The attribute dictionary of the class to be created.
  82. __pydantic_generic_metadata__: Metadata for generic models.
  83. __pydantic_reset_parent_namespace__: Reset parent namespace.
  84. _create_model_module: The module of the class to be created, if created by `create_model`.
  85. **kwargs: Catch-all for any other keyword arguments.
  86. Returns:
  87. The new class created by the metaclass.
  88. """
  89. # Note `ModelMetaclass` refers to `BaseModel`, but is also used to *create* `BaseModel`, so we rely on the fact
  90. # that `BaseModel` itself won't have any bases, but any subclass of it will, to determine whether the `__new__`
  91. # call we're in the middle of is for the `BaseModel` class.
  92. if bases:
  93. raw_annotations: dict[str, Any]
  94. if sys.version_info >= (3, 14):
  95. if (
  96. '__annotations__' in namespace
  97. ): # `from __future__ import annotations` was used in the model's module
  98. raw_annotations = namespace['__annotations__']
  99. else:
  100. # See https://docs.python.org/3.14/library/annotationlib.html#using-annotations-in-a-metaclass:
  101. from annotationlib import Format, call_annotate_function, get_annotate_from_class_namespace
  102. if annotate := get_annotate_from_class_namespace(namespace):
  103. raw_annotations = call_annotate_function(annotate, format=Format.FORWARDREF)
  104. else:
  105. raw_annotations = {}
  106. else:
  107. raw_annotations = namespace.get('__annotations__', {})
  108. base_field_names, class_vars, base_private_attributes = mcs._collect_bases_data(bases)
  109. config_wrapper = ConfigWrapper.for_model(bases, namespace, raw_annotations, kwargs)
  110. namespace['model_config'] = config_wrapper.config_dict
  111. private_attributes = inspect_namespace(
  112. namespace, raw_annotations, config_wrapper.ignored_types, class_vars, base_field_names
  113. )
  114. if private_attributes or base_private_attributes:
  115. original_model_post_init = get_model_post_init(namespace, bases)
  116. if original_model_post_init is not None:
  117. # if there are private_attributes and a model_post_init function, we handle both
  118. @wraps(original_model_post_init)
  119. def wrapped_model_post_init(self: BaseModel, context: Any, /) -> None:
  120. """We need to both initialize private attributes and call the user-defined model_post_init
  121. method.
  122. """
  123. init_private_attributes(self, context)
  124. original_model_post_init(self, context)
  125. namespace['model_post_init'] = wrapped_model_post_init
  126. else:
  127. namespace['model_post_init'] = init_private_attributes
  128. namespace['__class_vars__'] = class_vars
  129. namespace['__private_attributes__'] = {**base_private_attributes, **private_attributes}
  130. cls = cast('type[BaseModel]', super().__new__(mcs, cls_name, bases, namespace, **kwargs))
  131. BaseModel_ = import_cached_base_model()
  132. mro = cls.__mro__
  133. if Generic in mro and mro.index(Generic) < mro.index(BaseModel_):
  134. warnings.warn(
  135. GenericBeforeBaseModelWarning(
  136. 'Classes should inherit from `BaseModel` before generic classes (e.g. `typing.Generic[T]`) '
  137. 'for pydantic generics to work properly.'
  138. ),
  139. stacklevel=2,
  140. )
  141. cls.__pydantic_custom_init__ = not getattr(cls.__init__, '__pydantic_base_init__', False)
  142. cls.__pydantic_post_init__ = (
  143. None if cls.model_post_init is BaseModel_.model_post_init else 'model_post_init'
  144. )
  145. cls.__pydantic_setattr_handlers__ = {}
  146. cls.__pydantic_decorators__ = DecoratorInfos.build(cls)
  147. cls.__pydantic_decorators__.update_from_config(config_wrapper)
  148. # Use the getattr below to grab the __parameters__ from the `typing.Generic` parent class
  149. if __pydantic_generic_metadata__:
  150. cls.__pydantic_generic_metadata__ = __pydantic_generic_metadata__
  151. else:
  152. parent_parameters = getattr(cls, '__pydantic_generic_metadata__', {}).get('parameters', ())
  153. parameters = getattr(cls, '__parameters__', None) or parent_parameters
  154. if parameters and parent_parameters and not all(x in parameters for x in parent_parameters):
  155. from ..root_model import RootModelRootType
  156. missing_parameters = tuple(x for x in parameters if x not in parent_parameters)
  157. if RootModelRootType in parent_parameters and RootModelRootType not in parameters:
  158. # This is a special case where the user has subclassed `RootModel`, but has not parametrized
  159. # RootModel with the generic type identifiers being used. Ex:
  160. # class MyModel(RootModel, Generic[T]):
  161. # root: T
  162. # Should instead just be:
  163. # class MyModel(RootModel[T]):
  164. # root: T
  165. parameters_str = ', '.join([x.__name__ for x in missing_parameters])
  166. error_message = (
  167. f'{cls.__name__} is a subclass of `RootModel`, but does not include the generic type identifier(s) '
  168. f'{parameters_str} in its parameters. '
  169. f'You should parametrize RootModel directly, e.g., `class {cls.__name__}(RootModel[{parameters_str}]): ...`.'
  170. )
  171. else:
  172. combined_parameters = parent_parameters + missing_parameters
  173. parameters_str = ', '.join([str(x) for x in combined_parameters])
  174. generic_type_label = f'typing.Generic[{parameters_str}]'
  175. error_message = (
  176. f'All parameters must be present on typing.Generic;'
  177. f' you should inherit from {generic_type_label}.'
  178. )
  179. if Generic not in bases: # pragma: no cover
  180. # We raise an error here not because it is desirable, but because some cases are mishandled.
  181. # It would be nice to remove this error and still have things behave as expected, it's just
  182. # challenging because we are using a custom `__class_getitem__` to parametrize generic models,
  183. # and not returning a typing._GenericAlias from it.
  184. bases_str = ', '.join([x.__name__ for x in bases] + [generic_type_label])
  185. error_message += (
  186. f' Note: `typing.Generic` must go last: `class {cls.__name__}({bases_str}): ...`)'
  187. )
  188. raise TypeError(error_message)
  189. cls.__pydantic_generic_metadata__ = {
  190. 'origin': None,
  191. 'args': (),
  192. 'parameters': parameters,
  193. }
  194. cls.__pydantic_complete__ = False # Ensure this specific class gets completed
  195. # preserve `__set_name__` protocol defined in https://peps.python.org/pep-0487
  196. # for attributes not in `new_namespace` (e.g. private attributes)
  197. for name, obj in private_attributes.items():
  198. obj.__set_name__(cls, name)
  199. if __pydantic_reset_parent_namespace__:
  200. cls.__pydantic_parent_namespace__ = build_lenient_weakvaluedict(parent_frame_namespace())
  201. parent_namespace: dict[str, Any] | None = getattr(cls, '__pydantic_parent_namespace__', None)
  202. if isinstance(parent_namespace, dict):
  203. parent_namespace = unpack_lenient_weakvaluedict(parent_namespace)
  204. ns_resolver = NsResolver(parent_namespace=parent_namespace)
  205. set_model_fields(cls, config_wrapper=config_wrapper, ns_resolver=ns_resolver)
  206. # This is also set in `complete_model_class()`, after schema gen because they are recreated.
  207. # We set them here as well for backwards compatibility:
  208. cls.__pydantic_computed_fields__ = {
  209. k: v.info for k, v in cls.__pydantic_decorators__.computed_fields.items()
  210. }
  211. if config_wrapper.defer_build:
  212. set_model_mocks(cls)
  213. else:
  214. # Any operation that requires accessing the field infos instances should be put inside
  215. # `complete_model_class()`:
  216. complete_model_class(
  217. cls,
  218. config_wrapper,
  219. ns_resolver,
  220. raise_errors=False,
  221. create_model_module=_create_model_module,
  222. )
  223. if config_wrapper.frozen and '__hash__' not in namespace:
  224. set_default_hash_func(cls, bases)
  225. # using super(cls, cls) on the next line ensures we only call the parent class's __pydantic_init_subclass__
  226. # I believe the `type: ignore` is only necessary because mypy doesn't realize that this code branch is
  227. # only hit for _proper_ subclasses of BaseModel
  228. super(cls, cls).__pydantic_init_subclass__(**kwargs) # type: ignore[misc]
  229. return cls
  230. else:
  231. # These are instance variables, but have been assigned to `NoInitField` to trick the type checker.
  232. for instance_slot in '__pydantic_fields_set__', '__pydantic_extra__', '__pydantic_private__':
  233. namespace.pop(
  234. instance_slot,
  235. None, # In case the metaclass is used with a class other than `BaseModel`.
  236. )
  237. namespace.get('__annotations__', {}).clear()
  238. return super().__new__(mcs, cls_name, bases, namespace, **kwargs)
  239. if not TYPE_CHECKING: # pragma: no branch
  240. # We put `__getattr__` in a non-TYPE_CHECKING block because otherwise, mypy allows arbitrary attribute access
  241. def __getattr__(self, item: str) -> Any:
  242. """This is necessary to keep attribute access working for class attribute access."""
  243. private_attributes = self.__dict__.get('__private_attributes__')
  244. if private_attributes and item in private_attributes:
  245. return private_attributes[item]
  246. raise AttributeError(item)
  247. @classmethod
  248. def __prepare__(cls, *args: Any, **kwargs: Any) -> dict[str, object]:
  249. return _ModelNamespaceDict()
  250. # Due to performance and memory issues, in the ABCMeta.__subclasscheck__ implementation, we don't support
  251. # registered virtual subclasses. See https://github.com/python/cpython/issues/92810#issuecomment-2762454345.
  252. # This may change once the CPython gets fixed (possibly in 3.15), in which case we should conditionally
  253. # define `register()`.
  254. def register(self, subclass: type[_T]) -> type[_T]:
  255. warnings.warn(
  256. f"For performance reasons, virtual subclasses registered using '{self.__qualname__}.register()' "
  257. "are not supported in 'isinstance()' and 'issubclass()' checks.",
  258. stacklevel=2,
  259. )
  260. return super().register(subclass)
  261. __instancecheck__ = type.__instancecheck__ # pyright: ignore[reportAssignmentType]
  262. __subclasscheck__ = type.__subclasscheck__ # pyright: ignore[reportAssignmentType]
  263. @staticmethod
  264. def _collect_bases_data(bases: tuple[type[Any], ...]) -> tuple[set[str], set[str], dict[str, ModelPrivateAttr]]:
  265. BaseModel = import_cached_base_model()
  266. field_names: set[str] = set()
  267. class_vars: set[str] = set()
  268. private_attributes: dict[str, ModelPrivateAttr] = {}
  269. for base in bases:
  270. if issubclass(base, BaseModel) and base is not BaseModel:
  271. # model_fields might not be defined yet in the case of generics, so we use getattr here:
  272. field_names.update(getattr(base, '__pydantic_fields__', {}).keys())
  273. class_vars.update(base.__class_vars__)
  274. private_attributes.update(base.__private_attributes__)
  275. return field_names, class_vars, private_attributes
  276. @property
  277. @deprecated(
  278. 'The `__fields__` attribute is deprecated, use the `model_fields` class property instead.', category=None
  279. )
  280. def __fields__(self) -> dict[str, FieldInfo]:
  281. warnings.warn(
  282. 'The `__fields__` attribute is deprecated, use the `model_fields` class property instead.',
  283. PydanticDeprecatedSince20,
  284. stacklevel=2,
  285. )
  286. return getattr(self, '__pydantic_fields__', {})
  287. @property
  288. def __pydantic_fields_complete__(self) -> bool:
  289. """Whether the fields where successfully collected (i.e. type hints were successfully resolves).
  290. This is a private attribute, not meant to be used outside Pydantic.
  291. """
  292. if '__pydantic_fields__' not in self.__dict__:
  293. return False
  294. field_infos = cast('dict[str, FieldInfo]', self.__pydantic_fields__) # pyright: ignore[reportAttributeAccessIssue]
  295. return all(field_info._complete for field_info in field_infos.values())
  296. def __dir__(self) -> list[str]:
  297. attributes = list(super().__dir__())
  298. if '__fields__' in attributes:
  299. attributes.remove('__fields__')
  300. return attributes
  301. def init_private_attributes(self: BaseModel, context: Any, /) -> None:
  302. """This function is meant to behave like a BaseModel method to initialise private attributes.
  303. It takes context as an argument since that's what pydantic-core passes when calling it.
  304. Args:
  305. self: The BaseModel instance.
  306. context: The context.
  307. """
  308. if getattr(self, '__pydantic_private__', None) is None:
  309. pydantic_private = {}
  310. for name, private_attr in self.__private_attributes__.items():
  311. default = private_attr.get_default()
  312. if default is not PydanticUndefined:
  313. pydantic_private[name] = default
  314. object_setattr(self, '__pydantic_private__', pydantic_private)
  315. def get_model_post_init(namespace: dict[str, Any], bases: tuple[type[Any], ...]) -> Callable[..., Any] | None:
  316. """Get the `model_post_init` method from the namespace or the class bases, or `None` if not defined."""
  317. if 'model_post_init' in namespace:
  318. return namespace['model_post_init']
  319. BaseModel = import_cached_base_model()
  320. model_post_init = get_attribute_from_bases(bases, 'model_post_init')
  321. if model_post_init is not BaseModel.model_post_init:
  322. return model_post_init
  323. def inspect_namespace( # noqa C901
  324. namespace: dict[str, Any],
  325. raw_annotations: dict[str, Any],
  326. ignored_types: tuple[type[Any], ...],
  327. base_class_vars: set[str],
  328. base_class_fields: set[str],
  329. ) -> dict[str, ModelPrivateAttr]:
  330. """Iterate over the namespace and:
  331. * gather private attributes
  332. * check for items which look like fields but are not (e.g. have no annotation) and warn.
  333. Args:
  334. namespace: The attribute dictionary of the class to be created.
  335. raw_annotations: The (non-evaluated) annotations of the model.
  336. ignored_types: A tuple of ignore types.
  337. base_class_vars: A set of base class class variables.
  338. base_class_fields: A set of base class fields.
  339. Returns:
  340. A dict contains private attributes info.
  341. Raises:
  342. TypeError: If there is a `__root__` field in model.
  343. NameError: If private attribute name is invalid.
  344. PydanticUserError:
  345. - If a field does not have a type annotation.
  346. - If a field on base class was overridden by a non-annotated attribute.
  347. """
  348. from ..fields import ModelPrivateAttr, PrivateAttr
  349. FieldInfo = import_cached_field_info()
  350. all_ignored_types = ignored_types + default_ignored_types()
  351. private_attributes: dict[str, ModelPrivateAttr] = {}
  352. if '__root__' in raw_annotations or '__root__' in namespace:
  353. raise TypeError("To define root models, use `pydantic.RootModel` rather than a field called '__root__'")
  354. ignored_names: set[str] = set()
  355. for var_name, value in list(namespace.items()):
  356. if var_name == 'model_config' or var_name == '__pydantic_extra__':
  357. continue
  358. elif (
  359. isinstance(value, type)
  360. and value.__module__ == namespace['__module__']
  361. and '__qualname__' in namespace
  362. and value.__qualname__.startswith(f'{namespace["__qualname__"]}.')
  363. ):
  364. # `value` is a nested type defined in this namespace; don't error
  365. continue
  366. elif isinstance(value, all_ignored_types) or value.__class__.__module__ == 'functools':
  367. ignored_names.add(var_name)
  368. continue
  369. elif isinstance(value, ModelPrivateAttr):
  370. if var_name.startswith('__'):
  371. raise NameError(
  372. 'Private attributes must not use dunder names;'
  373. f' use a single underscore prefix instead of {var_name!r}.'
  374. )
  375. elif is_valid_field_name(var_name):
  376. raise NameError(
  377. 'Private attributes must not use valid field names;'
  378. f' use sunder names, e.g. {"_" + var_name!r} instead of {var_name!r}.'
  379. )
  380. private_attributes[var_name] = value
  381. del namespace[var_name]
  382. elif isinstance(value, FieldInfo) and not is_valid_field_name(var_name):
  383. suggested_name = var_name.lstrip('_') or 'my_field' # don't suggest '' for all-underscore name
  384. raise NameError(
  385. f'Fields must not use names with leading underscores;'
  386. f' e.g., use {suggested_name!r} instead of {var_name!r}.'
  387. )
  388. elif var_name.startswith('__'):
  389. continue
  390. elif is_valid_privateattr_name(var_name):
  391. if var_name not in raw_annotations or not is_classvar_annotation(raw_annotations[var_name]):
  392. private_attributes[var_name] = cast(ModelPrivateAttr, PrivateAttr(default=value))
  393. del namespace[var_name]
  394. elif var_name in base_class_vars:
  395. continue
  396. elif var_name not in raw_annotations:
  397. if var_name in base_class_fields:
  398. raise PydanticUserError(
  399. f'Field {var_name!r} defined on a base class was overridden by a non-annotated attribute. '
  400. f'All field definitions, including overrides, require a type annotation.',
  401. code='model-field-overridden',
  402. )
  403. elif isinstance(value, FieldInfo):
  404. raise PydanticUserError(
  405. f'Field {var_name!r} requires a type annotation', code='model-field-missing-annotation'
  406. )
  407. else:
  408. raise PydanticUserError(
  409. f'A non-annotated attribute was detected: `{var_name} = {value!r}`. All model fields require a '
  410. f'type annotation; if `{var_name}` is not meant to be a field, you may be able to resolve this '
  411. f"error by annotating it as a `ClassVar` or updating `model_config['ignored_types']`.",
  412. code='model-field-missing-annotation',
  413. )
  414. for ann_name, ann_type in raw_annotations.items():
  415. if (
  416. is_valid_privateattr_name(ann_name)
  417. and ann_name not in private_attributes
  418. and ann_name not in ignored_names
  419. # This condition can be a false negative when `ann_type` is stringified,
  420. # but it is handled in most cases in `set_model_fields`:
  421. and not is_classvar_annotation(ann_type)
  422. and ann_type not in all_ignored_types
  423. and getattr(ann_type, '__module__', None) != 'functools'
  424. ):
  425. if isinstance(ann_type, str):
  426. # Walking up the frames to get the module namespace where the model is defined
  427. # (as the model class wasn't created yet, we unfortunately can't use `cls.__module__`):
  428. frame = sys._getframe(2)
  429. if frame is not None:
  430. try:
  431. ann_type = eval_type_backport(
  432. _make_forward_ref(ann_type, is_argument=False, is_class=True),
  433. globalns=frame.f_globals,
  434. localns=frame.f_locals,
  435. )
  436. except (NameError, TypeError):
  437. pass
  438. if typing_objects.is_annotated(get_origin(ann_type)):
  439. _, *metadata = get_args(ann_type)
  440. private_attr = next((v for v in metadata if isinstance(v, ModelPrivateAttr)), None)
  441. if private_attr is not None:
  442. private_attributes[ann_name] = private_attr
  443. continue
  444. private_attributes[ann_name] = PrivateAttr()
  445. return private_attributes
  446. def set_default_hash_func(cls: type[BaseModel], bases: tuple[type[Any], ...]) -> None:
  447. base_hash_func = get_attribute_from_bases(bases, '__hash__')
  448. new_hash_func = make_hash_func(cls)
  449. if base_hash_func in {None, object.__hash__} or getattr(base_hash_func, '__code__', None) == new_hash_func.__code__:
  450. # If `__hash__` is some default, we generate a hash function.
  451. # It will be `None` if not overridden from BaseModel.
  452. # It may be `object.__hash__` if there is another
  453. # parent class earlier in the bases which doesn't override `__hash__` (e.g. `typing.Generic`).
  454. # It may be a value set by `set_default_hash_func` if `cls` is a subclass of another frozen model.
  455. # In the last case we still need a new hash function to account for new `model_fields`.
  456. cls.__hash__ = new_hash_func
  457. def make_hash_func(cls: type[BaseModel]) -> Any:
  458. getter = operator.itemgetter(*cls.__pydantic_fields__.keys()) if cls.__pydantic_fields__ else lambda _: 0
  459. def hash_func(self: Any) -> int:
  460. try:
  461. return hash(getter(self.__dict__))
  462. except KeyError:
  463. # In rare cases (such as when using the deprecated copy method), the __dict__ may not contain
  464. # all model fields, which is how we can get here.
  465. # getter(self.__dict__) is much faster than any 'safe' method that accounts for missing keys,
  466. # and wrapping it in a `try` doesn't slow things down much in the common case.
  467. return hash(getter(SafeGetItemProxy(self.__dict__)))
  468. return hash_func
  469. def set_model_fields(
  470. cls: type[BaseModel],
  471. config_wrapper: ConfigWrapper,
  472. ns_resolver: NsResolver | None,
  473. ) -> None:
  474. """Collect and set `cls.__pydantic_fields__` and `cls.__class_vars__`.
  475. Args:
  476. cls: BaseModel or dataclass.
  477. config_wrapper: The config wrapper instance.
  478. ns_resolver: Namespace resolver to use when getting model annotations.
  479. """
  480. typevars_map = get_model_typevars_map(cls)
  481. fields, class_vars = collect_model_fields(cls, config_wrapper, ns_resolver, typevars_map=typevars_map)
  482. cls.__pydantic_fields__ = fields
  483. cls.__class_vars__.update(class_vars)
  484. for k in class_vars:
  485. # Class vars should not be private attributes
  486. # We remove them _here_ and not earlier because we rely on inspecting the class to determine its classvars,
  487. # but private attributes are determined by inspecting the namespace _prior_ to class creation.
  488. # In the case that a classvar with a leading-'_' is defined via a ForwardRef (e.g., when using
  489. # `__future__.annotations`), we want to remove the private attribute which was detected _before_ we knew it
  490. # evaluated to a classvar
  491. value = cls.__private_attributes__.pop(k, None)
  492. if value is not None and value.default is not PydanticUndefined:
  493. setattr(cls, k, value.default)
  494. def complete_model_class(
  495. cls: type[BaseModel],
  496. config_wrapper: ConfigWrapper,
  497. ns_resolver: NsResolver,
  498. *,
  499. raise_errors: bool = True,
  500. call_on_complete_hook: bool = True,
  501. create_model_module: str | None = None,
  502. ) -> bool:
  503. """Finish building a model class.
  504. This logic must be called after class has been created since validation functions must be bound
  505. and `get_type_hints` requires a class object.
  506. Args:
  507. cls: BaseModel or dataclass.
  508. config_wrapper: The config wrapper instance.
  509. ns_resolver: The namespace resolver instance to use during schema building.
  510. raise_errors: Whether to raise errors.
  511. call_on_complete_hook: Whether to call the `__pydantic_on_complete__` hook.
  512. create_model_module: The module of the class to be created, if created by `create_model`.
  513. Returns:
  514. `True` if the model is successfully completed, else `False`.
  515. Raises:
  516. PydanticUndefinedAnnotation: If `PydanticUndefinedAnnotation` occurs in`__get_pydantic_core_schema__`
  517. and `raise_errors=True`.
  518. """
  519. typevars_map = get_model_typevars_map(cls)
  520. if not cls.__pydantic_fields_complete__:
  521. # Note: when coming from `ModelMetaclass.__new__()`, this results in fields being built twice.
  522. # We do so a second time here so that we can get the `NameError` for the specific undefined annotation.
  523. # Alternatively, we could let `GenerateSchema()` raise the error, but there are cases where incomplete
  524. # fields are inherited in `collect_model_fields()` and can actually have their annotation resolved in the
  525. # generate schema process. As we want to avoid having `__pydantic_fields_complete__` set to `False`
  526. # when `__pydantic_complete__` is `True`, we rebuild here:
  527. try:
  528. cls.__pydantic_fields__ = rebuild_model_fields(
  529. cls,
  530. config_wrapper=config_wrapper,
  531. ns_resolver=ns_resolver,
  532. typevars_map=typevars_map,
  533. )
  534. except NameError as e:
  535. exc = PydanticUndefinedAnnotation.from_name_error(e)
  536. set_model_mocks(cls, f'`{exc.name}`')
  537. if raise_errors:
  538. raise exc from e
  539. if not raise_errors and not cls.__pydantic_fields_complete__:
  540. # No need to continue with schema gen, it is guaranteed to fail
  541. return False
  542. assert cls.__pydantic_fields_complete__
  543. gen_schema = GenerateSchema(
  544. config_wrapper,
  545. ns_resolver,
  546. typevars_map,
  547. )
  548. try:
  549. schema = gen_schema.generate_schema(cls)
  550. except PydanticUndefinedAnnotation as e:
  551. if raise_errors:
  552. raise
  553. set_model_mocks(cls, f'`{e.name}`')
  554. return False
  555. core_config = config_wrapper.core_config(title=cls.__name__)
  556. try:
  557. schema = gen_schema.clean_schema(schema)
  558. except InvalidSchemaError:
  559. set_model_mocks(cls)
  560. return False
  561. # This needs to happen *after* model schema generation, as the return type
  562. # of the properties are evaluated and the `ComputedFieldInfo` are recreated:
  563. cls.__pydantic_computed_fields__ = {k: v.info for k, v in cls.__pydantic_decorators__.computed_fields.items()}
  564. set_deprecated_descriptors(cls)
  565. cls.__pydantic_core_schema__ = schema
  566. cls.__pydantic_validator__ = create_schema_validator(
  567. schema,
  568. cls,
  569. create_model_module or cls.__module__,
  570. cls.__qualname__,
  571. 'create_model' if create_model_module else 'BaseModel',
  572. core_config,
  573. config_wrapper.plugin_settings,
  574. )
  575. cls.__pydantic_serializer__ = SchemaSerializer(schema, core_config)
  576. # set __signature__ attr only for model class, but not for its instances
  577. # (because instances can define `__call__`, and `inspect.signature` shouldn't
  578. # use the `__signature__` attribute and instead generate from `__call__`).
  579. cls.__signature__ = LazyClassAttribute(
  580. '__signature__',
  581. partial(
  582. generate_pydantic_signature,
  583. init=cls.__init__,
  584. fields=cls.__pydantic_fields__,
  585. validate_by_name=config_wrapper.validate_by_name,
  586. extra=config_wrapper.extra,
  587. ),
  588. )
  589. cls.__pydantic_complete__ = True
  590. if call_on_complete_hook:
  591. cls.__pydantic_on_complete__()
  592. return True
  593. def set_deprecated_descriptors(cls: type[BaseModel]) -> None:
  594. """Set data descriptors on the class for deprecated fields."""
  595. for field, field_info in cls.__pydantic_fields__.items():
  596. if (msg := field_info.deprecation_message) is not None:
  597. desc = _DeprecatedFieldDescriptor(msg)
  598. desc.__set_name__(cls, field)
  599. setattr(cls, field, desc)
  600. for field, computed_field_info in cls.__pydantic_computed_fields__.items():
  601. if (
  602. (msg := computed_field_info.deprecation_message) is not None
  603. # Avoid having two warnings emitted:
  604. and not hasattr(unwrap_wrapped_function(computed_field_info.wrapped_property), '__deprecated__')
  605. ):
  606. desc = _DeprecatedFieldDescriptor(msg, computed_field_info.wrapped_property)
  607. desc.__set_name__(cls, field)
  608. setattr(cls, field, desc)
  609. class _DeprecatedFieldDescriptor:
  610. """Read-only data descriptor used to emit a runtime deprecation warning before accessing a deprecated field.
  611. Attributes:
  612. msg: The deprecation message to be emitted.
  613. wrapped_property: The property instance if the deprecated field is a computed field, or `None`.
  614. field_name: The name of the field being deprecated.
  615. """
  616. field_name: str
  617. def __init__(self, msg: str, wrapped_property: property | None = None) -> None:
  618. self.msg = msg
  619. self.wrapped_property = wrapped_property
  620. def __set_name__(self, cls: type[BaseModel], name: str) -> None:
  621. self.field_name = name
  622. def __get__(self, obj: BaseModel | None, obj_type: type[BaseModel] | None = None) -> Any:
  623. if obj is None:
  624. if self.wrapped_property is not None:
  625. return self.wrapped_property.__get__(None, obj_type)
  626. raise AttributeError(self.field_name)
  627. warnings.warn(self.msg, DeprecationWarning, stacklevel=2)
  628. if self.wrapped_property is not None:
  629. return self.wrapped_property.__get__(obj, obj_type)
  630. return obj.__dict__[self.field_name]
  631. # Defined to make it a data descriptor and take precedence over the instance's dictionary.
  632. # Note that it will not be called when setting a value on a model instance
  633. # as `BaseModel.__setattr__` is defined and takes priority.
  634. def __set__(self, obj: Any, value: Any) -> NoReturn:
  635. raise AttributeError(self.field_name)
  636. class _PydanticWeakRef:
  637. """Wrapper for `weakref.ref` that enables `pickle` serialization.
  638. Cloudpickle fails to serialize `weakref.ref` objects due to an arcane error related
  639. to abstract base classes (`abc.ABC`). This class works around the issue by wrapping
  640. `weakref.ref` instead of subclassing it.
  641. See https://github.com/pydantic/pydantic/issues/6763 for context.
  642. Semantics:
  643. - If not pickled, behaves the same as a `weakref.ref`.
  644. - If pickled along with the referenced object, the same `weakref.ref` behavior
  645. will be maintained between them after unpickling.
  646. - If pickled without the referenced object, after unpickling the underlying
  647. reference will be cleared (`__call__` will always return `None`).
  648. """
  649. def __init__(self, obj: Any):
  650. if obj is None:
  651. # The object will be `None` upon deserialization if the serialized weakref
  652. # had lost its underlying object.
  653. self._wr = None
  654. else:
  655. self._wr = weakref.ref(obj)
  656. def __call__(self) -> Any:
  657. if self._wr is None:
  658. return None
  659. else:
  660. return self._wr()
  661. def __reduce__(self) -> tuple[Callable, tuple[weakref.ReferenceType | None]]:
  662. return _PydanticWeakRef, (self(),)
  663. def build_lenient_weakvaluedict(d: dict[str, Any] | None) -> dict[str, Any] | None:
  664. """Takes an input dictionary, and produces a new value that (invertibly) replaces the values with weakrefs.
  665. We can't just use a WeakValueDictionary because many types (including int, str, etc.) can't be stored as values
  666. in a WeakValueDictionary.
  667. The `unpack_lenient_weakvaluedict` function can be used to reverse this operation.
  668. """
  669. if d is None:
  670. return None
  671. result = {}
  672. for k, v in d.items():
  673. try:
  674. proxy = _PydanticWeakRef(v)
  675. except TypeError:
  676. proxy = v
  677. result[k] = proxy
  678. return result
  679. def unpack_lenient_weakvaluedict(d: dict[str, Any] | None) -> dict[str, Any] | None:
  680. """Inverts the transform performed by `build_lenient_weakvaluedict`."""
  681. if d is None:
  682. return None
  683. result = {}
  684. for k, v in d.items():
  685. if isinstance(v, _PydanticWeakRef):
  686. v = v()
  687. if v is not None:
  688. result[k] = v
  689. else:
  690. result[k] = v
  691. return result
  692. @cache
  693. def default_ignored_types() -> tuple[type[Any], ...]:
  694. from ..fields import ComputedFieldInfo
  695. ignored_types = [
  696. FunctionType,
  697. property,
  698. classmethod,
  699. staticmethod,
  700. PydanticDescriptorProxy,
  701. ComputedFieldInfo,
  702. TypeAliasType, # from `typing_extensions`
  703. ]
  704. if sys.version_info >= (3, 12):
  705. ignored_types.append(typing.TypeAliasType)
  706. return tuple(ignored_types)