fields.py 45 KB

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