fields.py 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504
  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. from functools import cached_property
  10. from typing import Any, Callable, ClassVar, TypeVar, cast, overload
  11. from warnings import warn
  12. import annotated_types
  13. import typing_extensions
  14. from pydantic_core import PydanticUndefined
  15. from typing_extensions import Literal, TypeAlias, Unpack, deprecated
  16. from . import types
  17. from ._internal import _decorators, _fields, _generics, _internal_dataclass, _repr, _typing_extra, _utils
  18. from ._internal._namespace_utils import GlobalsNamespace, MappingNamespace
  19. from .aliases import AliasChoices, AliasPath
  20. from .config import JsonDict
  21. from .errors import PydanticUserError
  22. from .json_schema import PydanticJsonSchemaWarning
  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. __all__ = 'Field', 'PrivateAttr', 'computed_field'
  31. _Unset: Any = PydanticUndefined
  32. if sys.version_info >= (3, 13):
  33. import warnings
  34. Deprecated: TypeAlias = warnings.deprecated | deprecated
  35. else:
  36. Deprecated: TypeAlias = deprecated
  37. class _FromFieldInfoInputs(typing_extensions.TypedDict, total=False):
  38. """This class exists solely to add type checking for the `**kwargs` in `FieldInfo.from_field`."""
  39. annotation: type[Any] | None
  40. default_factory: Callable[[], Any] | Callable[[dict[str, Any]], Any] | None
  41. alias: str | None
  42. alias_priority: int | None
  43. validation_alias: str | AliasPath | AliasChoices | None
  44. serialization_alias: str | None
  45. title: str | None
  46. field_title_generator: Callable[[str, FieldInfo], str] | None
  47. description: str | None
  48. examples: list[Any] | None
  49. exclude: bool | None
  50. gt: annotated_types.SupportsGt | None
  51. ge: annotated_types.SupportsGe | None
  52. lt: annotated_types.SupportsLt | None
  53. le: annotated_types.SupportsLe | None
  54. multiple_of: float | None
  55. strict: bool | None
  56. min_length: int | None
  57. max_length: int | None
  58. pattern: str | typing.Pattern[str] | None
  59. allow_inf_nan: bool | None
  60. max_digits: int | None
  61. decimal_places: int | None
  62. union_mode: Literal['smart', 'left_to_right'] | None
  63. discriminator: str | types.Discriminator | None
  64. deprecated: Deprecated | str | bool | None
  65. json_schema_extra: JsonDict | Callable[[JsonDict], None] | None
  66. frozen: bool | None
  67. validate_default: bool | None
  68. repr: bool
  69. init: bool | None
  70. init_var: bool | None
  71. kw_only: bool | None
  72. coerce_numbers_to_str: bool | None
  73. fail_fast: bool | None
  74. class _FieldInfoInputs(_FromFieldInfoInputs, total=False):
  75. """This class exists solely to add type checking for the `**kwargs` in `FieldInfo.__init__`."""
  76. default: Any
  77. class FieldInfo(_repr.Representation):
  78. """This class holds information about a field.
  79. `FieldInfo` is used for any field definition regardless of whether the [`Field()`][pydantic.fields.Field]
  80. function is explicitly used.
  81. !!! warning
  82. You generally shouldn't be creating `FieldInfo` directly, you'll only need to use it when accessing
  83. [`BaseModel`][pydantic.main.BaseModel] `.model_fields` internals.
  84. Attributes:
  85. annotation: The type annotation of the field.
  86. default: The default value of the field.
  87. default_factory: A callable to generate the default value. The callable can either take 0 arguments
  88. (in which case it is called as is) or a single argument containing the already validated data.
  89. alias: The alias name of the field.
  90. alias_priority: The priority of the field's alias.
  91. validation_alias: The validation alias of the field.
  92. serialization_alias: The serialization alias of the field.
  93. title: The title of the field.
  94. field_title_generator: A callable that takes a field name and returns title for it.
  95. description: The description of the field.
  96. examples: List of examples of the field.
  97. exclude: Whether to exclude the field from the model serialization.
  98. discriminator: Field name or Discriminator for discriminating the type in a tagged union.
  99. deprecated: A deprecation message, an instance of `warnings.deprecated` or the `typing_extensions.deprecated` backport,
  100. or a boolean. If `True`, a default deprecation message will be emitted when accessing the field.
  101. json_schema_extra: A dict or callable to provide extra JSON schema properties.
  102. frozen: Whether the field is frozen.
  103. validate_default: Whether to validate the default value of the field.
  104. repr: Whether to include the field in representation of the model.
  105. init: Whether the field should be included in the constructor of the dataclass.
  106. init_var: Whether the field should _only_ be included in the constructor of the dataclass, and not stored.
  107. kw_only: Whether the field should be a keyword-only argument in the constructor of the dataclass.
  108. metadata: List of metadata constraints.
  109. """
  110. annotation: type[Any] | None
  111. default: Any
  112. default_factory: Callable[[], Any] | Callable[[dict[str, Any]], Any] | None
  113. alias: str | None
  114. alias_priority: int | None
  115. validation_alias: str | AliasPath | AliasChoices | None
  116. serialization_alias: str | None
  117. title: str | None
  118. field_title_generator: Callable[[str, FieldInfo], str] | None
  119. description: str | None
  120. examples: list[Any] | None
  121. exclude: bool | None
  122. discriminator: str | types.Discriminator | None
  123. deprecated: Deprecated | str | bool | None
  124. json_schema_extra: JsonDict | Callable[[JsonDict], None] | None
  125. frozen: bool | None
  126. validate_default: bool | None
  127. repr: bool
  128. init: bool | None
  129. init_var: bool | None
  130. kw_only: bool | None
  131. metadata: list[Any]
  132. __slots__ = (
  133. 'annotation',
  134. 'evaluated',
  135. 'default',
  136. 'default_factory',
  137. 'alias',
  138. 'alias_priority',
  139. 'validation_alias',
  140. 'serialization_alias',
  141. 'title',
  142. 'field_title_generator',
  143. 'description',
  144. 'examples',
  145. 'exclude',
  146. 'discriminator',
  147. 'deprecated',
  148. 'json_schema_extra',
  149. 'frozen',
  150. 'validate_default',
  151. 'repr',
  152. 'init',
  153. 'init_var',
  154. 'kw_only',
  155. 'metadata',
  156. '_attributes_set',
  157. )
  158. # used to convert kwargs to metadata/constraints,
  159. # None has a special meaning - these items are collected into a `PydanticGeneralMetadata`
  160. metadata_lookup: ClassVar[dict[str, typing.Callable[[Any], Any] | None]] = {
  161. 'strict': types.Strict,
  162. 'gt': annotated_types.Gt,
  163. 'ge': annotated_types.Ge,
  164. 'lt': annotated_types.Lt,
  165. 'le': annotated_types.Le,
  166. 'multiple_of': annotated_types.MultipleOf,
  167. 'min_length': annotated_types.MinLen,
  168. 'max_length': annotated_types.MaxLen,
  169. 'pattern': None,
  170. 'allow_inf_nan': None,
  171. 'max_digits': None,
  172. 'decimal_places': None,
  173. 'union_mode': None,
  174. 'coerce_numbers_to_str': None,
  175. 'fail_fast': types.FailFast,
  176. }
  177. def __init__(self, **kwargs: Unpack[_FieldInfoInputs]) -> None:
  178. """This class should generally not be initialized directly; instead, use the `pydantic.fields.Field` function
  179. or one of the constructor classmethods.
  180. See the signature of `pydantic.fields.Field` for more details about the expected arguments.
  181. """
  182. self._attributes_set = {k: v for k, v in kwargs.items() if v is not _Unset}
  183. kwargs = {k: _DefaultValues.get(k) if v is _Unset else v for k, v in kwargs.items()} # type: ignore
  184. self.annotation, annotation_metadata = self._extract_metadata(kwargs.get('annotation'))
  185. self.evaluated = False
  186. default = kwargs.pop('default', PydanticUndefined)
  187. if default is Ellipsis:
  188. self.default = PydanticUndefined
  189. # Also remove it from the attributes set, otherwise
  190. # `GenerateSchema._common_field_schema` mistakenly
  191. # uses it:
  192. self._attributes_set.pop('default', None)
  193. else:
  194. self.default = default
  195. self.default_factory = kwargs.pop('default_factory', None)
  196. if self.default is not PydanticUndefined and self.default_factory is not None:
  197. raise TypeError('cannot specify both default and default_factory')
  198. self.alias = kwargs.pop('alias', None)
  199. self.validation_alias = kwargs.pop('validation_alias', None)
  200. self.serialization_alias = kwargs.pop('serialization_alias', None)
  201. alias_is_set = any(alias is not None for alias in (self.alias, self.validation_alias, self.serialization_alias))
  202. self.alias_priority = kwargs.pop('alias_priority', None) or 2 if alias_is_set else None
  203. self.title = kwargs.pop('title', None)
  204. self.field_title_generator = kwargs.pop('field_title_generator', None)
  205. self.description = kwargs.pop('description', None)
  206. self.examples = kwargs.pop('examples', None)
  207. self.exclude = kwargs.pop('exclude', None)
  208. self.discriminator = kwargs.pop('discriminator', None)
  209. # For compatibility with FastAPI<=0.110.0, we preserve the existing value if it is not overridden
  210. self.deprecated = kwargs.pop('deprecated', getattr(self, 'deprecated', None))
  211. self.repr = kwargs.pop('repr', True)
  212. self.json_schema_extra = kwargs.pop('json_schema_extra', None)
  213. self.validate_default = kwargs.pop('validate_default', None)
  214. self.frozen = kwargs.pop('frozen', None)
  215. # currently only used on dataclasses
  216. self.init = kwargs.pop('init', None)
  217. self.init_var = kwargs.pop('init_var', None)
  218. self.kw_only = kwargs.pop('kw_only', None)
  219. self.metadata = self._collect_metadata(kwargs) + annotation_metadata # type: ignore
  220. @staticmethod
  221. def from_field(default: Any = PydanticUndefined, **kwargs: Unpack[_FromFieldInfoInputs]) -> FieldInfo:
  222. """Create a new `FieldInfo` object with the `Field` function.
  223. Args:
  224. default: The default value for the field. Defaults to Undefined.
  225. **kwargs: Additional arguments dictionary.
  226. Raises:
  227. TypeError: If 'annotation' is passed as a keyword argument.
  228. Returns:
  229. A new FieldInfo object with the given parameters.
  230. Example:
  231. This is how you can create a field with default value like this:
  232. ```python
  233. import pydantic
  234. class MyModel(pydantic.BaseModel):
  235. foo: int = pydantic.Field(4)
  236. ```
  237. """
  238. if 'annotation' in kwargs:
  239. raise TypeError('"annotation" is not permitted as a Field keyword argument')
  240. return FieldInfo(default=default, **kwargs)
  241. @staticmethod
  242. def from_annotation(annotation: type[Any]) -> FieldInfo:
  243. """Creates a `FieldInfo` instance from a bare annotation.
  244. This function is used internally to create a `FieldInfo` from a bare annotation like this:
  245. ```python
  246. import pydantic
  247. class MyModel(pydantic.BaseModel):
  248. foo: int # <-- like this
  249. ```
  250. We also account for the case where the annotation can be an instance of `Annotated` and where
  251. one of the (not first) arguments in `Annotated` is an instance of `FieldInfo`, e.g.:
  252. ```python
  253. import annotated_types
  254. from typing_extensions import Annotated
  255. import pydantic
  256. class MyModel(pydantic.BaseModel):
  257. foo: Annotated[int, annotated_types.Gt(42)]
  258. bar: Annotated[int, pydantic.Field(gt=42)]
  259. ```
  260. Args:
  261. annotation: An annotation object.
  262. Returns:
  263. An instance of the field metadata.
  264. """
  265. final = False
  266. if _typing_extra.is_finalvar(annotation):
  267. final = True
  268. if annotation is not typing_extensions.Final:
  269. annotation = typing_extensions.get_args(annotation)[0]
  270. if _typing_extra.is_annotated(annotation):
  271. first_arg, *extra_args = typing_extensions.get_args(annotation)
  272. if _typing_extra.is_finalvar(first_arg):
  273. final = True
  274. field_info_annotations = [a for a in extra_args if isinstance(a, FieldInfo)]
  275. field_info = FieldInfo.merge_field_infos(*field_info_annotations, annotation=first_arg)
  276. if field_info:
  277. new_field_info = copy(field_info)
  278. new_field_info.annotation = first_arg
  279. new_field_info.frozen = final or field_info.frozen
  280. metadata: list[Any] = []
  281. for a in extra_args:
  282. if _typing_extra.is_deprecated_instance(a):
  283. new_field_info.deprecated = a.message
  284. elif not isinstance(a, FieldInfo):
  285. metadata.append(a)
  286. else:
  287. metadata.extend(a.metadata)
  288. new_field_info.metadata = metadata
  289. return new_field_info
  290. return FieldInfo(annotation=annotation, frozen=final or None) # pyright: ignore[reportArgumentType]
  291. @staticmethod
  292. def from_annotated_attribute(annotation: type[Any], default: Any) -> FieldInfo:
  293. """Create `FieldInfo` from an annotation with a default value.
  294. This is used in cases like the following:
  295. ```python
  296. import annotated_types
  297. from typing_extensions import Annotated
  298. import pydantic
  299. class MyModel(pydantic.BaseModel):
  300. foo: int = 4 # <-- like this
  301. bar: Annotated[int, annotated_types.Gt(4)] = 4 # <-- or this
  302. spam: Annotated[int, pydantic.Field(gt=4)] = 4 # <-- or this
  303. ```
  304. Args:
  305. annotation: The type annotation of the field.
  306. default: The default value of the field.
  307. Returns:
  308. A field object with the passed values.
  309. """
  310. if annotation is default:
  311. raise PydanticUserError(
  312. 'Error when building FieldInfo from annotated attribute. '
  313. "Make sure you don't have any field name clashing with a type annotation ",
  314. code='unevaluable-type-annotation',
  315. )
  316. final = _typing_extra.is_finalvar(annotation)
  317. if final and annotation is not typing_extensions.Final:
  318. annotation = typing_extensions.get_args(annotation)[0]
  319. if isinstance(default, FieldInfo):
  320. default.annotation, annotation_metadata = FieldInfo._extract_metadata(annotation) # pyright: ignore[reportArgumentType]
  321. default.metadata += annotation_metadata
  322. default = default.merge_field_infos(
  323. *[x for x in annotation_metadata if isinstance(x, FieldInfo)], default, annotation=default.annotation
  324. )
  325. default.frozen = final or default.frozen
  326. return default
  327. if isinstance(default, dataclasses.Field):
  328. init_var = False
  329. if annotation is dataclasses.InitVar:
  330. init_var = True
  331. annotation = typing.cast(Any, Any)
  332. elif isinstance(annotation, dataclasses.InitVar):
  333. init_var = True
  334. annotation = annotation.type
  335. pydantic_field = FieldInfo._from_dataclass_field(default)
  336. pydantic_field.annotation, annotation_metadata = FieldInfo._extract_metadata(annotation) # pyright: ignore[reportArgumentType]
  337. pydantic_field.metadata += annotation_metadata
  338. pydantic_field = pydantic_field.merge_field_infos(
  339. *[x for x in annotation_metadata if isinstance(x, FieldInfo)],
  340. pydantic_field,
  341. annotation=pydantic_field.annotation,
  342. )
  343. pydantic_field.frozen = final or pydantic_field.frozen
  344. pydantic_field.init_var = init_var
  345. pydantic_field.init = getattr(default, 'init', None)
  346. pydantic_field.kw_only = getattr(default, 'kw_only', None)
  347. return pydantic_field
  348. if _typing_extra.is_annotated(annotation):
  349. first_arg, *extra_args = typing_extensions.get_args(annotation)
  350. field_infos = [a for a in extra_args if isinstance(a, FieldInfo)]
  351. field_info = FieldInfo.merge_field_infos(*field_infos, annotation=first_arg, default=default)
  352. metadata: list[Any] = []
  353. for a in extra_args:
  354. if _typing_extra.is_deprecated_instance(a):
  355. field_info.deprecated = a.message
  356. elif not isinstance(a, FieldInfo):
  357. metadata.append(a)
  358. else:
  359. metadata.extend(a.metadata)
  360. field_info.metadata = metadata
  361. return field_info
  362. return FieldInfo(annotation=annotation, default=default, frozen=final or None) # pyright: ignore[reportArgumentType]
  363. @staticmethod
  364. def merge_field_infos(*field_infos: FieldInfo, **overrides: Any) -> FieldInfo:
  365. """Merge `FieldInfo` instances keeping only explicitly set attributes.
  366. Later `FieldInfo` instances override earlier ones.
  367. Returns:
  368. FieldInfo: A merged FieldInfo instance.
  369. """
  370. if len(field_infos) == 1:
  371. # No merging necessary, but we still need to make a copy and apply the overrides
  372. field_info = copy(field_infos[0])
  373. field_info._attributes_set.update(overrides)
  374. default_override = overrides.pop('default', PydanticUndefined)
  375. if default_override is Ellipsis:
  376. default_override = PydanticUndefined
  377. if default_override is not PydanticUndefined:
  378. field_info.default = default_override
  379. for k, v in overrides.items():
  380. setattr(field_info, k, v)
  381. return field_info # type: ignore
  382. merged_field_info_kwargs: dict[str, Any] = {}
  383. metadata = {}
  384. for field_info in field_infos:
  385. attributes_set = field_info._attributes_set.copy()
  386. try:
  387. json_schema_extra = attributes_set.pop('json_schema_extra')
  388. existing_json_schema_extra = merged_field_info_kwargs.get('json_schema_extra')
  389. if existing_json_schema_extra is None:
  390. merged_field_info_kwargs['json_schema_extra'] = json_schema_extra
  391. if isinstance(existing_json_schema_extra, dict):
  392. if isinstance(json_schema_extra, dict):
  393. merged_field_info_kwargs['json_schema_extra'] = {
  394. **existing_json_schema_extra,
  395. **json_schema_extra,
  396. }
  397. if callable(json_schema_extra):
  398. warn(
  399. 'Composing `dict` and `callable` type `json_schema_extra` is not supported.'
  400. 'The `callable` type is being ignored.'
  401. "If you'd like support for this behavior, please open an issue on pydantic.",
  402. PydanticJsonSchemaWarning,
  403. )
  404. elif callable(json_schema_extra):
  405. # if ever there's a case of a callable, we'll just keep the last json schema extra spec
  406. merged_field_info_kwargs['json_schema_extra'] = json_schema_extra
  407. except KeyError:
  408. pass
  409. # later FieldInfo instances override everything except json_schema_extra from earlier FieldInfo instances
  410. merged_field_info_kwargs.update(attributes_set)
  411. for x in field_info.metadata:
  412. if not isinstance(x, FieldInfo):
  413. metadata[type(x)] = x
  414. merged_field_info_kwargs.update(overrides)
  415. field_info = FieldInfo(**merged_field_info_kwargs)
  416. field_info.metadata = list(metadata.values())
  417. return field_info
  418. @staticmethod
  419. def _from_dataclass_field(dc_field: DataclassField[Any]) -> FieldInfo:
  420. """Return a new `FieldInfo` instance from a `dataclasses.Field` instance.
  421. Args:
  422. dc_field: The `dataclasses.Field` instance to convert.
  423. Returns:
  424. The corresponding `FieldInfo` instance.
  425. Raises:
  426. TypeError: If any of the `FieldInfo` kwargs does not match the `dataclass.Field` kwargs.
  427. """
  428. default = dc_field.default
  429. if default is dataclasses.MISSING:
  430. default = _Unset
  431. if dc_field.default_factory is dataclasses.MISSING:
  432. default_factory = _Unset
  433. else:
  434. default_factory = dc_field.default_factory
  435. # use the `Field` function so in correct kwargs raise the correct `TypeError`
  436. dc_field_metadata = {k: v for k, v in dc_field.metadata.items() if k in _FIELD_ARG_NAMES}
  437. return Field(default=default, default_factory=default_factory, repr=dc_field.repr, **dc_field_metadata) # pyright: ignore[reportCallIssue]
  438. @staticmethod
  439. def _extract_metadata(annotation: type[Any] | None) -> tuple[type[Any] | None, list[Any]]:
  440. """Tries to extract metadata/constraints from an annotation if it uses `Annotated`.
  441. Args:
  442. annotation: The type hint annotation for which metadata has to be extracted.
  443. Returns:
  444. A tuple containing the extracted metadata type and the list of extra arguments.
  445. """
  446. if annotation is not None:
  447. if _typing_extra.is_annotated(annotation):
  448. first_arg, *extra_args = typing_extensions.get_args(annotation)
  449. return first_arg, list(extra_args)
  450. return annotation, []
  451. @staticmethod
  452. def _collect_metadata(kwargs: dict[str, Any]) -> list[Any]:
  453. """Collect annotations from kwargs.
  454. Args:
  455. kwargs: Keyword arguments passed to the function.
  456. Returns:
  457. A list of metadata objects - a combination of `annotated_types.BaseMetadata` and
  458. `PydanticMetadata`.
  459. """
  460. metadata: list[Any] = []
  461. general_metadata = {}
  462. for key, value in list(kwargs.items()):
  463. try:
  464. marker = FieldInfo.metadata_lookup[key]
  465. except KeyError:
  466. continue
  467. del kwargs[key]
  468. if value is not None:
  469. if marker is None:
  470. general_metadata[key] = value
  471. else:
  472. metadata.append(marker(value))
  473. if general_metadata:
  474. metadata.append(_fields.pydantic_general_metadata(**general_metadata))
  475. return metadata
  476. @property
  477. def deprecation_message(self) -> str | None:
  478. """The deprecation message to be emitted, or `None` if not set."""
  479. if self.deprecated is None:
  480. return None
  481. if isinstance(self.deprecated, bool):
  482. return 'deprecated' if self.deprecated else None
  483. return self.deprecated if isinstance(self.deprecated, str) else self.deprecated.message
  484. @property
  485. def default_factory_takes_validated_data(self) -> bool | None:
  486. """Whether the provided default factory callable has a validated data parameter.
  487. Returns `None` if no default factory is set.
  488. """
  489. if self.default_factory is not None:
  490. return _fields.takes_validated_data_argument(self.default_factory)
  491. @overload
  492. def get_default(
  493. self, *, call_default_factory: Literal[True], validated_data: dict[str, Any] | None = None
  494. ) -> Any: ...
  495. @overload
  496. def get_default(self, *, call_default_factory: Literal[False] = ...) -> Any: ...
  497. def get_default(self, *, call_default_factory: bool = False, validated_data: dict[str, Any] | None = None) -> Any:
  498. """Get the default value.
  499. We expose an option for whether to call the default_factory (if present), as calling it may
  500. result in side effects that we want to avoid. However, there are times when it really should
  501. be called (namely, when instantiating a model via `model_construct`).
  502. Args:
  503. call_default_factory: Whether to call the default factory or not.
  504. validated_data: The already validated data to be passed to the default factory.
  505. Returns:
  506. The default value, calling the default factory if requested or `None` if not set.
  507. """
  508. if self.default_factory is None:
  509. return _utils.smart_deepcopy(self.default)
  510. elif call_default_factory:
  511. if self.default_factory_takes_validated_data:
  512. fac = cast('Callable[[dict[str, Any]], Any]', self.default_factory)
  513. if validated_data is None:
  514. raise ValueError(
  515. "The default factory requires the 'validated_data' argument, which was not provided when calling 'get_default'."
  516. )
  517. return fac(validated_data)
  518. else:
  519. fac = cast('Callable[[], Any]', self.default_factory)
  520. return fac()
  521. else:
  522. return None
  523. def is_required(self) -> bool:
  524. """Check if the field is required (i.e., does not have a default value or factory).
  525. Returns:
  526. `True` if the field is required, `False` otherwise.
  527. """
  528. return self.default is PydanticUndefined and self.default_factory is None
  529. def rebuild_annotation(self) -> Any:
  530. """Attempts to rebuild the original annotation for use in function signatures.
  531. If metadata is present, it adds it to the original annotation using
  532. `Annotated`. Otherwise, it returns the original annotation as-is.
  533. Note that because the metadata has been flattened, the original annotation
  534. may not be reconstructed exactly as originally provided, e.g. if the original
  535. type had unrecognized annotations, or was annotated with a call to `pydantic.Field`.
  536. Returns:
  537. The rebuilt annotation.
  538. """
  539. if not self.metadata:
  540. return self.annotation
  541. else:
  542. # Annotated arguments must be a tuple
  543. return typing_extensions.Annotated[(self.annotation, *self.metadata)] # type: ignore
  544. def apply_typevars_map(
  545. self,
  546. typevars_map: dict[Any, Any] | None,
  547. globalns: GlobalsNamespace | None = None,
  548. localns: MappingNamespace | None = None,
  549. ) -> None:
  550. """Apply a `typevars_map` to the annotation.
  551. This method is used when analyzing parametrized generic types to replace typevars with their concrete types.
  552. This method applies the `typevars_map` to the annotation in place.
  553. Args:
  554. typevars_map: A dictionary mapping type variables to their concrete types.
  555. globalns: The globals namespace to use during type annotation evaluation.
  556. localns: The locals namespace to use during type annotation evaluation.
  557. See Also:
  558. pydantic._internal._generics.replace_types is used for replacing the typevars with
  559. their concrete types.
  560. """
  561. annotation, _ = _typing_extra.try_eval_type(self.annotation, globalns, localns)
  562. self.annotation = _generics.replace_types(annotation, typevars_map)
  563. def __repr_args__(self) -> ReprArgs:
  564. yield 'annotation', _repr.PlainRepr(_repr.display_as_type(self.annotation))
  565. yield 'required', self.is_required()
  566. for s in self.__slots__:
  567. # TODO: properly make use of the protocol (https://rich.readthedocs.io/en/stable/pretty.html#rich-repr-protocol)
  568. # By yielding a three-tuple:
  569. if s in ('_attributes_set', 'annotation', 'evaluated'):
  570. continue
  571. elif s == 'metadata' and not self.metadata:
  572. continue
  573. elif s == 'repr' and self.repr is True:
  574. continue
  575. if s == 'frozen' and self.frozen is False:
  576. continue
  577. if s == 'validation_alias' and self.validation_alias == self.alias:
  578. continue
  579. if s == 'serialization_alias' and self.serialization_alias == self.alias:
  580. continue
  581. if s == 'default' and self.default is not PydanticUndefined:
  582. yield 'default', self.default
  583. elif s == 'default_factory' and self.default_factory is not None:
  584. yield 'default_factory', _repr.PlainRepr(_repr.display_as_type(self.default_factory))
  585. else:
  586. value = getattr(self, s)
  587. if value is not None and value is not PydanticUndefined:
  588. yield s, value
  589. class _EmptyKwargs(typing_extensions.TypedDict):
  590. """This class exists solely to ensure that type checking warns about passing `**extra` in `Field`."""
  591. _DefaultValues = {
  592. 'default': ...,
  593. 'default_factory': None,
  594. 'alias': None,
  595. 'alias_priority': None,
  596. 'validation_alias': None,
  597. 'serialization_alias': None,
  598. 'title': None,
  599. 'description': None,
  600. 'examples': None,
  601. 'exclude': None,
  602. 'discriminator': None,
  603. 'json_schema_extra': None,
  604. 'frozen': None,
  605. 'validate_default': None,
  606. 'repr': True,
  607. 'init': None,
  608. 'init_var': None,
  609. 'kw_only': None,
  610. 'pattern': None,
  611. 'strict': None,
  612. 'gt': None,
  613. 'ge': None,
  614. 'lt': None,
  615. 'le': None,
  616. 'multiple_of': None,
  617. 'allow_inf_nan': None,
  618. 'max_digits': None,
  619. 'decimal_places': None,
  620. 'min_length': None,
  621. 'max_length': None,
  622. 'coerce_numbers_to_str': None,
  623. }
  624. _T = TypeVar('_T')
  625. # NOTE: Actual return type is 'FieldInfo', but we want to help type checkers
  626. # to understand the magic that happens at runtime with the following overloads:
  627. @overload # type hint the return value as `Any` to avoid type checking regressions when using `...`.
  628. def Field(
  629. default: ellipsis, # noqa: F821 # TODO: use `_typing_extra.EllipsisType` when we drop Py3.9
  630. *,
  631. alias: str | None = _Unset,
  632. alias_priority: int | None = _Unset,
  633. validation_alias: str | AliasPath | AliasChoices | None = _Unset,
  634. serialization_alias: str | None = _Unset,
  635. title: str | None = _Unset,
  636. field_title_generator: Callable[[str, FieldInfo], str] | None = _Unset,
  637. description: str | None = _Unset,
  638. examples: list[Any] | None = _Unset,
  639. exclude: bool | None = _Unset,
  640. discriminator: str | types.Discriminator | None = _Unset,
  641. deprecated: Deprecated | str | bool | None = _Unset,
  642. json_schema_extra: JsonDict | Callable[[JsonDict], None] | None = _Unset,
  643. frozen: bool | None = _Unset,
  644. validate_default: bool | None = _Unset,
  645. repr: bool = _Unset,
  646. init: bool | None = _Unset,
  647. init_var: bool | None = _Unset,
  648. kw_only: bool | None = _Unset,
  649. pattern: str | typing.Pattern[str] | None = _Unset,
  650. strict: bool | None = _Unset,
  651. coerce_numbers_to_str: bool | None = _Unset,
  652. gt: annotated_types.SupportsGt | None = _Unset,
  653. ge: annotated_types.SupportsGe | None = _Unset,
  654. lt: annotated_types.SupportsLt | None = _Unset,
  655. le: annotated_types.SupportsLe | None = _Unset,
  656. multiple_of: float | None = _Unset,
  657. allow_inf_nan: bool | None = _Unset,
  658. max_digits: int | None = _Unset,
  659. decimal_places: int | None = _Unset,
  660. min_length: int | None = _Unset,
  661. max_length: int | None = _Unset,
  662. union_mode: Literal['smart', 'left_to_right'] = _Unset,
  663. fail_fast: bool | None = _Unset,
  664. **extra: Unpack[_EmptyKwargs],
  665. ) -> Any: ...
  666. @overload # `default` argument set
  667. def Field(
  668. default: _T,
  669. *,
  670. alias: str | None = _Unset,
  671. alias_priority: int | None = _Unset,
  672. validation_alias: str | AliasPath | AliasChoices | None = _Unset,
  673. serialization_alias: str | None = _Unset,
  674. title: str | None = _Unset,
  675. field_title_generator: Callable[[str, FieldInfo], str] | None = _Unset,
  676. description: str | None = _Unset,
  677. examples: list[Any] | None = _Unset,
  678. exclude: bool | None = _Unset,
  679. discriminator: str | types.Discriminator | None = _Unset,
  680. deprecated: Deprecated | str | bool | None = _Unset,
  681. json_schema_extra: JsonDict | Callable[[JsonDict], None] | None = _Unset,
  682. frozen: bool | None = _Unset,
  683. validate_default: bool | None = _Unset,
  684. repr: bool = _Unset,
  685. init: bool | None = _Unset,
  686. init_var: bool | None = _Unset,
  687. kw_only: bool | None = _Unset,
  688. pattern: str | typing.Pattern[str] | None = _Unset,
  689. strict: bool | None = _Unset,
  690. coerce_numbers_to_str: bool | None = _Unset,
  691. gt: annotated_types.SupportsGt | None = _Unset,
  692. ge: annotated_types.SupportsGe | None = _Unset,
  693. lt: annotated_types.SupportsLt | None = _Unset,
  694. le: annotated_types.SupportsLe | None = _Unset,
  695. multiple_of: float | None = _Unset,
  696. allow_inf_nan: bool | None = _Unset,
  697. max_digits: int | None = _Unset,
  698. decimal_places: int | None = _Unset,
  699. min_length: int | None = _Unset,
  700. max_length: int | None = _Unset,
  701. union_mode: Literal['smart', 'left_to_right'] = _Unset,
  702. fail_fast: bool | None = _Unset,
  703. **extra: Unpack[_EmptyKwargs],
  704. ) -> _T: ...
  705. @overload # `default_factory` argument set
  706. def Field(
  707. *,
  708. default_factory: Callable[[], _T] | Callable[[dict[str, Any]], _T],
  709. alias: str | None = _Unset,
  710. alias_priority: int | None = _Unset,
  711. validation_alias: str | AliasPath | AliasChoices | None = _Unset,
  712. serialization_alias: str | None = _Unset,
  713. title: str | None = _Unset,
  714. field_title_generator: Callable[[str, FieldInfo], str] | None = _Unset,
  715. description: str | None = _Unset,
  716. examples: list[Any] | None = _Unset,
  717. exclude: bool | None = _Unset,
  718. discriminator: str | types.Discriminator | None = _Unset,
  719. deprecated: Deprecated | str | bool | None = _Unset,
  720. json_schema_extra: JsonDict | Callable[[JsonDict], None] | None = _Unset,
  721. frozen: bool | None = _Unset,
  722. validate_default: bool | None = _Unset,
  723. repr: bool = _Unset,
  724. init: bool | None = _Unset,
  725. init_var: bool | None = _Unset,
  726. kw_only: bool | None = _Unset,
  727. pattern: str | typing.Pattern[str] | None = _Unset,
  728. strict: bool | None = _Unset,
  729. coerce_numbers_to_str: bool | None = _Unset,
  730. gt: annotated_types.SupportsGt | None = _Unset,
  731. ge: annotated_types.SupportsGe | None = _Unset,
  732. lt: annotated_types.SupportsLt | None = _Unset,
  733. le: annotated_types.SupportsLe | None = _Unset,
  734. multiple_of: float | None = _Unset,
  735. allow_inf_nan: bool | None = _Unset,
  736. max_digits: int | None = _Unset,
  737. decimal_places: int | None = _Unset,
  738. min_length: int | None = _Unset,
  739. max_length: int | None = _Unset,
  740. union_mode: Literal['smart', 'left_to_right'] = _Unset,
  741. fail_fast: bool | None = _Unset,
  742. **extra: Unpack[_EmptyKwargs],
  743. ) -> _T: ...
  744. @overload
  745. def Field( # No default set
  746. *,
  747. alias: str | None = _Unset,
  748. alias_priority: int | None = _Unset,
  749. validation_alias: str | AliasPath | AliasChoices | None = _Unset,
  750. serialization_alias: str | None = _Unset,
  751. title: str | None = _Unset,
  752. field_title_generator: Callable[[str, FieldInfo], str] | None = _Unset,
  753. description: str | None = _Unset,
  754. examples: list[Any] | None = _Unset,
  755. exclude: bool | None = _Unset,
  756. discriminator: str | types.Discriminator | None = _Unset,
  757. deprecated: Deprecated | str | bool | None = _Unset,
  758. json_schema_extra: JsonDict | Callable[[JsonDict], None] | None = _Unset,
  759. frozen: bool | None = _Unset,
  760. validate_default: bool | None = _Unset,
  761. repr: bool = _Unset,
  762. init: bool | None = _Unset,
  763. init_var: bool | None = _Unset,
  764. kw_only: bool | None = _Unset,
  765. pattern: str | typing.Pattern[str] | None = _Unset,
  766. strict: bool | None = _Unset,
  767. coerce_numbers_to_str: bool | None = _Unset,
  768. gt: annotated_types.SupportsGt | None = _Unset,
  769. ge: annotated_types.SupportsGe | None = _Unset,
  770. lt: annotated_types.SupportsLt | None = _Unset,
  771. le: annotated_types.SupportsLe | None = _Unset,
  772. multiple_of: float | None = _Unset,
  773. allow_inf_nan: bool | None = _Unset,
  774. max_digits: int | None = _Unset,
  775. decimal_places: int | None = _Unset,
  776. min_length: int | None = _Unset,
  777. max_length: int | None = _Unset,
  778. union_mode: Literal['smart', 'left_to_right'] = _Unset,
  779. fail_fast: bool | None = _Unset,
  780. **extra: Unpack[_EmptyKwargs],
  781. ) -> Any: ...
  782. def Field( # noqa: C901
  783. default: Any = PydanticUndefined,
  784. *,
  785. default_factory: Callable[[], Any] | Callable[[dict[str, Any]], Any] | None = _Unset,
  786. alias: str | None = _Unset,
  787. alias_priority: int | None = _Unset,
  788. validation_alias: str | AliasPath | AliasChoices | None = _Unset,
  789. serialization_alias: str | None = _Unset,
  790. title: str | None = _Unset,
  791. field_title_generator: Callable[[str, FieldInfo], str] | None = _Unset,
  792. description: str | None = _Unset,
  793. examples: list[Any] | None = _Unset,
  794. exclude: bool | None = _Unset,
  795. discriminator: str | types.Discriminator | None = _Unset,
  796. deprecated: Deprecated | str | bool | None = _Unset,
  797. json_schema_extra: JsonDict | Callable[[JsonDict], None] | None = _Unset,
  798. frozen: bool | None = _Unset,
  799. validate_default: bool | None = _Unset,
  800. repr: bool = _Unset,
  801. init: bool | None = _Unset,
  802. init_var: bool | None = _Unset,
  803. kw_only: bool | None = _Unset,
  804. pattern: str | typing.Pattern[str] | None = _Unset,
  805. strict: bool | None = _Unset,
  806. coerce_numbers_to_str: bool | None = _Unset,
  807. gt: annotated_types.SupportsGt | None = _Unset,
  808. ge: annotated_types.SupportsGe | None = _Unset,
  809. lt: annotated_types.SupportsLt | None = _Unset,
  810. le: annotated_types.SupportsLe | None = _Unset,
  811. multiple_of: float | None = _Unset,
  812. allow_inf_nan: bool | None = _Unset,
  813. max_digits: int | None = _Unset,
  814. decimal_places: int | None = _Unset,
  815. min_length: int | None = _Unset,
  816. max_length: int | None = _Unset,
  817. union_mode: Literal['smart', 'left_to_right'] = _Unset,
  818. fail_fast: bool | None = _Unset,
  819. **extra: Unpack[_EmptyKwargs],
  820. ) -> Any:
  821. """Usage docs: https://docs.pydantic.dev/2.10/concepts/fields
  822. Create a field for objects that can be configured.
  823. Used to provide extra information about a field, either for the model schema or complex validation. Some arguments
  824. apply only to number fields (`int`, `float`, `Decimal`) and some apply only to `str`.
  825. Note:
  826. - 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`
  827. Args:
  828. default: Default value if the field is not set.
  829. default_factory: A callable to generate the default value. The callable can either take 0 arguments
  830. (in which case it is called as is) or a single argument containing the already validated data.
  831. alias: The name to use for the attribute when validating or serializing by alias.
  832. This is often used for things like converting between snake and camel case.
  833. alias_priority: Priority of the alias. This affects whether an alias generator is used.
  834. validation_alias: Like `alias`, but only affects validation, not serialization.
  835. serialization_alias: Like `alias`, but only affects serialization, not validation.
  836. title: Human-readable title.
  837. field_title_generator: A callable that takes a field name and returns title for it.
  838. description: Human-readable description.
  839. examples: Example values for this field.
  840. exclude: Whether to exclude the field from the model serialization.
  841. discriminator: Field name or Discriminator for discriminating the type in a tagged union.
  842. deprecated: A deprecation message, an instance of `warnings.deprecated` or the `typing_extensions.deprecated` backport,
  843. or a boolean. If `True`, a default deprecation message will be emitted when accessing the field.
  844. json_schema_extra: A dict or callable to provide extra JSON schema properties.
  845. frozen: Whether the field is frozen. If true, attempts to change the value on an instance will raise an error.
  846. validate_default: If `True`, apply validation to the default value every time you create an instance.
  847. Otherwise, for performance reasons, the default value of the field is trusted and not validated.
  848. repr: A boolean indicating whether to include the field in the `__repr__` output.
  849. init: Whether the field should be included in the constructor of the dataclass.
  850. (Only applies to dataclasses.)
  851. init_var: Whether the field should _only_ be included in the constructor of the dataclass.
  852. (Only applies to dataclasses.)
  853. kw_only: Whether the field should be a keyword-only argument in the constructor of the dataclass.
  854. (Only applies to dataclasses.)
  855. coerce_numbers_to_str: Whether to enable coercion of any `Number` type to `str` (not applicable in `strict` mode).
  856. strict: If `True`, strict validation is applied to the field.
  857. See [Strict Mode](../concepts/strict_mode.md) for details.
  858. gt: Greater than. If set, value must be greater than this. Only applicable to numbers.
  859. ge: Greater than or equal. If set, value must be greater than or equal to this. Only applicable to numbers.
  860. lt: Less than. If set, value must be less than this. Only applicable to numbers.
  861. le: Less than or equal. If set, value must be less than or equal to this. Only applicable to numbers.
  862. multiple_of: Value must be a multiple of this. Only applicable to numbers.
  863. min_length: Minimum length for iterables.
  864. max_length: Maximum length for iterables.
  865. pattern: Pattern for strings (a regular expression).
  866. allow_inf_nan: Allow `inf`, `-inf`, `nan`. Only applicable to numbers.
  867. max_digits: Maximum number of allow digits for strings.
  868. decimal_places: Maximum number of decimal places allowed for numbers.
  869. union_mode: The strategy to apply when validating a union. Can be `smart` (the default), or `left_to_right`.
  870. See [Union Mode](../concepts/unions.md#union-modes) for details.
  871. fail_fast: If `True`, validation will stop on the first error. If `False`, all validation errors will be collected.
  872. This option can be applied only to iterable types (list, tuple, set, and frozenset).
  873. extra: (Deprecated) Extra fields that will be included in the JSON schema.
  874. !!! warning Deprecated
  875. The `extra` kwargs is deprecated. Use `json_schema_extra` instead.
  876. Returns:
  877. A new [`FieldInfo`][pydantic.fields.FieldInfo]. The return annotation is `Any` so `Field` can be used on
  878. type-annotated fields without causing a type error.
  879. """
  880. # Check deprecated and removed params from V1. This logic should eventually be removed.
  881. const = extra.pop('const', None) # type: ignore
  882. if const is not None:
  883. raise PydanticUserError('`const` is removed, use `Literal` instead', code='removed-kwargs')
  884. min_items = extra.pop('min_items', None) # type: ignore
  885. if min_items is not None:
  886. warn('`min_items` is deprecated and will be removed, use `min_length` instead', DeprecationWarning)
  887. if min_length in (None, _Unset):
  888. min_length = min_items # type: ignore
  889. max_items = extra.pop('max_items', None) # type: ignore
  890. if max_items is not None:
  891. warn('`max_items` is deprecated and will be removed, use `max_length` instead', DeprecationWarning)
  892. if max_length in (None, _Unset):
  893. max_length = max_items # type: ignore
  894. unique_items = extra.pop('unique_items', None) # type: ignore
  895. if unique_items is not None:
  896. raise PydanticUserError(
  897. (
  898. '`unique_items` is removed, use `Set` instead'
  899. '(this feature is discussed in https://github.com/pydantic/pydantic-core/issues/296)'
  900. ),
  901. code='removed-kwargs',
  902. )
  903. allow_mutation = extra.pop('allow_mutation', None) # type: ignore
  904. if allow_mutation is not None:
  905. warn('`allow_mutation` is deprecated and will be removed. use `frozen` instead', DeprecationWarning)
  906. if allow_mutation is False:
  907. frozen = True
  908. regex = extra.pop('regex', None) # type: ignore
  909. if regex is not None:
  910. raise PydanticUserError('`regex` is removed. use `pattern` instead', code='removed-kwargs')
  911. if extra:
  912. warn(
  913. 'Using extra keyword arguments on `Field` is deprecated and will be removed.'
  914. ' Use `json_schema_extra` instead.'
  915. f' (Extra keys: {", ".join(k.__repr__() for k in extra.keys())})',
  916. DeprecationWarning,
  917. )
  918. if not json_schema_extra or json_schema_extra is _Unset:
  919. json_schema_extra = extra # type: ignore
  920. if (
  921. validation_alias
  922. and validation_alias is not _Unset
  923. and not isinstance(validation_alias, (str, AliasChoices, AliasPath))
  924. ):
  925. raise TypeError('Invalid `validation_alias` type. it should be `str`, `AliasChoices`, or `AliasPath`')
  926. if serialization_alias in (_Unset, None) and isinstance(alias, str):
  927. serialization_alias = alias
  928. if validation_alias in (_Unset, None):
  929. validation_alias = alias
  930. include = extra.pop('include', None) # type: ignore
  931. if include is not None:
  932. warn('`include` is deprecated and does nothing. It will be removed, use `exclude` instead', DeprecationWarning)
  933. return FieldInfo.from_field(
  934. default,
  935. default_factory=default_factory,
  936. alias=alias,
  937. alias_priority=alias_priority,
  938. validation_alias=validation_alias,
  939. serialization_alias=serialization_alias,
  940. title=title,
  941. field_title_generator=field_title_generator,
  942. description=description,
  943. examples=examples,
  944. exclude=exclude,
  945. discriminator=discriminator,
  946. deprecated=deprecated,
  947. json_schema_extra=json_schema_extra,
  948. frozen=frozen,
  949. pattern=pattern,
  950. validate_default=validate_default,
  951. repr=repr,
  952. init=init,
  953. init_var=init_var,
  954. kw_only=kw_only,
  955. coerce_numbers_to_str=coerce_numbers_to_str,
  956. strict=strict,
  957. gt=gt,
  958. ge=ge,
  959. lt=lt,
  960. le=le,
  961. multiple_of=multiple_of,
  962. min_length=min_length,
  963. max_length=max_length,
  964. allow_inf_nan=allow_inf_nan,
  965. max_digits=max_digits,
  966. decimal_places=decimal_places,
  967. union_mode=union_mode,
  968. fail_fast=fail_fast,
  969. )
  970. _FIELD_ARG_NAMES = set(inspect.signature(Field).parameters)
  971. _FIELD_ARG_NAMES.remove('extra') # do not include the varkwargs parameter
  972. class ModelPrivateAttr(_repr.Representation):
  973. """A descriptor for private attributes in class models.
  974. !!! warning
  975. You generally shouldn't be creating `ModelPrivateAttr` instances directly, instead use
  976. `pydantic.fields.PrivateAttr`. (This is similar to `FieldInfo` vs. `Field`.)
  977. Attributes:
  978. default: The default value of the attribute if not provided.
  979. default_factory: A callable function that generates the default value of the
  980. attribute if not provided.
  981. """
  982. __slots__ = ('default', 'default_factory')
  983. def __init__(
  984. self, default: Any = PydanticUndefined, *, default_factory: typing.Callable[[], Any] | None = None
  985. ) -> None:
  986. if default is Ellipsis:
  987. self.default = PydanticUndefined
  988. else:
  989. self.default = default
  990. self.default_factory = default_factory
  991. if not typing.TYPE_CHECKING:
  992. # We put `__getattr__` in a non-TYPE_CHECKING block because otherwise, mypy allows arbitrary attribute access
  993. def __getattr__(self, item: str) -> Any:
  994. """This function improves compatibility with custom descriptors by ensuring delegation happens
  995. as expected when the default value of a private attribute is a descriptor.
  996. """
  997. if item in {'__get__', '__set__', '__delete__'}:
  998. if hasattr(self.default, item):
  999. return getattr(self.default, item)
  1000. raise AttributeError(f'{type(self).__name__!r} object has no attribute {item!r}')
  1001. def __set_name__(self, cls: type[Any], name: str) -> None:
  1002. """Preserve `__set_name__` protocol defined in https://peps.python.org/pep-0487."""
  1003. default = self.default
  1004. if default is PydanticUndefined:
  1005. return
  1006. set_name = getattr(default, '__set_name__', None)
  1007. if callable(set_name):
  1008. set_name(cls, name)
  1009. def get_default(self) -> Any:
  1010. """Retrieve the default value of the object.
  1011. If `self.default_factory` is `None`, the method will return a deep copy of the `self.default` object.
  1012. If `self.default_factory` is not `None`, it will call `self.default_factory` and return the value returned.
  1013. Returns:
  1014. The default value of the object.
  1015. """
  1016. return _utils.smart_deepcopy(self.default) if self.default_factory is None else self.default_factory()
  1017. def __eq__(self, other: Any) -> bool:
  1018. return isinstance(other, self.__class__) and (self.default, self.default_factory) == (
  1019. other.default,
  1020. other.default_factory,
  1021. )
  1022. # NOTE: Actual return type is 'ModelPrivateAttr', but we want to help type checkers
  1023. # to understand the magic that happens at runtime.
  1024. @overload # `default` argument set
  1025. def PrivateAttr(
  1026. default: _T,
  1027. *,
  1028. init: Literal[False] = False,
  1029. ) -> _T: ...
  1030. @overload # `default_factory` argument set
  1031. def PrivateAttr(
  1032. *,
  1033. default_factory: Callable[[], _T],
  1034. init: Literal[False] = False,
  1035. ) -> _T: ...
  1036. @overload # No default set
  1037. def PrivateAttr(
  1038. *,
  1039. init: Literal[False] = False,
  1040. ) -> Any: ...
  1041. def PrivateAttr(
  1042. default: Any = PydanticUndefined,
  1043. *,
  1044. default_factory: Callable[[], Any] | None = None,
  1045. init: Literal[False] = False,
  1046. ) -> Any:
  1047. """Usage docs: https://docs.pydantic.dev/2.10/concepts/models/#private-model-attributes
  1048. Indicates that an attribute is intended for private use and not handled during normal validation/serialization.
  1049. Private attributes are not validated by Pydantic, so it's up to you to ensure they are used in a type-safe manner.
  1050. Private attributes are stored in `__private_attributes__` on the model.
  1051. Args:
  1052. default: The attribute's default value. Defaults to Undefined.
  1053. default_factory: Callable that will be
  1054. called when a default value is needed for this attribute.
  1055. If both `default` and `default_factory` are set, an error will be raised.
  1056. init: Whether the attribute should be included in the constructor of the dataclass. Always `False`.
  1057. Returns:
  1058. An instance of [`ModelPrivateAttr`][pydantic.fields.ModelPrivateAttr] class.
  1059. Raises:
  1060. ValueError: If both `default` and `default_factory` are set.
  1061. """
  1062. if default is not PydanticUndefined and default_factory is not None:
  1063. raise TypeError('cannot specify both default and default_factory')
  1064. return ModelPrivateAttr(
  1065. default,
  1066. default_factory=default_factory,
  1067. )
  1068. @dataclasses.dataclass(**_internal_dataclass.slots_true)
  1069. class ComputedFieldInfo:
  1070. """A container for data from `@computed_field` so that we can access it while building the pydantic-core schema.
  1071. Attributes:
  1072. decorator_repr: A class variable representing the decorator string, '@computed_field'.
  1073. wrapped_property: The wrapped computed field property.
  1074. return_type: The type of the computed field property's return value.
  1075. alias: The alias of the property to be used during serialization.
  1076. alias_priority: The priority of the alias. This affects whether an alias generator is used.
  1077. title: Title of the computed field to include in the serialization JSON schema.
  1078. field_title_generator: A callable that takes a field name and returns title for it.
  1079. description: Description of the computed field to include in the serialization JSON schema.
  1080. deprecated: A deprecation message, an instance of `warnings.deprecated` or the `typing_extensions.deprecated` backport,
  1081. or a boolean. If `True`, a default deprecation message will be emitted when accessing the field.
  1082. examples: Example values of the computed field to include in the serialization JSON schema.
  1083. json_schema_extra: A dict or callable to provide extra JSON schema properties.
  1084. repr: A boolean indicating whether to include the field in the __repr__ output.
  1085. """
  1086. decorator_repr: ClassVar[str] = '@computed_field'
  1087. wrapped_property: property
  1088. return_type: Any
  1089. alias: str | None
  1090. alias_priority: int | None
  1091. title: str | None
  1092. field_title_generator: typing.Callable[[str, ComputedFieldInfo], str] | None
  1093. description: str | None
  1094. deprecated: Deprecated | str | bool | None
  1095. examples: list[Any] | None
  1096. json_schema_extra: JsonDict | typing.Callable[[JsonDict], None] | None
  1097. repr: bool
  1098. @property
  1099. def deprecation_message(self) -> str | None:
  1100. """The deprecation message to be emitted, or `None` if not set."""
  1101. if self.deprecated is None:
  1102. return None
  1103. if isinstance(self.deprecated, bool):
  1104. return 'deprecated' if self.deprecated else None
  1105. return self.deprecated if isinstance(self.deprecated, str) else self.deprecated.message
  1106. def _wrapped_property_is_private(property_: cached_property | property) -> bool: # type: ignore
  1107. """Returns true if provided property is private, False otherwise."""
  1108. wrapped_name: str = ''
  1109. if isinstance(property_, property):
  1110. wrapped_name = getattr(property_.fget, '__name__', '')
  1111. elif isinstance(property_, cached_property): # type: ignore
  1112. wrapped_name = getattr(property_.func, '__name__', '') # type: ignore
  1113. return wrapped_name.startswith('_') and not wrapped_name.startswith('__')
  1114. # this should really be `property[T], cached_property[T]` but property is not generic unlike cached_property
  1115. # See https://github.com/python/typing/issues/985 and linked issues
  1116. PropertyT = typing.TypeVar('PropertyT')
  1117. @typing.overload
  1118. def computed_field(
  1119. *,
  1120. alias: str | None = None,
  1121. alias_priority: int | None = None,
  1122. title: str | None = None,
  1123. field_title_generator: typing.Callable[[str, ComputedFieldInfo], str] | None = None,
  1124. description: str | None = None,
  1125. deprecated: Deprecated | str | bool | None = None,
  1126. examples: list[Any] | None = None,
  1127. json_schema_extra: JsonDict | typing.Callable[[JsonDict], None] | None = None,
  1128. repr: bool = True,
  1129. return_type: Any = PydanticUndefined,
  1130. ) -> typing.Callable[[PropertyT], PropertyT]: ...
  1131. @typing.overload
  1132. def computed_field(__func: PropertyT) -> PropertyT: ...
  1133. def computed_field(
  1134. func: PropertyT | None = None,
  1135. /,
  1136. *,
  1137. alias: str | None = None,
  1138. alias_priority: int | None = None,
  1139. title: str | None = None,
  1140. field_title_generator: typing.Callable[[str, ComputedFieldInfo], str] | None = None,
  1141. description: str | None = None,
  1142. deprecated: Deprecated | str | bool | None = None,
  1143. examples: list[Any] | None = None,
  1144. json_schema_extra: JsonDict | typing.Callable[[JsonDict], None] | None = None,
  1145. repr: bool | None = None,
  1146. return_type: Any = PydanticUndefined,
  1147. ) -> PropertyT | typing.Callable[[PropertyT], PropertyT]:
  1148. """Usage docs: https://docs.pydantic.dev/2.10/concepts/fields#the-computed_field-decorator
  1149. Decorator to include `property` and `cached_property` when serializing models or dataclasses.
  1150. This is useful for fields that are computed from other fields, or for fields that are expensive to compute and should be cached.
  1151. ```python
  1152. from pydantic import BaseModel, computed_field
  1153. class Rectangle(BaseModel):
  1154. width: int
  1155. length: int
  1156. @computed_field
  1157. @property
  1158. def area(self) -> int:
  1159. return self.width * self.length
  1160. print(Rectangle(width=3, length=2).model_dump())
  1161. #> {'width': 3, 'length': 2, 'area': 6}
  1162. ```
  1163. If applied to functions not yet decorated with `@property` or `@cached_property`, the function is
  1164. automatically wrapped with `property`. Although this is more concise, you will lose IntelliSense in your IDE,
  1165. and confuse static type checkers, thus explicit use of `@property` is recommended.
  1166. !!! warning "Mypy Warning"
  1167. Even with the `@property` or `@cached_property` applied to your function before `@computed_field`,
  1168. mypy may throw a `Decorated property not supported` error.
  1169. See [mypy issue #1362](https://github.com/python/mypy/issues/1362), for more information.
  1170. To avoid this error message, add `# type: ignore[misc]` to the `@computed_field` line.
  1171. [pyright](https://github.com/microsoft/pyright) supports `@computed_field` without error.
  1172. ```python
  1173. import random
  1174. from pydantic import BaseModel, computed_field
  1175. class Square(BaseModel):
  1176. width: float
  1177. @computed_field
  1178. def area(self) -> float: # converted to a `property` by `computed_field`
  1179. return round(self.width**2, 2)
  1180. @area.setter
  1181. def area(self, new_area: float) -> None:
  1182. self.width = new_area**0.5
  1183. @computed_field(alias='the magic number', repr=False)
  1184. def random_number(self) -> int:
  1185. return random.randint(0, 1_000)
  1186. square = Square(width=1.3)
  1187. # `random_number` does not appear in representation
  1188. print(repr(square))
  1189. #> Square(width=1.3, area=1.69)
  1190. print(square.random_number)
  1191. #> 3
  1192. square.area = 4
  1193. print(square.model_dump_json(by_alias=True))
  1194. #> {"width":2.0,"area":4.0,"the magic number":3}
  1195. ```
  1196. !!! warning "Overriding with `computed_field`"
  1197. You can't override a field from a parent class with a `computed_field` in the child class.
  1198. `mypy` complains about this behavior if allowed, and `dataclasses` doesn't allow this pattern either.
  1199. See the example below:
  1200. ```python
  1201. from pydantic import BaseModel, computed_field
  1202. class Parent(BaseModel):
  1203. a: str
  1204. try:
  1205. class Child(Parent):
  1206. @computed_field
  1207. @property
  1208. def a(self) -> str:
  1209. return 'new a'
  1210. except ValueError as e:
  1211. print(repr(e))
  1212. #> ValueError("you can't override a field with a computed field")
  1213. ```
  1214. Private properties decorated with `@computed_field` have `repr=False` by default.
  1215. ```python
  1216. from functools import cached_property
  1217. from pydantic import BaseModel, computed_field
  1218. class Model(BaseModel):
  1219. foo: int
  1220. @computed_field
  1221. @cached_property
  1222. def _private_cached_property(self) -> int:
  1223. return -self.foo
  1224. @computed_field
  1225. @property
  1226. def _private_property(self) -> int:
  1227. return -self.foo
  1228. m = Model(foo=1)
  1229. print(repr(m))
  1230. #> Model(foo=1)
  1231. ```
  1232. Args:
  1233. func: the function to wrap.
  1234. alias: alias to use when serializing this computed field, only used when `by_alias=True`
  1235. alias_priority: priority of the alias. This affects whether an alias generator is used
  1236. title: Title to use when including this computed field in JSON Schema
  1237. field_title_generator: A callable that takes a field name and returns title for it.
  1238. description: Description to use when including this computed field in JSON Schema, defaults to the function's
  1239. docstring
  1240. deprecated: A deprecation message (or an instance of `warnings.deprecated` or the `typing_extensions.deprecated` backport).
  1241. to be emitted when accessing the field. Or a boolean. This will automatically be set if the property is decorated with the
  1242. `deprecated` decorator.
  1243. examples: Example values to use when including this computed field in JSON Schema
  1244. json_schema_extra: A dict or callable to provide extra JSON schema properties.
  1245. repr: whether to include this computed field in model repr.
  1246. Default is `False` for private properties and `True` for public properties.
  1247. return_type: optional return for serialization logic to expect when serializing to JSON, if included
  1248. this must be correct, otherwise a `TypeError` is raised.
  1249. If you don't include a return type Any is used, which does runtime introspection to handle arbitrary
  1250. objects.
  1251. Returns:
  1252. A proxy wrapper for the property.
  1253. """
  1254. def dec(f: Any) -> Any:
  1255. nonlocal description, deprecated, return_type, alias_priority
  1256. unwrapped = _decorators.unwrap_wrapped_function(f)
  1257. if description is None and unwrapped.__doc__:
  1258. description = inspect.cleandoc(unwrapped.__doc__)
  1259. if deprecated is None and hasattr(unwrapped, '__deprecated__'):
  1260. deprecated = unwrapped.__deprecated__
  1261. # if the function isn't already decorated with `@property` (or another descriptor), then we wrap it now
  1262. f = _decorators.ensure_property(f)
  1263. alias_priority = (alias_priority or 2) if alias is not None else None
  1264. if repr is None:
  1265. repr_: bool = not _wrapped_property_is_private(property_=f)
  1266. else:
  1267. repr_ = repr
  1268. dec_info = ComputedFieldInfo(
  1269. f,
  1270. return_type,
  1271. alias,
  1272. alias_priority,
  1273. title,
  1274. field_title_generator,
  1275. description,
  1276. deprecated,
  1277. examples,
  1278. json_schema_extra,
  1279. repr_,
  1280. )
  1281. return _decorators.PydanticDescriptorProxy(f, dec_info)
  1282. if func is None:
  1283. return dec
  1284. else:
  1285. return dec(func)