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

1560 行
63 KiB

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