fields.py 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177
  1. """Defining fields on models."""
  2. from __future__ import annotations as _annotations
  3. import dataclasses
  4. import inspect
  5. import sys
  6. import typing
  7. from copy import copy
  8. from dataclasses import Field as DataclassField
  9. try:
  10. from functools import cached_property # type: ignore
  11. except ImportError:
  12. # python 3.7
  13. cached_property = None
  14. from typing import Any, ClassVar
  15. from warnings import warn
  16. import annotated_types
  17. import typing_extensions
  18. from pydantic_core import PydanticUndefined
  19. from typing_extensions import Literal, Unpack
  20. from . import types
  21. from ._internal import _decorators, _fields, _generics, _internal_dataclass, _repr, _typing_extra, _utils
  22. from .errors import PydanticUserError
  23. from .warnings import PydanticDeprecatedSince20
  24. if typing.TYPE_CHECKING:
  25. from ._internal._repr import ReprArgs
  26. else:
  27. # See PyCharm issues https://youtrack.jetbrains.com/issue/PY-21915
  28. # and https://youtrack.jetbrains.com/issue/PY-51428
  29. DeprecationWarning = PydanticDeprecatedSince20
  30. _Unset: Any = PydanticUndefined
  31. class _FromFieldInfoInputs(typing_extensions.TypedDict, total=False):
  32. """This class exists solely to add type checking for the `**kwargs` in `FieldInfo.from_field`."""
  33. annotation: type[Any] | None
  34. default_factory: typing.Callable[[], Any] | None
  35. alias: str | None
  36. alias_priority: int | None
  37. validation_alias: str | AliasPath | AliasChoices | None
  38. serialization_alias: str | None
  39. title: str | None
  40. description: str | None
  41. examples: list[Any] | None
  42. exclude: bool | None
  43. gt: float | None
  44. ge: float | None
  45. lt: float | None
  46. le: float | None
  47. multiple_of: float | None
  48. strict: bool | None
  49. min_length: int | None
  50. max_length: int | None
  51. pattern: str | None
  52. allow_inf_nan: bool | None
  53. max_digits: int | None
  54. decimal_places: int | None
  55. union_mode: Literal['smart', 'left_to_right'] | None
  56. discriminator: str | None
  57. json_schema_extra: dict[str, Any] | typing.Callable[[dict[str, Any]], None] | None
  58. frozen: bool | None
  59. validate_default: bool | None
  60. repr: bool
  61. init_var: bool | None
  62. kw_only: bool | None
  63. class _FieldInfoInputs(_FromFieldInfoInputs, total=False):
  64. """This class exists solely to add type checking for the `**kwargs` in `FieldInfo.__init__`."""
  65. default: Any
  66. class FieldInfo(_repr.Representation):
  67. """This class holds information about a field.
  68. `FieldInfo` is used for any field definition regardless of whether the [`Field()`][pydantic.fields.Field]
  69. function is explicitly used.
  70. !!! warning
  71. You generally shouldn't be creating `FieldInfo` directly, you'll only need to use it when accessing
  72. [`BaseModel`][pydantic.main.BaseModel] `.model_fields` internals.
  73. Attributes:
  74. annotation: The type annotation of the field.
  75. default: The default value of the field.
  76. default_factory: The factory function used to construct the default for the field.
  77. alias: The alias name of the field.
  78. alias_priority: The priority of the field's alias.
  79. validation_alias: The validation alias name of the field.
  80. serialization_alias: The serialization alias name of the field.
  81. title: The title of the field.
  82. description: The description of the field.
  83. examples: List of examples of the field.
  84. exclude: Whether to exclude the field from the model serialization.
  85. discriminator: Field name for discriminating the type in a tagged union.
  86. json_schema_extra: Dictionary of extra JSON schema properties.
  87. frozen: Whether the field is frozen.
  88. validate_default: Whether to validate the default value of the field.
  89. repr: Whether to include the field in representation of the model.
  90. init_var: Whether the field should be included in the constructor of the dataclass.
  91. kw_only: Whether the field should be a keyword-only argument in the constructor of the dataclass.
  92. metadata: List of metadata constraints.
  93. """
  94. annotation: type[Any] | None
  95. default: Any
  96. default_factory: typing.Callable[[], Any] | None
  97. alias: str | None
  98. alias_priority: int | None
  99. validation_alias: str | AliasPath | AliasChoices | None
  100. serialization_alias: str | None
  101. title: str | None
  102. description: str | None
  103. examples: list[Any] | None
  104. exclude: bool | None
  105. discriminator: str | None
  106. json_schema_extra: dict[str, Any] | typing.Callable[[dict[str, Any]], None] | None
  107. frozen: bool | None
  108. validate_default: bool | None
  109. repr: bool
  110. init_var: bool | None
  111. kw_only: bool | None
  112. metadata: list[Any]
  113. __slots__ = (
  114. 'annotation',
  115. 'default',
  116. 'default_factory',
  117. 'alias',
  118. 'alias_priority',
  119. 'validation_alias',
  120. 'serialization_alias',
  121. 'title',
  122. 'description',
  123. 'examples',
  124. 'exclude',
  125. 'discriminator',
  126. 'json_schema_extra',
  127. 'frozen',
  128. 'validate_default',
  129. 'repr',
  130. 'init_var',
  131. 'kw_only',
  132. 'metadata',
  133. '_attributes_set',
  134. )
  135. # used to convert kwargs to metadata/constraints,
  136. # None has a special meaning - these items are collected into a `PydanticGeneralMetadata`
  137. metadata_lookup: ClassVar[dict[str, typing.Callable[[Any], Any] | None]] = {
  138. 'strict': types.Strict,
  139. 'gt': annotated_types.Gt,
  140. 'ge': annotated_types.Ge,
  141. 'lt': annotated_types.Lt,
  142. 'le': annotated_types.Le,
  143. 'multiple_of': annotated_types.MultipleOf,
  144. 'min_length': annotated_types.MinLen,
  145. 'max_length': annotated_types.MaxLen,
  146. 'pattern': None,
  147. 'allow_inf_nan': None,
  148. 'max_digits': None,
  149. 'decimal_places': None,
  150. 'union_mode': None,
  151. }
  152. def __init__(self, **kwargs: Unpack[_FieldInfoInputs]) -> None:
  153. """This class should generally not be initialized directly; instead, use the `pydantic.fields.Field` function
  154. or one of the constructor classmethods.
  155. See the signature of `pydantic.fields.Field` for more details about the expected arguments.
  156. """
  157. self._attributes_set = {k: v for k, v in kwargs.items() if v is not _Unset}
  158. kwargs = {k: _DefaultValues.get(k) if v is _Unset else v for k, v in kwargs.items()} # type: ignore
  159. self.annotation, annotation_metadata = self._extract_metadata(kwargs.get('annotation'))
  160. default = kwargs.pop('default', PydanticUndefined)
  161. if default is Ellipsis:
  162. self.default = PydanticUndefined
  163. else:
  164. self.default = default
  165. self.default_factory = kwargs.pop('default_factory', None)
  166. if self.default is not PydanticUndefined and self.default_factory is not None:
  167. raise TypeError('cannot specify both default and default_factory')
  168. self.title = kwargs.pop('title', None)
  169. self.alias = kwargs.pop('alias', None)
  170. self.validation_alias = kwargs.pop('validation_alias', None)
  171. self.serialization_alias = kwargs.pop('serialization_alias', None)
  172. alias_is_set = any(alias is not None for alias in (self.alias, self.validation_alias, self.serialization_alias))
  173. self.alias_priority = kwargs.pop('alias_priority', None) or 2 if alias_is_set else None
  174. self.description = kwargs.pop('description', None)
  175. self.examples = kwargs.pop('examples', None)
  176. self.exclude = kwargs.pop('exclude', None)
  177. self.discriminator = kwargs.pop('discriminator', None)
  178. self.repr = kwargs.pop('repr', True)
  179. self.json_schema_extra = kwargs.pop('json_schema_extra', None)
  180. self.validate_default = kwargs.pop('validate_default', None)
  181. self.frozen = kwargs.pop('frozen', None)
  182. # currently only used on dataclasses
  183. self.init_var = kwargs.pop('init_var', None)
  184. self.kw_only = kwargs.pop('kw_only', None)
  185. self.metadata = self._collect_metadata(kwargs) + annotation_metadata # type: ignore
  186. @classmethod
  187. def from_field(
  188. cls, default: Any = PydanticUndefined, **kwargs: Unpack[_FromFieldInfoInputs]
  189. ) -> typing_extensions.Self:
  190. """Create a new `FieldInfo` object with the `Field` function.
  191. Args:
  192. default: The default value for the field. Defaults to Undefined.
  193. **kwargs: Additional arguments dictionary.
  194. Raises:
  195. TypeError: If 'annotation' is passed as a keyword argument.
  196. Returns:
  197. A new FieldInfo object with the given parameters.
  198. Example:
  199. This is how you can create a field with default value like this:
  200. ```python
  201. import pydantic
  202. class MyModel(pydantic.BaseModel):
  203. foo: int = pydantic.Field(4)
  204. ```
  205. """
  206. if 'annotation' in kwargs:
  207. raise TypeError('"annotation" is not permitted as a Field keyword argument')
  208. return cls(default=default, **kwargs)
  209. @classmethod
  210. def from_annotation(cls, annotation: type[Any]) -> typing_extensions.Self:
  211. """Creates a `FieldInfo` instance from a bare annotation.
  212. Args:
  213. annotation: An annotation object.
  214. Returns:
  215. An instance of the field metadata.
  216. Example:
  217. This is how you can create a field from a bare annotation like this:
  218. ```python
  219. import pydantic
  220. class MyModel(pydantic.BaseModel):
  221. foo: int # <-- like this
  222. ```
  223. We also account for the case where the annotation can be an instance of `Annotated` and where
  224. one of the (not first) arguments in `Annotated` are an instance of `FieldInfo`, e.g.:
  225. ```python
  226. import annotated_types
  227. from typing_extensions import Annotated
  228. import pydantic
  229. class MyModel(pydantic.BaseModel):
  230. foo: Annotated[int, annotated_types.Gt(42)]
  231. bar: Annotated[int, pydantic.Field(gt=42)]
  232. ```
  233. """
  234. final = False
  235. if _typing_extra.is_finalvar(annotation):
  236. final = True
  237. if annotation is not typing_extensions.Final:
  238. annotation = typing_extensions.get_args(annotation)[0]
  239. if _typing_extra.is_annotated(annotation):
  240. first_arg, *extra_args = typing_extensions.get_args(annotation)
  241. if _typing_extra.is_finalvar(first_arg):
  242. final = True
  243. field_info_annotations = [a for a in extra_args if isinstance(a, FieldInfo)]
  244. field_info = cls.merge_field_infos(*field_info_annotations, annotation=first_arg)
  245. if field_info:
  246. new_field_info = copy(field_info)
  247. new_field_info.annotation = first_arg
  248. new_field_info.frozen = final or field_info.frozen
  249. metadata: list[Any] = []
  250. for a in extra_args:
  251. if not isinstance(a, FieldInfo):
  252. metadata.append(a)
  253. else:
  254. metadata.extend(a.metadata)
  255. new_field_info.metadata = metadata
  256. return new_field_info
  257. return cls(annotation=annotation, frozen=final or None)
  258. @classmethod
  259. def from_annotated_attribute(cls, annotation: type[Any], default: Any) -> typing_extensions.Self:
  260. """Create `FieldInfo` from an annotation with a default value.
  261. Args:
  262. annotation: The type annotation of the field.
  263. default: The default value of the field.
  264. Returns:
  265. A field object with the passed values.
  266. Example:
  267. ```python
  268. import annotated_types
  269. from typing_extensions import Annotated
  270. import pydantic
  271. class MyModel(pydantic.BaseModel):
  272. foo: int = 4 # <-- like this
  273. bar: Annotated[int, annotated_types.Gt(4)] = 4 # <-- or this
  274. spam: Annotated[int, pydantic.Field(gt=4)] = 4 # <-- or this
  275. ```
  276. """
  277. final = False
  278. if _typing_extra.is_finalvar(annotation):
  279. final = True
  280. if annotation is not typing_extensions.Final:
  281. annotation = typing_extensions.get_args(annotation)[0]
  282. if isinstance(default, cls):
  283. default.annotation, annotation_metadata = cls._extract_metadata(annotation)
  284. default.metadata += annotation_metadata
  285. default = default.merge_field_infos(
  286. *[x for x in annotation_metadata if isinstance(x, cls)], default, annotation=default.annotation
  287. )
  288. default.frozen = final or default.frozen
  289. return default
  290. elif isinstance(default, dataclasses.Field):
  291. init_var = False
  292. if annotation is dataclasses.InitVar:
  293. if sys.version_info < (3, 8):
  294. raise RuntimeError('InitVar is not supported in Python 3.7 as type information is lost')
  295. init_var = True
  296. annotation = Any
  297. elif isinstance(annotation, dataclasses.InitVar):
  298. init_var = True
  299. annotation = annotation.type
  300. pydantic_field = cls._from_dataclass_field(default)
  301. pydantic_field.annotation, annotation_metadata = cls._extract_metadata(annotation)
  302. pydantic_field.metadata += annotation_metadata
  303. pydantic_field = pydantic_field.merge_field_infos(
  304. *[x for x in annotation_metadata if isinstance(x, cls)],
  305. pydantic_field,
  306. annotation=pydantic_field.annotation,
  307. )
  308. pydantic_field.frozen = final or pydantic_field.frozen
  309. pydantic_field.init_var = init_var
  310. pydantic_field.kw_only = getattr(default, 'kw_only', None)
  311. return pydantic_field
  312. else:
  313. if _typing_extra.is_annotated(annotation):
  314. first_arg, *extra_args = typing_extensions.get_args(annotation)
  315. field_infos = [a for a in extra_args if isinstance(a, FieldInfo)]
  316. field_info = cls.merge_field_infos(*field_infos, annotation=first_arg, default=default)
  317. metadata: list[Any] = []
  318. for a in extra_args:
  319. if not isinstance(a, FieldInfo):
  320. metadata.append(a)
  321. else:
  322. metadata.extend(a.metadata)
  323. field_info.metadata = metadata
  324. return field_info
  325. return cls(annotation=annotation, default=default, frozen=final or None)
  326. @staticmethod
  327. def merge_field_infos(*field_infos: FieldInfo, **overrides: Any) -> FieldInfo:
  328. """Merge `FieldInfo` instances keeping only explicitly set attributes.
  329. Later `FieldInfo` instances override earlier ones.
  330. Returns:
  331. FieldInfo: A merged FieldInfo instance.
  332. """
  333. flattened_field_infos: list[FieldInfo] = []
  334. for field_info in field_infos:
  335. flattened_field_infos.extend(x for x in field_info.metadata if isinstance(x, FieldInfo))
  336. flattened_field_infos.append(field_info)
  337. field_infos = tuple(flattened_field_infos)
  338. if len(field_infos) == 1:
  339. # No merging necessary, but we still need to make a copy and apply the overrides
  340. field_info = copy(field_infos[0])
  341. field_info._attributes_set.update(overrides)
  342. for k, v in overrides.items():
  343. setattr(field_info, k, v)
  344. return field_info
  345. new_kwargs: dict[str, Any] = {}
  346. metadata = {}
  347. for field_info in field_infos:
  348. new_kwargs.update(field_info._attributes_set)
  349. for x in field_info.metadata:
  350. if not isinstance(x, FieldInfo):
  351. metadata[type(x)] = x
  352. new_kwargs.update(overrides)
  353. field_info = FieldInfo(**new_kwargs)
  354. field_info.metadata = list(metadata.values())
  355. return field_info
  356. @classmethod
  357. def _from_dataclass_field(cls, dc_field: DataclassField[Any]) -> typing_extensions.Self:
  358. """Return a new `FieldInfo` instance from a `dataclasses.Field` instance.
  359. Args:
  360. dc_field: The `dataclasses.Field` instance to convert.
  361. Returns:
  362. The corresponding `FieldInfo` instance.
  363. Raises:
  364. TypeError: If any of the `FieldInfo` kwargs does not match the `dataclass.Field` kwargs.
  365. """
  366. default = dc_field.default
  367. if default is dataclasses.MISSING:
  368. default = PydanticUndefined
  369. if dc_field.default_factory is dataclasses.MISSING:
  370. default_factory: typing.Callable[[], Any] | None = None
  371. else:
  372. default_factory = dc_field.default_factory
  373. # use the `Field` function so in correct kwargs raise the correct `TypeError`
  374. dc_field_metadata = {k: v for k, v in dc_field.metadata.items() if k in _FIELD_ARG_NAMES}
  375. return Field(default=default, default_factory=default_factory, repr=dc_field.repr, **dc_field_metadata)
  376. @classmethod
  377. def _extract_metadata(cls, annotation: type[Any] | None) -> tuple[type[Any] | None, list[Any]]:
  378. """Tries to extract metadata/constraints from an annotation if it uses `Annotated`.
  379. Args:
  380. annotation: The type hint annotation for which metadata has to be extracted.
  381. Returns:
  382. A tuple containing the extracted metadata type and the list of extra arguments.
  383. """
  384. if annotation is not None:
  385. if _typing_extra.is_annotated(annotation):
  386. first_arg, *extra_args = typing_extensions.get_args(annotation)
  387. return first_arg, list(extra_args)
  388. return annotation, []
  389. @classmethod
  390. def _collect_metadata(cls, kwargs: dict[str, Any]) -> list[Any]:
  391. """Collect annotations from kwargs.
  392. The return type is actually `annotated_types.BaseMetadata | PydanticMetadata`,
  393. but it gets combined with `list[Any]` from `Annotated[T, ...]`, hence types.
  394. Args:
  395. kwargs: Keyword arguments passed to the function.
  396. Returns:
  397. A list of metadata objects - a combination of `annotated_types.BaseMetadata` and
  398. `PydanticMetadata`.
  399. """
  400. metadata: list[Any] = []
  401. general_metadata = {}
  402. for key, value in list(kwargs.items()):
  403. try:
  404. marker = cls.metadata_lookup[key]
  405. except KeyError:
  406. continue
  407. del kwargs[key]
  408. if value is not None:
  409. if marker is None:
  410. general_metadata[key] = value
  411. else:
  412. metadata.append(marker(value))
  413. if general_metadata:
  414. metadata.append(_fields.PydanticGeneralMetadata(**general_metadata))
  415. return metadata
  416. def get_default(self, *, call_default_factory: bool = False) -> Any:
  417. """Get the default value.
  418. We expose an option for whether to call the default_factory (if present), as calling it may
  419. result in side effects that we want to avoid. However, there are times when it really should
  420. be called (namely, when instantiating a model via `model_construct`).
  421. Args:
  422. call_default_factory: Whether to call the default_factory or not. Defaults to `False`.
  423. Returns:
  424. The default value, calling the default factory if requested or `None` if not set.
  425. """
  426. if self.default_factory is None:
  427. return _utils.smart_deepcopy(self.default)
  428. elif call_default_factory:
  429. return self.default_factory()
  430. else:
  431. return None
  432. def is_required(self) -> bool:
  433. """Check if the argument is required.
  434. Returns:
  435. `True` if the argument is required, `False` otherwise.
  436. """
  437. return self.default is PydanticUndefined and self.default_factory is None
  438. def rebuild_annotation(self) -> Any:
  439. """Rebuilds the original annotation for use in function signatures.
  440. If metadata is present, it adds it to the original annotation using an
  441. `AnnotatedAlias`. Otherwise, it returns the original annotation as is.
  442. Returns:
  443. The rebuilt annotation.
  444. """
  445. if not self.metadata:
  446. return self.annotation
  447. else:
  448. # Annotated arguments must be a tuple
  449. return typing_extensions.Annotated[(self.annotation, *self.metadata)] # type: ignore
  450. def apply_typevars_map(self, typevars_map: dict[Any, Any] | None, types_namespace: dict[str, Any] | None) -> None:
  451. """Apply a `typevars_map` to the annotation.
  452. This method is used when analyzing parametrized generic types to replace typevars with their concrete types.
  453. This method applies the `typevars_map` to the annotation in place.
  454. Args:
  455. typevars_map: A dictionary mapping type variables to their concrete types.
  456. types_namespace (dict | None): A dictionary containing related types to the annotated type.
  457. See Also:
  458. pydantic._internal._generics.replace_types is used for replacing the typevars with
  459. their concrete types.
  460. """
  461. annotation = _typing_extra.eval_type_lenient(self.annotation, types_namespace, None)
  462. self.annotation = _generics.replace_types(annotation, typevars_map)
  463. def __repr_args__(self) -> ReprArgs:
  464. yield 'annotation', _repr.PlainRepr(_repr.display_as_type(self.annotation))
  465. yield 'required', self.is_required()
  466. for s in self.__slots__:
  467. if s == '_attributes_set':
  468. continue
  469. if s == 'annotation':
  470. continue
  471. elif s == 'metadata' and not self.metadata:
  472. continue
  473. elif s == 'repr' and self.repr is True:
  474. continue
  475. if s == 'frozen' and self.frozen is False:
  476. continue
  477. if s == 'validation_alias' and self.validation_alias == self.alias:
  478. continue
  479. if s == 'serialization_alias' and self.serialization_alias == self.alias:
  480. continue
  481. if s == 'default_factory' and self.default_factory is not None:
  482. yield 'default_factory', _repr.PlainRepr(_repr.display_as_type(self.default_factory))
  483. else:
  484. value = getattr(self, s)
  485. if value is not None and value is not PydanticUndefined:
  486. yield s, value
  487. @dataclasses.dataclass(**_internal_dataclass.slots_true)
  488. class AliasPath:
  489. """Usage docs: https://docs.pydantic.dev/2.4/concepts/fields#aliaspath-and-aliaschoices
  490. A data class used by `validation_alias` as a convenience to create aliases.
  491. Attributes:
  492. path: A list of string or integer aliases.
  493. """
  494. path: list[int | str]
  495. def __init__(self, first_arg: str, *args: str | int) -> None:
  496. self.path = [first_arg] + list(args)
  497. def convert_to_aliases(self) -> list[str | int]:
  498. """Converts arguments to a list of string or integer aliases.
  499. Returns:
  500. The list of aliases.
  501. """
  502. return self.path
  503. @dataclasses.dataclass(**_internal_dataclass.slots_true)
  504. class AliasChoices:
  505. """Usage docs: https://docs.pydantic.dev/2.4/concepts/fields#aliaspath-and-aliaschoices
  506. A data class used by `validation_alias` as a convenience to create aliases.
  507. Attributes:
  508. choices: A list containing a string or `AliasPath`.
  509. """
  510. choices: list[str | AliasPath]
  511. def __init__(self, first_choice: str | AliasPath, *choices: str | AliasPath) -> None:
  512. self.choices = [first_choice] + list(choices)
  513. def convert_to_aliases(self) -> list[list[str | int]]:
  514. """Converts arguments to a list of lists containing string or integer aliases.
  515. Returns:
  516. The list of aliases.
  517. """
  518. aliases: list[list[str | int]] = []
  519. for c in self.choices:
  520. if isinstance(c, AliasPath):
  521. aliases.append(c.convert_to_aliases())
  522. else:
  523. aliases.append([c])
  524. return aliases
  525. class _EmptyKwargs(typing_extensions.TypedDict):
  526. """This class exists solely to ensure that type checking warns about passing `**extra` in `Field`."""
  527. _DefaultValues = dict(
  528. default=...,
  529. default_factory=None,
  530. alias=None,
  531. alias_priority=None,
  532. validation_alias=None,
  533. serialization_alias=None,
  534. title=None,
  535. description=None,
  536. examples=None,
  537. exclude=None,
  538. discriminator=None,
  539. json_schema_extra=None,
  540. frozen=None,
  541. validate_default=None,
  542. repr=True,
  543. init_var=None,
  544. kw_only=None,
  545. pattern=None,
  546. strict=None,
  547. gt=None,
  548. ge=None,
  549. lt=None,
  550. le=None,
  551. multiple_of=None,
  552. allow_inf_nan=None,
  553. max_digits=None,
  554. decimal_places=None,
  555. min_length=None,
  556. max_length=None,
  557. )
  558. def Field( # noqa: C901
  559. default: Any = PydanticUndefined,
  560. *,
  561. default_factory: typing.Callable[[], Any] | None = _Unset,
  562. alias: str | None = _Unset,
  563. alias_priority: int | None = _Unset,
  564. validation_alias: str | AliasPath | AliasChoices | None = _Unset,
  565. serialization_alias: str | None = _Unset,
  566. title: str | None = _Unset,
  567. description: str | None = _Unset,
  568. examples: list[Any] | None = _Unset,
  569. exclude: bool | None = _Unset,
  570. discriminator: str | None = _Unset,
  571. json_schema_extra: dict[str, Any] | typing.Callable[[dict[str, Any]], None] | None = _Unset,
  572. frozen: bool | None = _Unset,
  573. validate_default: bool | None = _Unset,
  574. repr: bool = _Unset,
  575. init_var: bool | None = _Unset,
  576. kw_only: bool | None = _Unset,
  577. pattern: str | None = _Unset,
  578. strict: bool | None = _Unset,
  579. gt: float | None = _Unset,
  580. ge: float | None = _Unset,
  581. lt: float | None = _Unset,
  582. le: float | None = _Unset,
  583. multiple_of: float | None = _Unset,
  584. allow_inf_nan: bool | None = _Unset,
  585. max_digits: int | None = _Unset,
  586. decimal_places: int | None = _Unset,
  587. min_length: int | None = _Unset,
  588. max_length: int | None = _Unset,
  589. union_mode: Literal['smart', 'left_to_right'] = _Unset,
  590. **extra: Unpack[_EmptyKwargs],
  591. ) -> Any:
  592. """Usage docs: https://docs.pydantic.dev/2.4/concepts/fields
  593. Create a field for objects that can be configured.
  594. Used to provide extra information about a field, either for the model schema or complex validation. Some arguments
  595. apply only to number fields (`int`, `float`, `Decimal`) and some apply only to `str`.
  596. Note:
  597. - Any `_Unset` objects will be replaced by the corresponding value defined in the `_DefaultValues` dictionary. If a key for the `_Unset` object is not found in the `_DefaultValues` dictionary, it will default to `None`
  598. Args:
  599. default: Default value if the field is not set.
  600. default_factory: A callable to generate the default value, such as :func:`~datetime.utcnow`.
  601. alias: An alternative name for the attribute.
  602. alias_priority: Priority of the alias. This affects whether an alias generator is used.
  603. validation_alias: 'Whitelist' validation step. The field will be the single one allowed by the alias or set of
  604. aliases defined.
  605. serialization_alias: 'Blacklist' validation step. The vanilla field will be the single one of the alias' or set
  606. of aliases' fields and all the other fields will be ignored at serialization time.
  607. title: Human-readable title.
  608. description: Human-readable description.
  609. examples: Example values for this field.
  610. exclude: Whether to exclude the field from the model serialization.
  611. discriminator: Field name for discriminating the type in a tagged union.
  612. json_schema_extra: Any additional JSON schema data for the schema property.
  613. frozen: Whether the field is frozen.
  614. validate_default: Run validation that isn't only checking existence of defaults. This can be set to `True` or `False`. If not set, it defaults to `None`.
  615. repr: A boolean indicating whether to include the field in the `__repr__` output.
  616. init_var: Whether the field should be included in the constructor of the dataclass.
  617. kw_only: Whether the field should be a keyword-only argument in the constructor of the dataclass.
  618. strict: If `True`, strict validation is applied to the field.
  619. See [Strict Mode](../concepts/strict_mode.md) for details.
  620. gt: Greater than. If set, value must be greater than this. Only applicable to numbers.
  621. ge: Greater than or equal. If set, value must be greater than or equal to this. Only applicable to numbers.
  622. lt: Less than. If set, value must be less than this. Only applicable to numbers.
  623. le: Less than or equal. If set, value must be less than or equal to this. Only applicable to numbers.
  624. multiple_of: Value must be a multiple of this. Only applicable to numbers.
  625. min_length: Minimum length for strings.
  626. max_length: Maximum length for strings.
  627. pattern: Pattern for strings.
  628. allow_inf_nan: Allow `inf`, `-inf`, `nan`. Only applicable to numbers.
  629. max_digits: Maximum number of allow digits for strings.
  630. decimal_places: Maximum number of decimal places allowed for numbers.
  631. union_mode: The strategy to apply when validating a union. Can be `smart` (the default), or `left_to_right`.
  632. See [Union Mode](standard_library_types.md#union-mode) for details.
  633. extra: Include extra fields used by the JSON schema.
  634. !!! warning Deprecated
  635. The `extra` kwargs is deprecated. Use `json_schema_extra` instead.
  636. Returns:
  637. A new [`FieldInfo`][pydantic.fields.FieldInfo], the return annotation is `Any` so `Field` can be used on
  638. type annotated fields without causing a typing error.
  639. """
  640. # Check deprecated and removed params from V1. This logic should eventually be removed.
  641. const = extra.pop('const', None) # type: ignore
  642. if const is not None:
  643. raise PydanticUserError('`const` is removed, use `Literal` instead', code='removed-kwargs')
  644. min_items = extra.pop('min_items', None) # type: ignore
  645. if min_items is not None:
  646. warn('`min_items` is deprecated and will be removed, use `min_length` instead', DeprecationWarning)
  647. if min_length in (None, _Unset):
  648. min_length = min_items # type: ignore
  649. max_items = extra.pop('max_items', None) # type: ignore
  650. if max_items is not None:
  651. warn('`max_items` is deprecated and will be removed, use `max_length` instead', DeprecationWarning)
  652. if max_length in (None, _Unset):
  653. max_length = max_items # type: ignore
  654. unique_items = extra.pop('unique_items', None) # type: ignore
  655. if unique_items is not None:
  656. raise PydanticUserError(
  657. (
  658. '`unique_items` is removed, use `Set` instead'
  659. '(this feature is discussed in https://github.com/pydantic/pydantic-core/issues/296)'
  660. ),
  661. code='removed-kwargs',
  662. )
  663. allow_mutation = extra.pop('allow_mutation', None) # type: ignore
  664. if allow_mutation is not None:
  665. warn('`allow_mutation` is deprecated and will be removed. use `frozen` instead', DeprecationWarning)
  666. if allow_mutation is False:
  667. frozen = True
  668. regex = extra.pop('regex', None) # type: ignore
  669. if regex is not None:
  670. raise PydanticUserError('`regex` is removed. use `pattern` instead', code='removed-kwargs')
  671. if extra:
  672. warn(
  673. 'Using extra keyword arguments on `Field` is deprecated and will be removed.'
  674. ' Use `json_schema_extra` instead.'
  675. f' (Extra keys: {", ".join(k.__repr__() for k in extra.keys())})',
  676. DeprecationWarning,
  677. )
  678. if not json_schema_extra or json_schema_extra is _Unset:
  679. json_schema_extra = extra # type: ignore
  680. if (
  681. validation_alias
  682. and validation_alias is not _Unset
  683. and not isinstance(validation_alias, (str, AliasChoices, AliasPath))
  684. ):
  685. raise TypeError('Invalid `validation_alias` type. it should be `str`, `AliasChoices`, or `AliasPath`')
  686. if serialization_alias in (_Unset, None) and isinstance(alias, str):
  687. serialization_alias = alias
  688. if validation_alias in (_Unset, None):
  689. validation_alias = alias
  690. include = extra.pop('include', None) # type: ignore
  691. if include is not None:
  692. warn('`include` is deprecated and does nothing. It will be removed, use `exclude` instead', DeprecationWarning)
  693. return FieldInfo.from_field(
  694. default,
  695. default_factory=default_factory,
  696. alias=alias,
  697. alias_priority=alias_priority,
  698. validation_alias=validation_alias,
  699. serialization_alias=serialization_alias,
  700. title=title,
  701. description=description,
  702. examples=examples,
  703. exclude=exclude,
  704. discriminator=discriminator,
  705. json_schema_extra=json_schema_extra,
  706. frozen=frozen,
  707. pattern=pattern,
  708. validate_default=validate_default,
  709. repr=repr,
  710. init_var=init_var,
  711. kw_only=kw_only,
  712. strict=strict,
  713. gt=gt,
  714. ge=ge,
  715. lt=lt,
  716. le=le,
  717. multiple_of=multiple_of,
  718. min_length=min_length,
  719. max_length=max_length,
  720. allow_inf_nan=allow_inf_nan,
  721. max_digits=max_digits,
  722. decimal_places=decimal_places,
  723. union_mode=union_mode,
  724. )
  725. _FIELD_ARG_NAMES = set(inspect.signature(Field).parameters)
  726. _FIELD_ARG_NAMES.remove('extra') # do not include the varkwargs parameter
  727. class ModelPrivateAttr(_repr.Representation):
  728. """A descriptor for private attributes in class models.
  729. Attributes:
  730. default: The default value of the attribute if not provided.
  731. default_factory: A callable function that generates the default value of the
  732. attribute if not provided.
  733. """
  734. __slots__ = 'default', 'default_factory'
  735. def __init__(
  736. self, default: Any = PydanticUndefined, *, default_factory: typing.Callable[[], Any] | None = None
  737. ) -> None:
  738. self.default = default
  739. self.default_factory = default_factory
  740. if not typing.TYPE_CHECKING:
  741. # We put `__getattr__` in a non-TYPE_CHECKING block because otherwise, mypy allows arbitrary attribute access
  742. def __getattr__(self, item: str) -> Any:
  743. """This function improves compatibility with custom descriptors by ensuring delegation happens
  744. as expected when the default value of a private attribute is a descriptor.
  745. """
  746. if item in {'__get__', '__set__', '__delete__'}:
  747. if hasattr(self.default, item):
  748. return getattr(self.default, item)
  749. raise AttributeError(f'{type(self).__name__!r} object has no attribute {item!r}')
  750. def __set_name__(self, cls: type[Any], name: str) -> None:
  751. """Preserve `__set_name__` protocol defined in https://peps.python.org/pep-0487."""
  752. if self.default is PydanticUndefined:
  753. return
  754. if not hasattr(self.default, '__set_name__'):
  755. return
  756. set_name = self.default.__set_name__
  757. if callable(set_name):
  758. set_name(cls, name)
  759. def get_default(self) -> Any:
  760. """Retrieve the default value of the object.
  761. If `self.default_factory` is `None`, the method will return a deep copy of the `self.default` object.
  762. If `self.default_factory` is not `None`, it will call `self.default_factory` and return the value returned.
  763. Returns:
  764. The default value of the object.
  765. """
  766. return _utils.smart_deepcopy(self.default) if self.default_factory is None else self.default_factory()
  767. def __eq__(self, other: Any) -> bool:
  768. return isinstance(other, self.__class__) and (self.default, self.default_factory) == (
  769. other.default,
  770. other.default_factory,
  771. )
  772. def PrivateAttr(
  773. default: Any = PydanticUndefined,
  774. *,
  775. default_factory: typing.Callable[[], Any] | None = None,
  776. ) -> Any:
  777. """Indicates that attribute is only used internally and never mixed with regular fields.
  778. Private attributes are not checked by Pydantic, so it's up to you to maintain their accuracy.
  779. Private attributes are stored in `__private_attributes__` on the model.
  780. Args:
  781. default: The attribute's default value. Defaults to Undefined.
  782. default_factory: Callable that will be
  783. called when a default value is needed for this attribute.
  784. If both `default` and `default_factory` are set, an error will be raised.
  785. Returns:
  786. An instance of [`ModelPrivateAttr`][pydantic.fields.ModelPrivateAttr] class.
  787. Raises:
  788. ValueError: If both `default` and `default_factory` are set.
  789. """
  790. if default is not PydanticUndefined and default_factory is not None:
  791. raise TypeError('cannot specify both default and default_factory')
  792. return ModelPrivateAttr(
  793. default,
  794. default_factory=default_factory,
  795. )
  796. @dataclasses.dataclass(**_internal_dataclass.slots_true)
  797. class ComputedFieldInfo:
  798. """A container for data from `@computed_field` so that we can access it while building the pydantic-core schema.
  799. Attributes:
  800. decorator_repr: A class variable representing the decorator string, '@computed_field'.
  801. wrapped_property: The wrapped computed field property.
  802. return_type: The type of the computed field property's return value.
  803. alias: The alias of the property to be used during encoding and decoding.
  804. alias_priority: priority of the alias. This affects whether an alias generator is used
  805. title: Title of the computed field as in OpenAPI document, should be a short summary.
  806. description: Description of the computed field as in OpenAPI document.
  807. repr: A boolean indicating whether or not to include the field in the __repr__ output.
  808. """
  809. decorator_repr: ClassVar[str] = '@computed_field'
  810. wrapped_property: property
  811. return_type: Any
  812. alias: str | None
  813. alias_priority: int | None
  814. title: str | None
  815. description: str | None
  816. repr: bool
  817. # this should really be `property[T], cached_proprety[T]` but property is not generic unlike cached_property
  818. # See https://github.com/python/typing/issues/985 and linked issues
  819. PropertyT = typing.TypeVar('PropertyT')
  820. @typing.overload
  821. def computed_field(
  822. *,
  823. return_type: Any = PydanticUndefined,
  824. alias: str | None = None,
  825. alias_priority: int | None = None,
  826. title: str | None = None,
  827. description: str | None = None,
  828. repr: bool = True,
  829. ) -> typing.Callable[[PropertyT], PropertyT]:
  830. ...
  831. @typing.overload
  832. def computed_field(__func: PropertyT) -> PropertyT:
  833. ...
  834. def _wrapped_property_is_private(property_: cached_property | property) -> bool: # type: ignore
  835. """Returns true if provided property is private, False otherwise."""
  836. wrapped_name: str = ''
  837. if isinstance(property_, property):
  838. wrapped_name = getattr(property_.fget, '__name__', '')
  839. elif isinstance(property_, cached_property): # type: ignore
  840. wrapped_name = getattr(property_.func, '__name__', '') # type: ignore
  841. return wrapped_name.startswith('_') and not wrapped_name.startswith('__')
  842. def computed_field(
  843. __f: PropertyT | None = None,
  844. *,
  845. alias: str | None = None,
  846. alias_priority: int | None = None,
  847. title: str | None = None,
  848. description: str | None = None,
  849. repr: bool | None = None,
  850. return_type: Any = PydanticUndefined,
  851. ) -> PropertyT | typing.Callable[[PropertyT], PropertyT]:
  852. """Decorator to include `property` and `cached_property` when serializing models or dataclasses.
  853. This is useful for fields that are computed from other fields, or for fields that are expensive to compute and should be cached.
  854. ```py
  855. from pydantic import BaseModel, computed_field
  856. class Rectangle(BaseModel):
  857. width: int
  858. length: int
  859. @computed_field
  860. @property
  861. def area(self) -> int:
  862. return self.width * self.length
  863. print(Rectangle(width=3, length=2).model_dump())
  864. #> {'width': 3, 'length': 2, 'area': 6}
  865. ```
  866. If applied to functions not yet decorated with `@property` or `@cached_property`, the function is
  867. automatically wrapped with `property`. Although this is more concise, you will lose IntelliSense in your IDE,
  868. and confuse static type checkers, thus explicit use of `@property` is recommended.
  869. !!! warning "Mypy Warning"
  870. Even with the `@property` or `@cached_property` applied to your function before `@computed_field`,
  871. mypy may throw a `Decorated property not supported` error.
  872. See [mypy issue #1362](https://github.com/python/mypy/issues/1362), for more information.
  873. To avoid this error message, add `# type: ignore[misc]` to the `@computed_field` line.
  874. [pyright](https://github.com/microsoft/pyright) supports `@computed_field` without error.
  875. ```py
  876. import random
  877. from pydantic import BaseModel, computed_field
  878. class Square(BaseModel):
  879. width: float
  880. @computed_field
  881. def area(self) -> float: # converted to a `property` by `computed_field`
  882. return round(self.width**2, 2)
  883. @area.setter
  884. def area(self, new_area: float) -> None:
  885. self.width = new_area**0.5
  886. @computed_field(alias='the magic number', repr=False)
  887. def random_number(self) -> int:
  888. return random.randint(0, 1_000)
  889. square = Square(width=1.3)
  890. # `random_number` does not appear in representation
  891. print(repr(square))
  892. #> Square(width=1.3, area=1.69)
  893. print(square.random_number)
  894. #> 3
  895. square.area = 4
  896. print(square.model_dump_json(by_alias=True))
  897. #> {"width":2.0,"area":4.0,"the magic number":3}
  898. ```
  899. !!! warning "Overriding with `computed_field`"
  900. You can't override a field from a parent class with a `computed_field` in the child class.
  901. `mypy` complains about this behavior if allowed, and `dataclasses` doesn't allow this pattern either.
  902. See the example below:
  903. ```py
  904. from pydantic import BaseModel, computed_field
  905. class Parent(BaseModel):
  906. a: str
  907. try:
  908. class Child(Parent):
  909. @computed_field
  910. @property
  911. def a(self) -> str:
  912. return 'new a'
  913. except ValueError as e:
  914. print(repr(e))
  915. #> ValueError("you can't override a field with a computed field")
  916. ```
  917. Private properties decorated with `@computed_field` have `repr=False` by default.
  918. ```py
  919. from functools import cached_property
  920. from pydantic import BaseModel, computed_field
  921. class Model(BaseModel):
  922. foo: int
  923. @computed_field
  924. @cached_property
  925. def _private_cached_property(self) -> int:
  926. return -self.foo
  927. @computed_field
  928. @property
  929. def _private_property(self) -> int:
  930. return -self.foo
  931. m = Model(foo=1)
  932. print(repr(m))
  933. #> M(foo=1)
  934. ```
  935. Args:
  936. __f: the function to wrap.
  937. alias: alias to use when serializing this computed field, only used when `by_alias=True`
  938. alias_priority: priority of the alias. This affects whether an alias generator is used
  939. title: Title to used when including this computed field in JSON Schema, currently unused waiting for #4697
  940. description: Description to used when including this computed field in JSON Schema, defaults to the functions
  941. docstring, currently unused waiting for #4697
  942. repr: whether to include this computed field in model repr.
  943. Default is `False` for private properties and `True` for public properties.
  944. return_type: optional return for serialization logic to expect when serializing to JSON, if included
  945. this must be correct, otherwise a `TypeError` is raised.
  946. If you don't include a return type Any is used, which does runtime introspection to handle arbitrary
  947. objects.
  948. Returns:
  949. A proxy wrapper for the property.
  950. """
  951. def dec(f: Any) -> Any:
  952. nonlocal description, return_type, alias_priority
  953. unwrapped = _decorators.unwrap_wrapped_function(f)
  954. if description is None and unwrapped.__doc__:
  955. description = inspect.cleandoc(unwrapped.__doc__)
  956. # if the function isn't already decorated with `@property` (or another descriptor), then we wrap it now
  957. f = _decorators.ensure_property(f)
  958. alias_priority = (alias_priority or 2) if alias is not None else None
  959. if repr is None:
  960. repr_: bool = False if _wrapped_property_is_private(property_=f) else True
  961. else:
  962. repr_ = repr
  963. dec_info = ComputedFieldInfo(f, return_type, alias, alias_priority, title, description, repr_)
  964. return _decorators.PydanticDescriptorProxy(f, dec_info)
  965. if __f is None:
  966. return dec
  967. else:
  968. return dec(__f)