Coverage for pydantic/fields.py: 91.23%

488 statements  

« prev     ^ index     » next       coverage.py v7.9.2, created at 2025-07-10 09:28 +0000

1"""Defining fields on models.""" 

2 

3from __future__ import annotations as _annotations 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

4 

5import dataclasses 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

6import inspect 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

7import sys 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

8import typing 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

9from collections.abc import Callable, Mapping 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

10from copy import copy 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

11from dataclasses import Field as DataclassField 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

12from functools import cached_property 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

13from typing import Annotated, Any, ClassVar, Literal, TypeVar, cast, final, overload 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

14from warnings import warn 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

15 

16import annotated_types 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

17import typing_extensions 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

18from pydantic_core import PydanticUndefined 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

19from typing_extensions import Self, TypeAlias, Unpack, deprecated 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

20from typing_inspection import typing_objects 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

21from typing_inspection.introspection import UNKNOWN, AnnotationSource, ForbiddenQualifier, Qualifier, inspect_annotation 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

22 

23from . import types 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

24from ._internal import _decorators, _fields, _generics, _internal_dataclass, _repr, _typing_extra, _utils 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

25from ._internal._namespace_utils import GlobalsNamespace, MappingNamespace 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

26from .aliases import AliasChoices, AliasGenerator, AliasPath 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

27from .config import JsonDict 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

28from .errors import PydanticForbiddenQualifier, PydanticUserError 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

29from .json_schema import PydanticJsonSchemaWarning 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

30from .warnings import PydanticDeprecatedSince20 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

31 

32if typing.TYPE_CHECKING: 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

33 from ._internal._config import ConfigWrapper 

34 from ._internal._repr import ReprArgs 1i

35else: 

36 # See PyCharm issues https://youtrack.jetbrains.com/issue/PY-21915 

37 # and https://youtrack.jetbrains.com/issue/PY-51428 

38 DeprecationWarning = PydanticDeprecatedSince20 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

39 

40__all__ = 'Field', 'PrivateAttr', 'computed_field' 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

41 

42 

43_Unset: Any = PydanticUndefined 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

44 

45if sys.version_info >= (3, 13): 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

46 import warnings 1abstMcdyzNPefEFO

47 

48 Deprecated: TypeAlias = warnings.deprecated | deprecated 1abstMcdyzNPefEFO

49else: 

50 Deprecated: TypeAlias = deprecated 1GHopqrghniIJuvwxjkKLABCDlm

51 

52 

53class _FromFieldInfoInputs(typing_extensions.TypedDict, total=False): 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

54 """This class exists solely to add type checking for the `**kwargs` in `FieldInfo.from_field`.""" 

55 

56 # TODO PEP 747: use TypeForm: 

57 annotation: type[Any] | None 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

58 default_factory: Callable[[], Any] | Callable[[dict[str, Any]], Any] | None 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

59 alias: str | None 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

60 alias_priority: int | None 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

61 validation_alias: str | AliasPath | AliasChoices | None 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

62 serialization_alias: str | None 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

63 title: str | None 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

64 field_title_generator: Callable[[str, FieldInfo], str] | None 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

65 description: str | None 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

66 examples: list[Any] | None 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

67 exclude: bool | None 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

68 gt: annotated_types.SupportsGt | None 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

69 ge: annotated_types.SupportsGe | None 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

70 lt: annotated_types.SupportsLt | None 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

71 le: annotated_types.SupportsLe | None 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

72 multiple_of: float | None 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

73 strict: bool | None 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

74 min_length: int | None 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

75 max_length: int | None 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

76 pattern: str | typing.Pattern[str] | None 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

77 allow_inf_nan: bool | None 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

78 max_digits: int | None 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

79 decimal_places: int | None 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

80 union_mode: Literal['smart', 'left_to_right'] | None 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

81 discriminator: str | types.Discriminator | None 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

82 deprecated: Deprecated | str | bool | None 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

83 json_schema_extra: JsonDict | Callable[[JsonDict], None] | None 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

84 frozen: bool | None 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

85 validate_default: bool | None 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

86 repr: bool 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

87 init: bool | None 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

88 init_var: bool | None 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

89 kw_only: bool | None 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

90 coerce_numbers_to_str: bool | None 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

91 fail_fast: bool | None 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

92 

93 

94class _FieldInfoInputs(_FromFieldInfoInputs, total=False): 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

95 """This class exists solely to add type checking for the `**kwargs` in `FieldInfo.__init__`.""" 

96 

97 default: Any 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

98 

99 

100@final 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

101class FieldInfo(_repr.Representation): 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

102 """This class holds information about a field. 

103 

104 `FieldInfo` is used for any field definition regardless of whether the [`Field()`][pydantic.fields.Field] 

105 function is explicitly used. 

106 

107 !!! warning 

108 You generally shouldn't be creating `FieldInfo` directly, you'll only need to use it when accessing 

109 [`BaseModel`][pydantic.main.BaseModel] `.model_fields` internals. 

110 

111 Attributes: 

112 annotation: The type annotation of the field. 

113 default: The default value of the field. 

114 default_factory: A callable to generate the default value. The callable can either take 0 arguments 

115 (in which case it is called as is) or a single argument containing the already validated data. 

116 alias: The alias name of the field. 

117 alias_priority: The priority of the field's alias. 

118 validation_alias: The validation alias of the field. 

119 serialization_alias: The serialization alias of the field. 

120 title: The title of the field. 

121 field_title_generator: A callable that takes a field name and returns title for it. 

122 description: The description of the field. 

123 examples: List of examples of the field. 

124 exclude: Whether to exclude the field from the model serialization. 

125 discriminator: Field name or Discriminator for discriminating the type in a tagged union. 

126 deprecated: A deprecation message, an instance of `warnings.deprecated` or the `typing_extensions.deprecated` backport, 

127 or a boolean. If `True`, a default deprecation message will be emitted when accessing the field. 

128 json_schema_extra: A dict or callable to provide extra JSON schema properties. 

129 frozen: Whether the field is frozen. 

130 validate_default: Whether to validate the default value of the field. 

131 repr: Whether to include the field in representation of the model. 

132 init: Whether the field should be included in the constructor of the dataclass. 

133 init_var: Whether the field should _only_ be included in the constructor of the dataclass, and not stored. 

134 kw_only: Whether the field should be a keyword-only argument in the constructor of the dataclass. 

135 metadata: List of metadata constraints. 

136 """ 

137 

138 annotation: type[Any] | None 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

139 default: Any 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

140 default_factory: Callable[[], Any] | Callable[[dict[str, Any]], Any] | None 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

141 alias: str | None 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

142 alias_priority: int | None 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

143 validation_alias: str | AliasPath | AliasChoices | None 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

144 serialization_alias: str | None 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

145 title: str | None 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

146 field_title_generator: Callable[[str, FieldInfo], str] | None 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

147 description: str | None 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

148 examples: list[Any] | None 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

149 exclude: bool | None 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

150 discriminator: str | types.Discriminator | None 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

151 deprecated: Deprecated | str | bool | None 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

152 json_schema_extra: JsonDict | Callable[[JsonDict], None] | None 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

153 frozen: bool | None 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

154 validate_default: bool | None 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

155 repr: bool 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

156 init: bool | None 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

157 init_var: bool | None 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

158 kw_only: bool | None 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

159 metadata: list[Any] 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

160 

161 __slots__ = ( 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

162 'annotation', 

163 'default', 

164 'default_factory', 

165 'alias', 

166 'alias_priority', 

167 'validation_alias', 

168 'serialization_alias', 

169 'title', 

170 'field_title_generator', 

171 'description', 

172 'examples', 

173 'exclude', 

174 'discriminator', 

175 'deprecated', 

176 'json_schema_extra', 

177 'frozen', 

178 'validate_default', 

179 'repr', 

180 'init', 

181 'init_var', 

182 'kw_only', 

183 'metadata', 

184 '_attributes_set', 

185 '_qualifiers', 

186 '_complete', 

187 '_original_assignment', 

188 '_original_annotation', 

189 ) 

190 

191 # used to convert kwargs to metadata/constraints, 

192 # None has a special meaning - these items are collected into a `PydanticGeneralMetadata` 

193 metadata_lookup: ClassVar[dict[str, typing.Callable[[Any], Any] | None]] = { 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

194 'strict': types.Strict, 

195 'gt': annotated_types.Gt, 

196 'ge': annotated_types.Ge, 

197 'lt': annotated_types.Lt, 

198 'le': annotated_types.Le, 

199 'multiple_of': annotated_types.MultipleOf, 

200 'min_length': annotated_types.MinLen, 

201 'max_length': annotated_types.MaxLen, 

202 'pattern': None, 

203 'allow_inf_nan': None, 

204 'max_digits': None, 

205 'decimal_places': None, 

206 'union_mode': None, 

207 'coerce_numbers_to_str': None, 

208 'fail_fast': types.FailFast, 

209 } 

210 

211 def __init__(self, **kwargs: Unpack[_FieldInfoInputs]) -> None: 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

212 """This class should generally not be initialized directly; instead, use the `pydantic.fields.Field` function 

213 or one of the constructor classmethods. 

214 

215 See the signature of `pydantic.fields.Field` for more details about the expected arguments. 

216 """ 

217 self._attributes_set = {k: v for k, v in kwargs.items() if v is not _Unset and k not in self.metadata_lookup} 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

218 kwargs = {k: _DefaultValues.get(k) if v is _Unset else v for k, v in kwargs.items()} # type: ignore 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

219 self.annotation = kwargs.get('annotation') 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

220 

221 default = kwargs.pop('default', PydanticUndefined) 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

222 if default is Ellipsis: 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

223 self.default = PydanticUndefined 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

224 self._attributes_set.pop('default', None) 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

225 else: 

226 self.default = default 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

227 

228 self.default_factory = kwargs.pop('default_factory', None) 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

229 

230 if self.default is not PydanticUndefined and self.default_factory is not None: 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

231 raise TypeError('cannot specify both default and default_factory') 1GHopqrghabstniIJuvwxjkcdyzKLABCDlmefEF

232 

233 self.alias = kwargs.pop('alias', None) 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

234 self.validation_alias = kwargs.pop('validation_alias', None) 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

235 self.serialization_alias = kwargs.pop('serialization_alias', None) 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

236 alias_is_set = any(alias is not None for alias in (self.alias, self.validation_alias, self.serialization_alias)) 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

237 self.alias_priority = kwargs.pop('alias_priority', None) or 2 if alias_is_set else None 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

238 self.title = kwargs.pop('title', None) 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

239 self.field_title_generator = kwargs.pop('field_title_generator', None) 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

240 self.description = kwargs.pop('description', None) 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

241 self.examples = kwargs.pop('examples', None) 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

242 self.exclude = kwargs.pop('exclude', None) 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

243 self.discriminator = kwargs.pop('discriminator', None) 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

244 # For compatibility with FastAPI<=0.110.0, we preserve the existing value if it is not overridden 

245 self.deprecated = kwargs.pop('deprecated', getattr(self, 'deprecated', None)) 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

246 self.repr = kwargs.pop('repr', True) 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

247 self.json_schema_extra = kwargs.pop('json_schema_extra', None) 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

248 self.validate_default = kwargs.pop('validate_default', None) 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

249 self.frozen = kwargs.pop('frozen', None) 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

250 # currently only used on dataclasses 

251 self.init = kwargs.pop('init', None) 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

252 self.init_var = kwargs.pop('init_var', None) 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

253 self.kw_only = kwargs.pop('kw_only', None) 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

254 

255 self.metadata = self._collect_metadata(kwargs) # type: ignore 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

256 

257 # Private attributes: 

258 self._qualifiers: set[Qualifier] = set() 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

259 # Used to rebuild FieldInfo instances: 

260 self._complete = True 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

261 self._original_annotation: Any = PydanticUndefined 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

262 self._original_assignment: Any = PydanticUndefined 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

263 

264 @staticmethod 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

265 def from_field(default: Any = PydanticUndefined, **kwargs: Unpack[_FromFieldInfoInputs]) -> FieldInfo: 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

266 """Create a new `FieldInfo` object with the `Field` function. 

267 

268 Args: 

269 default: The default value for the field. Defaults to Undefined. 

270 **kwargs: Additional arguments dictionary. 

271 

272 Raises: 

273 TypeError: If 'annotation' is passed as a keyword argument. 

274 

275 Returns: 

276 A new FieldInfo object with the given parameters. 

277 

278 Example: 

279 This is how you can create a field with default value like this: 

280 

281 ```python 

282 import pydantic 

283 

284 class MyModel(pydantic.BaseModel): 

285 foo: int = pydantic.Field(4) 

286 ``` 

287 """ 

288 if 'annotation' in kwargs: 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

289 raise TypeError('"annotation" is not permitted as a Field keyword argument') 1GHopqrghabstniIJuvwxjkcdyzKLABCDlmefEF

290 return FieldInfo(default=default, **kwargs) 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

291 

292 @staticmethod 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

293 def from_annotation(annotation: type[Any], *, _source: AnnotationSource = AnnotationSource.ANY) -> FieldInfo: 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

294 """Creates a `FieldInfo` instance from a bare annotation. 

295 

296 This function is used internally to create a `FieldInfo` from a bare annotation like this: 

297 

298 ```python 

299 import pydantic 

300 

301 class MyModel(pydantic.BaseModel): 

302 foo: int # <-- like this 

303 ``` 

304 

305 We also account for the case where the annotation can be an instance of `Annotated` and where 

306 one of the (not first) arguments in `Annotated` is an instance of `FieldInfo`, e.g.: 

307 

308 ```python 

309 from typing import Annotated 

310 

311 import annotated_types 

312 

313 import pydantic 

314 

315 class MyModel(pydantic.BaseModel): 

316 foo: Annotated[int, annotated_types.Gt(42)] 

317 bar: Annotated[int, pydantic.Field(gt=42)] 

318 ``` 

319 

320 Args: 

321 annotation: An annotation object. 

322 

323 Returns: 

324 An instance of the field metadata. 

325 """ 

326 try: 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

327 inspected_ann = inspect_annotation( 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

328 annotation, 

329 annotation_source=_source, 

330 unpack_type_aliases='skip', 

331 ) 

332 except ForbiddenQualifier as e: 1GHopqrghabstniIJuvwxjkcdyzKLABCDlmefEF

333 raise PydanticForbiddenQualifier(e.qualifier, annotation) 1GHopqrghabstniIJuvwxjkcdyzKLABCDlmefEF

334 

335 # TODO check for classvar and error? 

336 

337 # No assigned value, this happens when using a bare `Final` qualifier (also for other 

338 # qualifiers, but they shouldn't appear here). In this case we infer the type as `Any` 

339 # because we don't have any assigned value. 

340 type_expr: Any = Any if inspected_ann.type is UNKNOWN else inspected_ann.type 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

341 final = 'final' in inspected_ann.qualifiers 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

342 metadata = inspected_ann.metadata 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

343 

344 attr_overrides = {'annotation': type_expr} 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

345 if final: 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

346 attr_overrides['frozen'] = True 1GHopqrghabstniIJuvwxjkcdyzKLABCDlmefEF

347 field_info = FieldInfo._construct(metadata, **attr_overrides) 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

348 field_info._qualifiers = inspected_ann.qualifiers 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

349 return field_info 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

350 

351 @staticmethod 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

352 def from_annotated_attribute( 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

353 annotation: type[Any], default: Any, *, _source: AnnotationSource = AnnotationSource.ANY 

354 ) -> FieldInfo: 

355 """Create `FieldInfo` from an annotation with a default value. 

356 

357 This is used in cases like the following: 

358 

359 ```python 

360 from typing import Annotated 

361 

362 import annotated_types 

363 

364 import pydantic 

365 

366 class MyModel(pydantic.BaseModel): 

367 foo: int = 4 # <-- like this 

368 bar: Annotated[int, annotated_types.Gt(4)] = 4 # <-- or this 

369 spam: Annotated[int, pydantic.Field(gt=4)] = 4 # <-- or this 

370 ``` 

371 

372 Args: 

373 annotation: The type annotation of the field. 

374 default: The default value of the field. 

375 

376 Returns: 

377 A field object with the passed values. 

378 """ 

379 if annotation is default: 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

380 raise PydanticUserError( 1GHopqrghabstniIJuvwxjkcdyzKLABCDlmefEF

381 'Error when building FieldInfo from annotated attribute. ' 

382 "Make sure you don't have any field name clashing with a type annotation.", 

383 code='unevaluable-type-annotation', 

384 ) 

385 

386 try: 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

387 inspected_ann = inspect_annotation( 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

388 annotation, 

389 annotation_source=_source, 

390 unpack_type_aliases='skip', 

391 ) 

392 except ForbiddenQualifier as e: 1GHopqrghabstniIJuvwxjkcdyzKLABCDlmefEF

393 raise PydanticForbiddenQualifier(e.qualifier, annotation) 1GHopqrghabstniIJuvwxjkcdyzKLABCDlmefEF

394 

395 # TODO check for classvar and error? 

396 

397 # TODO infer from the default, this can be done in v3 once we treat final fields with 

398 # a default as proper fields and not class variables: 

399 type_expr: Any = Any if inspected_ann.type is UNKNOWN else inspected_ann.type 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

400 final = 'final' in inspected_ann.qualifiers 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

401 metadata = inspected_ann.metadata 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

402 

403 # HACK 1: the order in which the metadata is merged is inconsistent; we need to prepend 

404 # metadata from the assignment at the beginning of the metadata. Changing this is only 

405 # possible in v3 (at least). See https://github.com/pydantic/pydantic/issues/10507 

406 prepend_metadata: list[Any] | None = None 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

407 attr_overrides = {'annotation': type_expr} 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

408 if final: 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

409 attr_overrides['frozen'] = True 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

410 

411 # HACK 2: FastAPI is subclassing `FieldInfo` and historically expected the actual 

412 # instance's type to be preserved when constructing new models with its subclasses as assignments. 

413 # This code is never reached by Pydantic itself, and in an ideal world this shouldn't be necessary. 

414 if not metadata and isinstance(default, FieldInfo) and type(default) is not FieldInfo: 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

415 field_info = default._copy() 1GHopqrghabstniIJuvwxjkcdyzKLABCDlmefEF

416 field_info._attributes_set.update(attr_overrides) 1GHopqrghabstniIJuvwxjkcdyzKLABCDlmefEF

417 for k, v in attr_overrides.items(): 1GHopqrghabstniIJuvwxjkcdyzKLABCDlmefEF

418 setattr(field_info, k, v) 1GHopqrghabstniIJuvwxjkcdyzKLABCDlmefEF

419 return field_info 1GHopqrghabstniIJuvwxjkcdyzKLABCDlmefEF

420 

421 if isinstance(default, FieldInfo): 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

422 default_copy = default._copy() # Copy unnecessary when we remove HACK 1. 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

423 prepend_metadata = default_copy.metadata 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

424 default_copy.metadata = [] 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

425 metadata = metadata + [default_copy] 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

426 elif isinstance(default, dataclasses.Field): 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

427 from_field = FieldInfo._from_dataclass_field(default) 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

428 prepend_metadata = from_field.metadata # Unnecessary when we remove HACK 1. 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

429 from_field.metadata = [] 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

430 metadata = metadata + [from_field] 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

431 if 'init_var' in inspected_ann.qualifiers: 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

432 attr_overrides['init_var'] = True 1GHopqrghabstniIJuvwxjkcdyzPKLABCDlmefEF

433 if (init := getattr(default, 'init', None)) is not None: 433 ↛ 435line 433 didn't jump to line 435 because the condition on line 433 was always true1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

434 attr_overrides['init'] = init 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

435 if (kw_only := getattr(default, 'kw_only', None)) is not None: 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

436 attr_overrides['kw_only'] = kw_only 1opqrghabstMiuvwxjkcdyzNPABCDlmefEFO

437 else: 

438 # `default` is the actual default value 

439 attr_overrides['default'] = default 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

440 

441 field_info = FieldInfo._construct( 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

442 prepend_metadata + metadata if prepend_metadata is not None else metadata, **attr_overrides 

443 ) 

444 field_info._qualifiers = inspected_ann.qualifiers 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

445 return field_info 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

446 

447 @classmethod 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

448 def _construct(cls, metadata: list[Any], **attr_overrides: Any) -> Self: 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

449 """Construct the final `FieldInfo` instance, by merging the possibly existing `FieldInfo` instances from the metadata. 

450 

451 With the following example: 

452 

453 ```python {test="skip" lint="skip"} 

454 class Model(BaseModel): 

455 f: Annotated[int, Gt(1), Field(description='desc', lt=2)] 

456 ``` 

457 

458 `metadata` refers to the metadata elements of the `Annotated` form. This metadata is iterated over from left to right: 

459 

460 - If the element is a `Field()` function (which is itself a `FieldInfo` instance), the field attributes (such as 

461 `description`) are saved to be set on the final `FieldInfo` instance. 

462 On the other hand, some kwargs (such as `lt`) are stored as `metadata` (see `FieldInfo.__init__()`, calling 

463 `FieldInfo._collect_metadata()`). In this case, the final metadata list is extended with the one from this instance. 

464 - Else, the element is considered as a single metadata object, and is appended to the final metadata list. 

465 

466 Args: 

467 metadata: The list of metadata elements to merge together. If the `FieldInfo` instance to be constructed is for 

468 a field with an assigned `Field()`, this `Field()` assignment should be added as the last element of the 

469 provided metadata. 

470 **attr_overrides: Extra attributes that should be set on the final merged `FieldInfo` instance. 

471 

472 Returns: 

473 The final merged `FieldInfo` instance. 

474 """ 

475 merged_metadata: list[Any] = [] 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

476 merged_kwargs: dict[str, Any] = {} 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

477 

478 for meta in metadata: 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

479 if isinstance(meta, FieldInfo): 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

480 merged_metadata.extend(meta.metadata) 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

481 

482 new_js_extra: JsonDict | None = None 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

483 current_js_extra = meta.json_schema_extra 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

484 if current_js_extra is not None and 'json_schema_extra' in merged_kwargs: 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

485 # We need to merge `json_schema_extra`'s: 

486 existing_js_extra = merged_kwargs['json_schema_extra'] 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

487 if isinstance(existing_js_extra, dict): 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

488 if isinstance(current_js_extra, dict): 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

489 new_js_extra = { 1GHopqrghabstniIJuvwxjkcdyzKLABCDlmefEF

490 **existing_js_extra, 

491 **current_js_extra, 

492 } 

493 elif callable(current_js_extra): 493 ↛ 508line 493 didn't jump to line 508 because the condition on line 493 was always true1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

494 warn( 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

495 'Composing `dict` and `callable` type `json_schema_extra` is not supported. ' 

496 'The `callable` type is being ignored. ' 

497 "If you'd like support for this behavior, please open an issue on pydantic.", 

498 UserWarning, 

499 ) 

500 elif callable(existing_js_extra) and isinstance(current_js_extra, dict): 500 ↛ 508line 500 didn't jump to line 508 because the condition on line 500 was always true1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

501 warn( 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

502 'Composing `dict` and `callable` type `json_schema_extra` is not supported. ' 

503 'The `callable` type is being ignored. ' 

504 "If you'd like support for this behavior, please open an issue on pydantic.", 

505 UserWarning, 

506 ) 

507 

508 merged_kwargs.update(meta._attributes_set) 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

509 if new_js_extra is not None: 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

510 merged_kwargs['json_schema_extra'] = new_js_extra 1GHopqrghabstniIJuvwxjkcdyzKLABCDlmefEF

511 elif typing_objects.is_deprecated(meta): 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

512 merged_kwargs['deprecated'] = meta 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

513 else: 

514 merged_metadata.append(meta) 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

515 

516 merged_kwargs.update(attr_overrides) 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

517 merged_field_info = cls(**merged_kwargs) 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

518 merged_field_info.metadata = merged_metadata 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

519 return merged_field_info 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

520 

521 @staticmethod 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

522 @typing_extensions.deprecated( 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

523 "The 'merge_field_infos()' method is deprecated and will be removed in a future version. " 

524 'If you relied on this method, please open an issue in the Pydantic issue tracker.', 

525 category=None, 

526 ) 

527 def merge_field_infos(*field_infos: FieldInfo, **overrides: Any) -> FieldInfo: 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

528 """Merge `FieldInfo` instances keeping only explicitly set attributes. 

529 

530 Later `FieldInfo` instances override earlier ones. 

531 

532 Returns: 

533 FieldInfo: A merged FieldInfo instance. 

534 """ 

535 if len(field_infos) == 1: 535 ↛ 550line 535 didn't jump to line 550 because the condition on line 535 was always true1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

536 # No merging necessary, but we still need to make a copy and apply the overrides 

537 field_info = field_infos[0]._copy() 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

538 field_info._attributes_set.update(overrides) 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

539 

540 default_override = overrides.pop('default', PydanticUndefined) 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

541 if default_override is Ellipsis: 541 ↛ 542line 541 didn't jump to line 542 because the condition on line 541 was never true1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

542 default_override = PydanticUndefined 

543 if default_override is not PydanticUndefined: 543 ↛ 546line 543 didn't jump to line 546 because the condition on line 543 was always true1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

544 field_info.default = default_override 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

545 

546 for k, v in overrides.items(): 546 ↛ 547line 546 didn't jump to line 547 because the loop on line 546 never started1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

547 setattr(field_info, k, v) 

548 return field_info # type: ignore 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

549 

550 merged_field_info_kwargs: dict[str, Any] = {} 

551 metadata = {} 

552 for field_info in field_infos: 

553 attributes_set = field_info._attributes_set.copy() 

554 

555 try: 

556 json_schema_extra = attributes_set.pop('json_schema_extra') 

557 existing_json_schema_extra = merged_field_info_kwargs.get('json_schema_extra') 

558 

559 if existing_json_schema_extra is None: 

560 merged_field_info_kwargs['json_schema_extra'] = json_schema_extra 

561 if isinstance(existing_json_schema_extra, dict): 

562 if isinstance(json_schema_extra, dict): 

563 merged_field_info_kwargs['json_schema_extra'] = { 

564 **existing_json_schema_extra, 

565 **json_schema_extra, 

566 } 

567 if callable(json_schema_extra): 

568 warn( 

569 'Composing `dict` and `callable` type `json_schema_extra` is not supported.' 

570 'The `callable` type is being ignored.' 

571 "If you'd like support for this behavior, please open an issue on pydantic.", 

572 PydanticJsonSchemaWarning, 

573 ) 

574 elif callable(json_schema_extra): 

575 # if ever there's a case of a callable, we'll just keep the last json schema extra spec 

576 merged_field_info_kwargs['json_schema_extra'] = json_schema_extra 

577 except KeyError: 

578 pass 

579 

580 # later FieldInfo instances override everything except json_schema_extra from earlier FieldInfo instances 

581 merged_field_info_kwargs.update(attributes_set) 

582 

583 for x in field_info.metadata: 

584 if not isinstance(x, FieldInfo): 

585 metadata[type(x)] = x 

586 

587 merged_field_info_kwargs.update(overrides) 

588 field_info = FieldInfo(**merged_field_info_kwargs) 

589 field_info.metadata = list(metadata.values()) 

590 return field_info 

591 

592 @staticmethod 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

593 def _from_dataclass_field(dc_field: DataclassField[Any]) -> FieldInfo: 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

594 """Return a new `FieldInfo` instance from a `dataclasses.Field` instance. 

595 

596 Args: 

597 dc_field: The `dataclasses.Field` instance to convert. 

598 

599 Returns: 

600 The corresponding `FieldInfo` instance. 

601 

602 Raises: 

603 TypeError: If any of the `FieldInfo` kwargs does not match the `dataclass.Field` kwargs. 

604 """ 

605 default = dc_field.default 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

606 if default is dataclasses.MISSING: 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

607 default = _Unset 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

608 

609 if dc_field.default_factory is dataclasses.MISSING: 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

610 default_factory = _Unset 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

611 else: 

612 default_factory = dc_field.default_factory 1GHopqrghabstniIJuvwxjkcdyzKLABCDlmefEF

613 

614 # use the `Field` function so in correct kwargs raise the correct `TypeError` 

615 dc_field_metadata = {k: v for k, v in dc_field.metadata.items() if k in _FIELD_ARG_NAMES} 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

616 return Field(default=default, default_factory=default_factory, repr=dc_field.repr, **dc_field_metadata) # pyright: ignore[reportCallIssue] 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

617 

618 @staticmethod 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

619 def _collect_metadata(kwargs: dict[str, Any]) -> list[Any]: 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

620 """Collect annotations from kwargs. 

621 

622 Args: 

623 kwargs: Keyword arguments passed to the function. 

624 

625 Returns: 

626 A list of metadata objects - a combination of `annotated_types.BaseMetadata` and 

627 `PydanticMetadata`. 

628 """ 

629 metadata: list[Any] = [] 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

630 general_metadata = {} 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

631 for key, value in list(kwargs.items()): 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

632 try: 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

633 marker = FieldInfo.metadata_lookup[key] 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

634 except KeyError: 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

635 continue 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

636 

637 del kwargs[key] 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

638 if value is not None: 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

639 if marker is None: 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

640 general_metadata[key] = value 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

641 else: 

642 metadata.append(marker(value)) 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

643 if general_metadata: 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

644 metadata.append(_fields.pydantic_general_metadata(**general_metadata)) 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

645 return metadata 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

646 

647 @property 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

648 def deprecation_message(self) -> str | None: 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

649 """The deprecation message to be emitted, or `None` if not set.""" 

650 if self.deprecated is None: 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

651 return None 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

652 if isinstance(self.deprecated, bool): 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

653 return 'deprecated' if self.deprecated else None 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

654 return self.deprecated if isinstance(self.deprecated, str) else self.deprecated.message 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

655 

656 @property 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

657 def default_factory_takes_validated_data(self) -> bool | None: 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

658 """Whether the provided default factory callable has a validated data parameter. 

659 

660 Returns `None` if no default factory is set. 

661 """ 

662 if self.default_factory is not None: 662 ↛ exitline 662 didn't return from function 'default_factory_takes_validated_data' because the condition on line 662 was always true1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

663 return _fields.takes_validated_data_argument(self.default_factory) 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

664 

665 @overload 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

666 def get_default( 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

667 self, *, call_default_factory: Literal[True], validated_data: dict[str, Any] | None = None 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

668 ) -> Any: ... 1ghabnijkcdPlmef

669 

670 @overload 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

671 def get_default(self, *, call_default_factory: Literal[False] = ...) -> Any: ... 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

672 

673 def get_default(self, *, call_default_factory: bool = False, validated_data: dict[str, Any] | None = None) -> Any: 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

674 """Get the default value. 

675 

676 We expose an option for whether to call the default_factory (if present), as calling it may 

677 result in side effects that we want to avoid. However, there are times when it really should 

678 be called (namely, when instantiating a model via `model_construct`). 

679 

680 Args: 

681 call_default_factory: Whether to call the default factory or not. 

682 validated_data: The already validated data to be passed to the default factory. 

683 

684 Returns: 

685 The default value, calling the default factory if requested or `None` if not set. 

686 """ 

687 if self.default_factory is None: 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

688 return _utils.smart_deepcopy(self.default) 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

689 elif call_default_factory: 689 ↛ 701line 689 didn't jump to line 701 because the condition on line 689 was always true1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

690 if self.default_factory_takes_validated_data: 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

691 fac = cast('Callable[[dict[str, Any]], Any]', self.default_factory) 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

692 if validated_data is None: 692 ↛ 693line 692 didn't jump to line 693 because the condition on line 692 was never true1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

693 raise ValueError( 

694 "The default factory requires the 'validated_data' argument, which was not provided when calling 'get_default'." 

695 ) 

696 return fac(validated_data) 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

697 else: 

698 fac = cast('Callable[[], Any]', self.default_factory) 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

699 return fac() 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

700 else: 

701 return None 

702 

703 def is_required(self) -> bool: 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

704 """Check if the field is required (i.e., does not have a default value or factory). 

705 

706 Returns: 

707 `True` if the field is required, `False` otherwise. 

708 """ 

709 return self.default is PydanticUndefined and self.default_factory is None 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

710 

711 def rebuild_annotation(self) -> Any: 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

712 """Attempts to rebuild the original annotation for use in function signatures. 

713 

714 If metadata is present, it adds it to the original annotation using 

715 `Annotated`. Otherwise, it returns the original annotation as-is. 

716 

717 Note that because the metadata has been flattened, the original annotation 

718 may not be reconstructed exactly as originally provided, e.g. if the original 

719 type had unrecognized annotations, or was annotated with a call to `pydantic.Field`. 

720 

721 Returns: 

722 The rebuilt annotation. 

723 """ 

724 if not self.metadata: 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

725 return self.annotation 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

726 else: 

727 # Annotated arguments must be a tuple 

728 return Annotated[(self.annotation, *self.metadata)] # type: ignore 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

729 

730 def apply_typevars_map( 1GHopqrghabstMIJuvwxjkcdyzNPKLABCDlmefEFO

731 self, 

732 typevars_map: Mapping[TypeVar, Any] | None, 

733 globalns: GlobalsNamespace | None = None, 

734 localns: MappingNamespace | None = None, 

735 ) -> None: 

736 """Apply a `typevars_map` to the annotation. 

737 

738 This method is used when analyzing parametrized generic types to replace typevars with their concrete types. 

739 

740 This method applies the `typevars_map` to the annotation in place. 

741 

742 Args: 

743 typevars_map: A dictionary mapping type variables to their concrete types. 

744 globalns: The globals namespace to use during type annotation evaluation. 

745 localns: The locals namespace to use during type annotation evaluation. 

746 

747 See Also: 

748 pydantic._internal._generics.replace_types is used for replacing the typevars with 

749 their concrete types. 

750 """ 

751 annotation = _generics.replace_types(self.annotation, typevars_map) 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

752 annotation, evaluated = _typing_extra.try_eval_type(annotation, globalns, localns) 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

753 self.annotation = annotation 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

754 if not evaluated: 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

755 self._complete = False 1GHopqrghabstniIJuvwxjkcdyzKLABCDlmefEF

756 self._original_annotation = self.annotation 1GHopqrghabstniIJuvwxjkcdyzKLABCDlmefEF

757 

758 def _copy(self) -> Self: 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

759 """Return a copy of the `FieldInfo` instance.""" 

760 # Note: we can't define a custom `__copy__()`, as `FieldInfo` is being subclassed 

761 # by some third-party libraries with extra attributes defined (and as `FieldInfo` 

762 # is slotted, we can't make a copy of the `__dict__`). 

763 copied = copy(self) 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

764 for attr_name in ('metadata', '_attributes_set', '_qualifiers'): 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

765 # Apply "deep-copy" behavior on collections attributes: 

766 value = getattr(copied, attr_name).copy() 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

767 setattr(copied, attr_name, value) 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

768 

769 return copied 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

770 

771 def __repr_args__(self) -> ReprArgs: 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

772 yield 'annotation', _repr.PlainRepr(_repr.display_as_type(self.annotation)) 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

773 yield 'required', self.is_required() 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

774 

775 for s in self.__slots__: 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

776 # TODO: properly make use of the protocol (https://rich.readthedocs.io/en/stable/pretty.html#rich-repr-protocol) 

777 # By yielding a three-tuple: 

778 if s in ( 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

779 'annotation', 

780 '_attributes_set', 

781 '_qualifiers', 

782 '_complete', 

783 '_original_assignment', 

784 '_original_annotation', 

785 ): 

786 continue 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

787 elif s == 'metadata' and not self.metadata: 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

788 continue 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

789 elif s == 'repr' and self.repr is True: 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

790 continue 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

791 if s == 'frozen' and self.frozen is False: 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

792 continue 1GHopqrghabstniIJuvwxjkcdyzKLABCDlmefEF

793 if s == 'validation_alias' and self.validation_alias == self.alias: 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

794 continue 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

795 if s == 'serialization_alias' and self.serialization_alias == self.alias: 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

796 continue 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

797 if s == 'default' and self.default is not PydanticUndefined: 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

798 yield 'default', self.default 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

799 elif s == 'default_factory' and self.default_factory is not None: 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

800 yield 'default_factory', _repr.PlainRepr(_repr.display_as_type(self.default_factory)) 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

801 else: 

802 value = getattr(self, s) 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

803 if value is not None and value is not PydanticUndefined: 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

804 yield s, value 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

805 

806 

807class _EmptyKwargs(typing_extensions.TypedDict): 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

808 """This class exists solely to ensure that type checking warns about passing `**extra` in `Field`.""" 

809 

810 

811_DefaultValues = { 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

812 'default': ..., 

813 'default_factory': None, 

814 'alias': None, 

815 'alias_priority': None, 

816 'validation_alias': None, 

817 'serialization_alias': None, 

818 'title': None, 

819 'description': None, 

820 'examples': None, 

821 'exclude': None, 

822 'discriminator': None, 

823 'json_schema_extra': None, 

824 'frozen': None, 

825 'validate_default': None, 

826 'repr': True, 

827 'init': None, 

828 'init_var': None, 

829 'kw_only': None, 

830 'pattern': None, 

831 'strict': None, 

832 'gt': None, 

833 'ge': None, 

834 'lt': None, 

835 'le': None, 

836 'multiple_of': None, 

837 'allow_inf_nan': None, 

838 'max_digits': None, 

839 'decimal_places': None, 

840 'min_length': None, 

841 'max_length': None, 

842 'coerce_numbers_to_str': None, 

843} 

844 

845 

846_T = TypeVar('_T') 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

847 

848 

849# NOTE: Actual return type is 'FieldInfo', but we want to help type checkers 

850# to understand the magic that happens at runtime with the following overloads: 

851@overload # type hint the return value as `Any` to avoid type checking regressions when using `...`. 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

852def Field( 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

853 default: ellipsis, # noqa: F821 # TODO: use `_typing_extra.EllipsisType` when we drop Py3.9 1ghabnijkcdPlmef

854 *, 

855 alias: str | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

856 alias_priority: int | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

857 validation_alias: str | AliasPath | AliasChoices | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

858 serialization_alias: str | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

859 title: str | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

860 field_title_generator: Callable[[str, FieldInfo], str] | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

861 description: str | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

862 examples: list[Any] | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

863 exclude: bool | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

864 discriminator: str | types.Discriminator | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

865 deprecated: Deprecated | str | bool | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

866 json_schema_extra: JsonDict | Callable[[JsonDict], None] | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

867 frozen: bool | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

868 validate_default: bool | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

869 repr: bool = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

870 init: bool | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

871 init_var: bool | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

872 kw_only: bool | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

873 pattern: str | typing.Pattern[str] | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

874 strict: bool | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

875 coerce_numbers_to_str: bool | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

876 gt: annotated_types.SupportsGt | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

877 ge: annotated_types.SupportsGe | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

878 lt: annotated_types.SupportsLt | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

879 le: annotated_types.SupportsLe | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

880 multiple_of: float | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

881 allow_inf_nan: bool | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

882 max_digits: int | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

883 decimal_places: int | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

884 min_length: int | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

885 max_length: int | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

886 union_mode: Literal['smart', 'left_to_right'] = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

887 fail_fast: bool | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

888 **extra: Unpack[_EmptyKwargs], 1ghabnijkcdPlmef

889) -> Any: ... 1ghabnijkcdPlmef

890@overload # `default` argument set, validate_default=True (no type checking on the default value) 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

891def Field( 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

892 default: Any, 1ghabnijkcdPlmef

893 *, 

894 alias: str | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

895 alias_priority: int | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

896 validation_alias: str | AliasPath | AliasChoices | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

897 serialization_alias: str | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

898 title: str | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

899 field_title_generator: Callable[[str, FieldInfo], str] | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

900 description: str | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

901 examples: list[Any] | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

902 exclude: bool | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

903 discriminator: str | types.Discriminator | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

904 deprecated: Deprecated | str | bool | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

905 json_schema_extra: JsonDict | Callable[[JsonDict], None] | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

906 frozen: bool | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

907 validate_default: Literal[True], 1ghabnijkcdPlmef

908 repr: bool = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

909 init: bool | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

910 init_var: bool | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

911 kw_only: bool | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

912 pattern: str | typing.Pattern[str] | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

913 strict: bool | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

914 coerce_numbers_to_str: bool | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

915 gt: annotated_types.SupportsGt | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

916 ge: annotated_types.SupportsGe | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

917 lt: annotated_types.SupportsLt | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

918 le: annotated_types.SupportsLe | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

919 multiple_of: float | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

920 allow_inf_nan: bool | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

921 max_digits: int | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

922 decimal_places: int | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

923 min_length: int | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

924 max_length: int | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

925 union_mode: Literal['smart', 'left_to_right'] = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

926 fail_fast: bool | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

927 **extra: Unpack[_EmptyKwargs], 1ghabnijkcdPlmef

928) -> Any: ... 1ghabnijkcdPlmef

929@overload # `default` argument set, validate_default=False or unset 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

930def Field( 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

931 default: _T, 1ghabnijkcdPlmef

932 *, 

933 alias: str | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

934 alias_priority: int | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

935 validation_alias: str | AliasPath | AliasChoices | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

936 serialization_alias: str | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

937 title: str | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

938 field_title_generator: Callable[[str, FieldInfo], str] | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

939 description: str | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

940 examples: list[Any] | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

941 exclude: bool | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

942 discriminator: str | types.Discriminator | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

943 deprecated: Deprecated | str | bool | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

944 json_schema_extra: JsonDict | Callable[[JsonDict], None] | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

945 frozen: bool | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

946 validate_default: Literal[False] = ..., 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

947 repr: bool = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

948 init: bool | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

949 init_var: bool | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

950 kw_only: bool | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

951 pattern: str | typing.Pattern[str] | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

952 strict: bool | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

953 coerce_numbers_to_str: bool | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

954 gt: annotated_types.SupportsGt | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

955 ge: annotated_types.SupportsGe | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

956 lt: annotated_types.SupportsLt | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

957 le: annotated_types.SupportsLe | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

958 multiple_of: float | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

959 allow_inf_nan: bool | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

960 max_digits: int | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

961 decimal_places: int | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

962 min_length: int | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

963 max_length: int | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

964 union_mode: Literal['smart', 'left_to_right'] = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

965 fail_fast: bool | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

966 **extra: Unpack[_EmptyKwargs], 1ghabnijkcdPlmef

967) -> _T: ... 1ghabnijkcdPlmef

968@overload # `default_factory` argument set, validate_default=True (no type checking on the default value) 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

969def Field( # pyright: ignore[reportOverlappingOverload] 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

970 *, 

971 default_factory: Callable[[], Any] | Callable[[dict[str, Any]], Any], 1ghabnijkcdPlmef

972 alias: str | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

973 alias_priority: int | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

974 validation_alias: str | AliasPath | AliasChoices | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

975 serialization_alias: str | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

976 title: str | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

977 field_title_generator: Callable[[str, FieldInfo], str] | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

978 description: str | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

979 examples: list[Any] | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

980 exclude: bool | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

981 discriminator: str | types.Discriminator | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

982 deprecated: Deprecated | str | bool | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

983 json_schema_extra: JsonDict | Callable[[JsonDict], None] | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

984 frozen: bool | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

985 validate_default: Literal[True], 1ghabnijkcdPlmef

986 repr: bool = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

987 init: bool | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

988 init_var: bool | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

989 kw_only: bool | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

990 pattern: str | typing.Pattern[str] | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

991 strict: bool | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

992 coerce_numbers_to_str: bool | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

993 gt: annotated_types.SupportsGt | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

994 ge: annotated_types.SupportsGe | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

995 lt: annotated_types.SupportsLt | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

996 le: annotated_types.SupportsLe | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

997 multiple_of: float | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

998 allow_inf_nan: bool | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

999 max_digits: int | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1000 decimal_places: int | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1001 min_length: int | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1002 max_length: int | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1003 union_mode: Literal['smart', 'left_to_right'] = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1004 fail_fast: bool | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1005 **extra: Unpack[_EmptyKwargs], 1ghabnijkcdPlmef

1006) -> Any: ... 1ghabnijkcdPlmef

1007@overload # `default_factory` argument set, validate_default=False or unset 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1008def Field( 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1009 *, 

1010 default_factory: Callable[[], _T] | Callable[[dict[str, Any]], _T], 1ghabnijkcdPlmef

1011 alias: str | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1012 alias_priority: int | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1013 validation_alias: str | AliasPath | AliasChoices | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1014 serialization_alias: str | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1015 title: str | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1016 field_title_generator: Callable[[str, FieldInfo], str] | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1017 description: str | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1018 examples: list[Any] | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1019 exclude: bool | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1020 discriminator: str | types.Discriminator | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1021 deprecated: Deprecated | str | bool | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1022 json_schema_extra: JsonDict | Callable[[JsonDict], None] | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1023 frozen: bool | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1024 validate_default: Literal[False] | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1025 repr: bool = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1026 init: bool | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1027 init_var: bool | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1028 kw_only: bool | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1029 pattern: str | typing.Pattern[str] | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1030 strict: bool | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1031 coerce_numbers_to_str: bool | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1032 gt: annotated_types.SupportsGt | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1033 ge: annotated_types.SupportsGe | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1034 lt: annotated_types.SupportsLt | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1035 le: annotated_types.SupportsLe | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1036 multiple_of: float | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1037 allow_inf_nan: bool | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1038 max_digits: int | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1039 decimal_places: int | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1040 min_length: int | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1041 max_length: int | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1042 union_mode: Literal['smart', 'left_to_right'] = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1043 fail_fast: bool | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1044 **extra: Unpack[_EmptyKwargs], 1ghabnijkcdPlmef

1045) -> _T: ... 1ghabnijkcdPlmef

1046@overload 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1047def Field( # No default set 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1048 *, 

1049 alias: str | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1050 alias_priority: int | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1051 validation_alias: str | AliasPath | AliasChoices | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1052 serialization_alias: str | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1053 title: str | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1054 field_title_generator: Callable[[str, FieldInfo], str] | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1055 description: str | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1056 examples: list[Any] | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1057 exclude: bool | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1058 discriminator: str | types.Discriminator | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1059 deprecated: Deprecated | str | bool | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1060 json_schema_extra: JsonDict | Callable[[JsonDict], None] | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1061 frozen: bool | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1062 validate_default: bool | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1063 repr: bool = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1064 init: bool | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1065 init_var: bool | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1066 kw_only: bool | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1067 pattern: str | typing.Pattern[str] | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1068 strict: bool | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1069 coerce_numbers_to_str: bool | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1070 gt: annotated_types.SupportsGt | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1071 ge: annotated_types.SupportsGe | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1072 lt: annotated_types.SupportsLt | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1073 le: annotated_types.SupportsLe | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1074 multiple_of: float | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1075 allow_inf_nan: bool | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1076 max_digits: int | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1077 decimal_places: int | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1078 min_length: int | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1079 max_length: int | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1080 union_mode: Literal['smart', 'left_to_right'] = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1081 fail_fast: bool | None = _Unset, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1082 **extra: Unpack[_EmptyKwargs], 1ghabnijkcdPlmef

1083) -> Any: ... 1ghabnijkcdPlmef

1084def Field( # noqa: C901 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1085 default: Any = PydanticUndefined, 

1086 *, 

1087 default_factory: Callable[[], Any] | Callable[[dict[str, Any]], Any] | None = _Unset, 

1088 alias: str | None = _Unset, 

1089 alias_priority: int | None = _Unset, 

1090 validation_alias: str | AliasPath | AliasChoices | None = _Unset, 

1091 serialization_alias: str | None = _Unset, 

1092 title: str | None = _Unset, 

1093 field_title_generator: Callable[[str, FieldInfo], str] | None = _Unset, 

1094 description: str | None = _Unset, 

1095 examples: list[Any] | None = _Unset, 

1096 exclude: bool | None = _Unset, 

1097 discriminator: str | types.Discriminator | None = _Unset, 

1098 deprecated: Deprecated | str | bool | None = _Unset, 

1099 json_schema_extra: JsonDict | Callable[[JsonDict], None] | None = _Unset, 

1100 frozen: bool | None = _Unset, 

1101 validate_default: bool | None = _Unset, 

1102 repr: bool = _Unset, 

1103 init: bool | None = _Unset, 

1104 init_var: bool | None = _Unset, 

1105 kw_only: bool | None = _Unset, 

1106 pattern: str | typing.Pattern[str] | None = _Unset, 

1107 strict: bool | None = _Unset, 

1108 coerce_numbers_to_str: bool | None = _Unset, 

1109 gt: annotated_types.SupportsGt | None = _Unset, 

1110 ge: annotated_types.SupportsGe | None = _Unset, 

1111 lt: annotated_types.SupportsLt | None = _Unset, 

1112 le: annotated_types.SupportsLe | None = _Unset, 

1113 multiple_of: float | None = _Unset, 

1114 allow_inf_nan: bool | None = _Unset, 

1115 max_digits: int | None = _Unset, 

1116 decimal_places: int | None = _Unset, 

1117 min_length: int | None = _Unset, 

1118 max_length: int | None = _Unset, 

1119 union_mode: Literal['smart', 'left_to_right'] = _Unset, 

1120 fail_fast: bool | None = _Unset, 

1121 **extra: Unpack[_EmptyKwargs], 

1122) -> Any: 

1123 """!!! abstract "Usage Documentation" 

1124 [Fields](../concepts/fields.md) 

1125 

1126 Create a field for objects that can be configured. 

1127 

1128 Used to provide extra information about a field, either for the model schema or complex validation. Some arguments 

1129 apply only to number fields (`int`, `float`, `Decimal`) and some apply only to `str`. 

1130 

1131 Note: 

1132 - 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` 

1133 

1134 Args: 

1135 default: Default value if the field is not set. 

1136 default_factory: A callable to generate the default value. The callable can either take 0 arguments 

1137 (in which case it is called as is) or a single argument containing the already validated data. 

1138 alias: The name to use for the attribute when validating or serializing by alias. 

1139 This is often used for things like converting between snake and camel case. 

1140 alias_priority: Priority of the alias. This affects whether an alias generator is used. 

1141 validation_alias: Like `alias`, but only affects validation, not serialization. 

1142 serialization_alias: Like `alias`, but only affects serialization, not validation. 

1143 title: Human-readable title. 

1144 field_title_generator: A callable that takes a field name and returns title for it. 

1145 description: Human-readable description. 

1146 examples: Example values for this field. 

1147 exclude: Whether to exclude the field from the model serialization. 

1148 discriminator: Field name or Discriminator for discriminating the type in a tagged union. 

1149 deprecated: A deprecation message, an instance of `warnings.deprecated` or the `typing_extensions.deprecated` backport, 

1150 or a boolean. If `True`, a default deprecation message will be emitted when accessing the field. 

1151 json_schema_extra: A dict or callable to provide extra JSON schema properties. 

1152 frozen: Whether the field is frozen. If true, attempts to change the value on an instance will raise an error. 

1153 validate_default: If `True`, apply validation to the default value every time you create an instance. 

1154 Otherwise, for performance reasons, the default value of the field is trusted and not validated. 

1155 repr: A boolean indicating whether to include the field in the `__repr__` output. 

1156 init: Whether the field should be included in the constructor of the dataclass. 

1157 (Only applies to dataclasses.) 

1158 init_var: Whether the field should _only_ be included in the constructor of the dataclass. 

1159 (Only applies to dataclasses.) 

1160 kw_only: Whether the field should be a keyword-only argument in the constructor of the dataclass. 

1161 (Only applies to dataclasses.) 

1162 coerce_numbers_to_str: Whether to enable coercion of any `Number` type to `str` (not applicable in `strict` mode). 

1163 strict: If `True`, strict validation is applied to the field. 

1164 See [Strict Mode](../concepts/strict_mode.md) for details. 

1165 gt: Greater than. If set, value must be greater than this. Only applicable to numbers. 

1166 ge: Greater than or equal. If set, value must be greater than or equal to this. Only applicable to numbers. 

1167 lt: Less than. If set, value must be less than this. Only applicable to numbers. 

1168 le: Less than or equal. If set, value must be less than or equal to this. Only applicable to numbers. 

1169 multiple_of: Value must be a multiple of this. Only applicable to numbers. 

1170 min_length: Minimum length for iterables. 

1171 max_length: Maximum length for iterables. 

1172 pattern: Pattern for strings (a regular expression). 

1173 allow_inf_nan: Allow `inf`, `-inf`, `nan`. Only applicable to float and [`Decimal`][decimal.Decimal] numbers. 

1174 max_digits: Maximum number of allow digits for strings. 

1175 decimal_places: Maximum number of decimal places allowed for numbers. 

1176 union_mode: The strategy to apply when validating a union. Can be `smart` (the default), or `left_to_right`. 

1177 See [Union Mode](../concepts/unions.md#union-modes) for details. 

1178 fail_fast: If `True`, validation will stop on the first error. If `False`, all validation errors will be collected. 

1179 This option can be applied only to iterable types (list, tuple, set, and frozenset). 

1180 extra: (Deprecated) Extra fields that will be included in the JSON schema. 

1181 

1182 !!! warning Deprecated 

1183 The `extra` kwargs is deprecated. Use `json_schema_extra` instead. 

1184 

1185 Returns: 

1186 A new [`FieldInfo`][pydantic.fields.FieldInfo]. The return annotation is `Any` so `Field` can be used on 

1187 type-annotated fields without causing a type error. 

1188 """ 

1189 # Check deprecated and removed params from V1. This logic should eventually be removed. 

1190 const = extra.pop('const', None) # type: ignore 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1191 if const is not None: 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1192 raise PydanticUserError('`const` is removed, use `Literal` instead', code='removed-kwargs') 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1193 

1194 min_items = extra.pop('min_items', None) # type: ignore 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1195 if min_items is not None: 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1196 warn('`min_items` is deprecated and will be removed, use `min_length` instead', DeprecationWarning) 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1197 if min_length in (None, _Unset): 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1198 min_length = min_items # type: ignore 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1199 

1200 max_items = extra.pop('max_items', None) # type: ignore 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1201 if max_items is not None: 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1202 warn('`max_items` is deprecated and will be removed, use `max_length` instead', DeprecationWarning) 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1203 if max_length in (None, _Unset): 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1204 max_length = max_items # type: ignore 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1205 

1206 unique_items = extra.pop('unique_items', None) # type: ignore 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1207 if unique_items is not None: 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1208 raise PydanticUserError( 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1209 ( 

1210 '`unique_items` is removed, use `Set` instead' 

1211 '(this feature is discussed in https://github.com/pydantic/pydantic-core/issues/296)' 

1212 ), 

1213 code='removed-kwargs', 

1214 ) 

1215 

1216 allow_mutation = extra.pop('allow_mutation', None) # type: ignore 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1217 if allow_mutation is not None: 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1218 warn('`allow_mutation` is deprecated and will be removed. use `frozen` instead', DeprecationWarning) 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1219 if allow_mutation is False: 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1220 frozen = True 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1221 

1222 regex = extra.pop('regex', None) # type: ignore 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1223 if regex is not None: 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1224 raise PydanticUserError('`regex` is removed. use `pattern` instead', code='removed-kwargs') 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1225 

1226 if extra: 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1227 warn( 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1228 'Using extra keyword arguments on `Field` is deprecated and will be removed.' 

1229 ' Use `json_schema_extra` instead.' 

1230 f' (Extra keys: {", ".join(k.__repr__() for k in extra.keys())})', 

1231 DeprecationWarning, 

1232 ) 

1233 if not json_schema_extra or json_schema_extra is _Unset: 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1234 json_schema_extra = extra # type: ignore 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1235 

1236 if ( 1GHopqrniIJuvwxKLABCD

1237 validation_alias 

1238 and validation_alias is not _Unset 

1239 and not isinstance(validation_alias, (str, AliasChoices, AliasPath)) 

1240 ): 

1241 raise TypeError('Invalid `validation_alias` type. it should be `str`, `AliasChoices`, or `AliasPath`') 1GHopqrghabstniIJuvwxjkcdyzKLABCDlmefEF

1242 

1243 if serialization_alias in (_Unset, None) and isinstance(alias, str): 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1244 serialization_alias = alias 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1245 

1246 if validation_alias in (_Unset, None): 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1247 validation_alias = alias 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1248 

1249 include = extra.pop('include', None) # type: ignore 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1250 if include is not None: 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1251 warn('`include` is deprecated and does nothing. It will be removed, use `exclude` instead', DeprecationWarning) 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1252 

1253 return FieldInfo.from_field( 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1254 default, 

1255 default_factory=default_factory, 

1256 alias=alias, 

1257 alias_priority=alias_priority, 

1258 validation_alias=validation_alias, 

1259 serialization_alias=serialization_alias, 

1260 title=title, 

1261 field_title_generator=field_title_generator, 

1262 description=description, 

1263 examples=examples, 

1264 exclude=exclude, 

1265 discriminator=discriminator, 

1266 deprecated=deprecated, 

1267 json_schema_extra=json_schema_extra, 

1268 frozen=frozen, 

1269 pattern=pattern, 

1270 validate_default=validate_default, 

1271 repr=repr, 

1272 init=init, 

1273 init_var=init_var, 

1274 kw_only=kw_only, 

1275 coerce_numbers_to_str=coerce_numbers_to_str, 

1276 strict=strict, 

1277 gt=gt, 

1278 ge=ge, 

1279 lt=lt, 

1280 le=le, 

1281 multiple_of=multiple_of, 

1282 min_length=min_length, 

1283 max_length=max_length, 

1284 allow_inf_nan=allow_inf_nan, 

1285 max_digits=max_digits, 

1286 decimal_places=decimal_places, 

1287 union_mode=union_mode, 

1288 fail_fast=fail_fast, 

1289 ) 

1290 

1291 

1292_FIELD_ARG_NAMES = set(inspect.signature(Field).parameters) 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1293_FIELD_ARG_NAMES.remove('extra') # do not include the varkwargs parameter 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1294 

1295 

1296class ModelPrivateAttr(_repr.Representation): 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1297 """A descriptor for private attributes in class models. 

1298 

1299 !!! warning 

1300 You generally shouldn't be creating `ModelPrivateAttr` instances directly, instead use 

1301 `pydantic.fields.PrivateAttr`. (This is similar to `FieldInfo` vs. `Field`.) 

1302 

1303 Attributes: 

1304 default: The default value of the attribute if not provided. 

1305 default_factory: A callable function that generates the default value of the 

1306 attribute if not provided. 

1307 """ 

1308 

1309 __slots__ = ('default', 'default_factory') 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1310 

1311 def __init__( 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1312 self, default: Any = PydanticUndefined, *, default_factory: typing.Callable[[], Any] | None = None 

1313 ) -> None: 

1314 if default is Ellipsis: 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1315 self.default = PydanticUndefined 1GHopqrghabstniIJuvwxjkcdyzKLABCDlmefEF

1316 else: 

1317 self.default = default 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1318 self.default_factory = default_factory 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1319 

1320 if not typing.TYPE_CHECKING: 1320 ↛ 1332line 1320 didn't jump to line 1332 because the condition on line 1320 was always true1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1321 # We put `__getattr__` in a non-TYPE_CHECKING block because otherwise, mypy allows arbitrary attribute access 

1322 

1323 def __getattr__(self, item: str) -> Any: 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1324 """This function improves compatibility with custom descriptors by ensuring delegation happens 

1325 as expected when the default value of a private attribute is a descriptor. 

1326 """ 

1327 if item in {'__get__', '__set__', '__delete__'}: 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1328 if hasattr(self.default, item): 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1329 return getattr(self.default, item) 1GHopqrghabstniIJuvwxjkcdyzKLABCDlmefEF

1330 raise AttributeError(f'{type(self).__name__!r} object has no attribute {item!r}') 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1331 

1332 def __set_name__(self, cls: type[Any], name: str) -> None: 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1333 """Preserve `__set_name__` protocol defined in https://peps.python.org/pep-0487.""" 

1334 default = self.default 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1335 if default is PydanticUndefined: 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1336 return 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1337 set_name = getattr(default, '__set_name__', None) 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1338 if callable(set_name): 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1339 set_name(cls, name) 1GHopqrghabstniIJuvwxjkcdyzKLABCDlmefEF

1340 

1341 def get_default(self) -> Any: 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1342 """Retrieve the default value of the object. 

1343 

1344 If `self.default_factory` is `None`, the method will return a deep copy of the `self.default` object. 

1345 

1346 If `self.default_factory` is not `None`, it will call `self.default_factory` and return the value returned. 

1347 

1348 Returns: 

1349 The default value of the object. 

1350 """ 

1351 return _utils.smart_deepcopy(self.default) if self.default_factory is None else self.default_factory() 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1352 

1353 def __eq__(self, other: Any) -> bool: 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1354 return isinstance(other, self.__class__) and (self.default, self.default_factory) == ( 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1355 other.default, 

1356 other.default_factory, 

1357 ) 

1358 

1359 

1360# NOTE: Actual return type is 'ModelPrivateAttr', but we want to help type checkers 

1361# to understand the magic that happens at runtime. 

1362@overload # `default` argument set 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1363def PrivateAttr( 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1364 default: _T, 1ghabnijkcdPlmef

1365 *, 

1366 init: Literal[False] = False, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1367) -> _T: ... 1ghabnijkcdPlmef

1368@overload # `default_factory` argument set 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1369def PrivateAttr( 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1370 *, 

1371 default_factory: Callable[[], _T], 1ghabnijkcdPlmef

1372 init: Literal[False] = False, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1373) -> _T: ... 1ghabnijkcdPlmef

1374@overload # No default set 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1375def PrivateAttr( 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1376 *, 

1377 init: Literal[False] = False, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1378) -> Any: ... 1ghabnijkcdPlmef

1379def PrivateAttr( 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1380 default: Any = PydanticUndefined, 

1381 *, 

1382 default_factory: Callable[[], Any] | None = None, 

1383 init: Literal[False] = False, 

1384) -> Any: 

1385 """!!! abstract "Usage Documentation" 

1386 [Private Model Attributes](../concepts/models.md#private-model-attributes) 

1387 

1388 Indicates that an attribute is intended for private use and not handled during normal validation/serialization. 

1389 

1390 Private attributes are not validated by Pydantic, so it's up to you to ensure they are used in a type-safe manner. 

1391 

1392 Private attributes are stored in `__private_attributes__` on the model. 

1393 

1394 Args: 

1395 default: The attribute's default value. Defaults to Undefined. 

1396 default_factory: Callable that will be 

1397 called when a default value is needed for this attribute. 

1398 If both `default` and `default_factory` are set, an error will be raised. 

1399 init: Whether the attribute should be included in the constructor of the dataclass. Always `False`. 

1400 

1401 Returns: 

1402 An instance of [`ModelPrivateAttr`][pydantic.fields.ModelPrivateAttr] class. 

1403 

1404 Raises: 

1405 ValueError: If both `default` and `default_factory` are set. 

1406 """ 

1407 if default is not PydanticUndefined and default_factory is not None: 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1408 raise TypeError('cannot specify both default and default_factory') 1GHopqrghabstniIJuvwxjkcdyzKLABCDlmefEF

1409 

1410 return ModelPrivateAttr( 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1411 default, 

1412 default_factory=default_factory, 

1413 ) 

1414 

1415 

1416@dataclasses.dataclass(**_internal_dataclass.slots_true) 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1417class ComputedFieldInfo: 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1418 """A container for data from `@computed_field` so that we can access it while building the pydantic-core schema. 

1419 

1420 Attributes: 

1421 decorator_repr: A class variable representing the decorator string, '@computed_field'. 

1422 wrapped_property: The wrapped computed field property. 

1423 return_type: The type of the computed field property's return value. 

1424 alias: The alias of the property to be used during serialization. 

1425 alias_priority: The priority of the alias. This affects whether an alias generator is used. 

1426 title: Title of the computed field to include in the serialization JSON schema. 

1427 field_title_generator: A callable that takes a field name and returns title for it. 

1428 description: Description of the computed field to include in the serialization JSON schema. 

1429 deprecated: A deprecation message, an instance of `warnings.deprecated` or the `typing_extensions.deprecated` backport, 

1430 or a boolean. If `True`, a default deprecation message will be emitted when accessing the field. 

1431 examples: Example values of the computed field to include in the serialization JSON schema. 

1432 json_schema_extra: A dict or callable to provide extra JSON schema properties. 

1433 repr: A boolean indicating whether to include the field in the __repr__ output. 

1434 """ 

1435 

1436 decorator_repr: ClassVar[str] = '@computed_field' 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1437 wrapped_property: property 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1438 return_type: Any 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1439 alias: str | None 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1440 alias_priority: int | None 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1441 title: str | None 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1442 field_title_generator: Callable[[str, ComputedFieldInfo], str] | None 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1443 description: str | None 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1444 deprecated: Deprecated | str | bool | None 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1445 examples: list[Any] | None 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1446 json_schema_extra: JsonDict | Callable[[JsonDict], None] | None 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1447 repr: bool 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1448 

1449 @property 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1450 def deprecation_message(self) -> str | None: 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1451 """The deprecation message to be emitted, or `None` if not set.""" 

1452 if self.deprecated is None: 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1453 return None 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1454 if isinstance(self.deprecated, bool): 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1455 return 'deprecated' if self.deprecated else None 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1456 return self.deprecated if isinstance(self.deprecated, str) else self.deprecated.message 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1457 

1458 def _update_from_config(self, config_wrapper: ConfigWrapper, name: str) -> None: 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1459 """Update the instance from the configuration set on the class this computed field belongs to.""" 

1460 title_generator = self.field_title_generator or config_wrapper.field_title_generator 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1461 if title_generator is not None and self.title is None: 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1462 self.title = title_generator(name, self) 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1463 if config_wrapper.alias_generator is not None: 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1464 self._apply_alias_generator(config_wrapper.alias_generator, name) 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1465 

1466 def _apply_alias_generator(self, alias_generator: Callable[[str], str] | AliasGenerator, name: str) -> None: 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1467 """Apply an alias generator to aliases if appropriate. 

1468 

1469 Args: 

1470 alias_generator: A callable that takes a string and returns a string, or an `AliasGenerator` instance. 

1471 name: The name of the computed field from which to generate the alias. 

1472 """ 

1473 # Apply an alias_generator if 

1474 # 1. An alias is not specified 

1475 # 2. An alias is specified, but the priority is <= 1 

1476 

1477 if self.alias_priority is None or self.alias_priority <= 1 or self.alias is None: 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1478 alias, _, serialization_alias = None, None, None 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1479 

1480 if isinstance(alias_generator, AliasGenerator): 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1481 alias, _, serialization_alias = alias_generator.generate_aliases(name) 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1482 elif callable(alias_generator): 1482 ↛ 1488line 1482 didn't jump to line 1488 because the condition on line 1482 was always true1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1483 alias = alias_generator(name) 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1484 

1485 # if priority is not set, we set to 1 

1486 # which supports the case where the alias_generator from a child class is used 

1487 # to generate an alias for a field in a parent class 

1488 if self.alias_priority is None or self.alias_priority <= 1: 1488 ↛ 1494line 1488 didn't jump to line 1494 because the condition on line 1488 was always true1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1489 self.alias_priority = 1 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1490 

1491 # if the priority is 1, then we set the aliases to the generated alias 

1492 # note that we use the serialization_alias with priority over alias, as computed_field 

1493 # aliases are used for serialization only (not validation) 

1494 if self.alias_priority == 1: 1494 ↛ exitline 1494 didn't return from function '_apply_alias_generator' because the condition on line 1494 was always true1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1495 self.alias = _utils.get_first_not_none(serialization_alias, alias) 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1496 

1497 

1498def _wrapped_property_is_private(property_: cached_property | property) -> bool: # type: ignore 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1499 """Returns true if provided property is private, False otherwise.""" 

1500 wrapped_name: str = '' 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1501 

1502 if isinstance(property_, property): 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1503 wrapped_name = getattr(property_.fget, '__name__', '') 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1504 elif isinstance(property_, cached_property): # type: ignore 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1505 wrapped_name = getattr(property_.func, '__name__', '') # type: ignore 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1506 

1507 return wrapped_name.startswith('_') and not wrapped_name.startswith('__') 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1508 

1509 

1510# this should really be `property[T], cached_property[T]` but property is not generic unlike cached_property 

1511# See https://github.com/python/typing/issues/985 and linked issues 

1512PropertyT = typing.TypeVar('PropertyT') 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1513 

1514 

1515@typing.overload 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1516def computed_field(func: PropertyT, /) -> PropertyT: ... 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1517 

1518 

1519@typing.overload 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1520def computed_field( 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1521 *, 

1522 alias: str | None = None, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1523 alias_priority: int | None = None, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1524 title: str | None = None, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1525 field_title_generator: typing.Callable[[str, ComputedFieldInfo], str] | None = None, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1526 description: str | None = None, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1527 deprecated: Deprecated | str | bool | None = None, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1528 examples: list[Any] | None = None, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1529 json_schema_extra: JsonDict | typing.Callable[[JsonDict], None] | None = None, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1530 repr: bool = True, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1531 return_type: Any = PydanticUndefined, 1GHopqrghabstMniIJuvwxjkcdyzNPKLABCDlmefEFO

1532) -> typing.Callable[[PropertyT], PropertyT]: ... 1ghabnijkcdPlmef

1533 

1534 

1535def computed_field( 1GHopqrghabstMIJuvwxjkcdyzNPKLABCDlmefEFO

1536 func: PropertyT | None = None, 

1537 /, 

1538 *, 

1539 alias: str | None = None, 

1540 alias_priority: int | None = None, 

1541 title: str | None = None, 

1542 field_title_generator: typing.Callable[[str, ComputedFieldInfo], str] | None = None, 

1543 description: str | None = None, 

1544 deprecated: Deprecated | str | bool | None = None, 

1545 examples: list[Any] | None = None, 

1546 json_schema_extra: JsonDict | typing.Callable[[JsonDict], None] | None = None, 

1547 repr: bool | None = None, 

1548 return_type: Any = PydanticUndefined, 

1549) -> PropertyT | typing.Callable[[PropertyT], PropertyT]: 

1550 """!!! abstract "Usage Documentation" 

1551 [The `computed_field` decorator](../concepts/fields.md#the-computed_field-decorator) 

1552 

1553 Decorator to include `property` and `cached_property` when serializing models or dataclasses. 

1554 

1555 This is useful for fields that are computed from other fields, or for fields that are expensive to compute and should be cached. 

1556 

1557 ```python 

1558 from pydantic import BaseModel, computed_field 

1559 

1560 class Rectangle(BaseModel): 

1561 width: int 

1562 length: int 

1563 

1564 @computed_field 

1565 @property 

1566 def area(self) -> int: 

1567 return self.width * self.length 

1568 

1569 print(Rectangle(width=3, length=2).model_dump()) 

1570 #> {'width': 3, 'length': 2, 'area': 6} 

1571 ``` 

1572 

1573 If applied to functions not yet decorated with `@property` or `@cached_property`, the function is 

1574 automatically wrapped with `property`. Although this is more concise, you will lose IntelliSense in your IDE, 

1575 and confuse static type checkers, thus explicit use of `@property` is recommended. 

1576 

1577 !!! warning "Mypy Warning" 

1578 Even with the `@property` or `@cached_property` applied to your function before `@computed_field`, 

1579 mypy may throw a `Decorated property not supported` error. 

1580 See [mypy issue #1362](https://github.com/python/mypy/issues/1362), for more information. 

1581 To avoid this error message, add `# type: ignore[prop-decorator]` to the `@computed_field` line. 

1582 

1583 [pyright](https://github.com/microsoft/pyright) supports `@computed_field` without error. 

1584 

1585 ```python 

1586 import random 

1587 

1588 from pydantic import BaseModel, computed_field 

1589 

1590 class Square(BaseModel): 

1591 width: float 

1592 

1593 @computed_field 

1594 def area(self) -> float: # converted to a `property` by `computed_field` 

1595 return round(self.width**2, 2) 

1596 

1597 @area.setter 

1598 def area(self, new_area: float) -> None: 

1599 self.width = new_area**0.5 

1600 

1601 @computed_field(alias='the magic number', repr=False) 

1602 def random_number(self) -> int: 

1603 return random.randint(0, 1_000) 

1604 

1605 square = Square(width=1.3) 

1606 

1607 # `random_number` does not appear in representation 

1608 print(repr(square)) 

1609 #> Square(width=1.3, area=1.69) 

1610 

1611 print(square.random_number) 

1612 #> 3 

1613 

1614 square.area = 4 

1615 

1616 print(square.model_dump_json(by_alias=True)) 

1617 #> {"width":2.0,"area":4.0,"the magic number":3} 

1618 ``` 

1619 

1620 !!! warning "Overriding with `computed_field`" 

1621 You can't override a field from a parent class with a `computed_field` in the child class. 

1622 `mypy` complains about this behavior if allowed, and `dataclasses` doesn't allow this pattern either. 

1623 See the example below: 

1624 

1625 ```python 

1626 from pydantic import BaseModel, computed_field 

1627 

1628 class Parent(BaseModel): 

1629 a: str 

1630 

1631 try: 

1632 

1633 class Child(Parent): 

1634 @computed_field 

1635 @property 

1636 def a(self) -> str: 

1637 return 'new a' 

1638 

1639 except TypeError as e: 

1640 print(e) 

1641 ''' 

1642 Field 'a' of class 'Child' overrides symbol of same name in a parent class. This override with a computed_field is incompatible. 

1643 ''' 

1644 ``` 

1645 

1646 Private properties decorated with `@computed_field` have `repr=False` by default. 

1647 

1648 ```python 

1649 from functools import cached_property 

1650 

1651 from pydantic import BaseModel, computed_field 

1652 

1653 class Model(BaseModel): 

1654 foo: int 

1655 

1656 @computed_field 

1657 @cached_property 

1658 def _private_cached_property(self) -> int: 

1659 return -self.foo 

1660 

1661 @computed_field 

1662 @property 

1663 def _private_property(self) -> int: 

1664 return -self.foo 

1665 

1666 m = Model(foo=1) 

1667 print(repr(m)) 

1668 #> Model(foo=1) 

1669 ``` 

1670 

1671 Args: 

1672 func: the function to wrap. 

1673 alias: alias to use when serializing this computed field, only used when `by_alias=True` 

1674 alias_priority: priority of the alias. This affects whether an alias generator is used 

1675 title: Title to use when including this computed field in JSON Schema 

1676 field_title_generator: A callable that takes a field name and returns title for it. 

1677 description: Description to use when including this computed field in JSON Schema, defaults to the function's 

1678 docstring 

1679 deprecated: A deprecation message (or an instance of `warnings.deprecated` or the `typing_extensions.deprecated` backport). 

1680 to be emitted when accessing the field. Or a boolean. This will automatically be set if the property is decorated with the 

1681 `deprecated` decorator. 

1682 examples: Example values to use when including this computed field in JSON Schema 

1683 json_schema_extra: A dict or callable to provide extra JSON schema properties. 

1684 repr: whether to include this computed field in model repr. 

1685 Default is `False` for private properties and `True` for public properties. 

1686 return_type: optional return for serialization logic to expect when serializing to JSON, if included 

1687 this must be correct, otherwise a `TypeError` is raised. 

1688 If you don't include a return type Any is used, which does runtime introspection to handle arbitrary 

1689 objects. 

1690 

1691 Returns: 

1692 A proxy wrapper for the property. 

1693 """ 

1694 

1695 def dec(f: Any) -> Any: 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1696 nonlocal description, deprecated, return_type, alias_priority 

1697 unwrapped = _decorators.unwrap_wrapped_function(f) 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1698 

1699 if description is None and unwrapped.__doc__: 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1700 description = inspect.cleandoc(unwrapped.__doc__) 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1701 

1702 if deprecated is None and hasattr(unwrapped, '__deprecated__'): 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1703 deprecated = unwrapped.__deprecated__ 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1704 

1705 # if the function isn't already decorated with `@property` (or another descriptor), then we wrap it now 

1706 f = _decorators.ensure_property(f) 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1707 alias_priority = (alias_priority or 2) if alias is not None else None 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1708 

1709 if repr is None: 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1710 repr_: bool = not _wrapped_property_is_private(property_=f) 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1711 else: 

1712 repr_ = repr 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1713 

1714 dec_info = ComputedFieldInfo( 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1715 f, 

1716 return_type, 

1717 alias, 

1718 alias_priority, 

1719 title, 

1720 field_title_generator, 

1721 description, 

1722 deprecated, 

1723 examples, 

1724 json_schema_extra, 

1725 repr_, 

1726 ) 

1727 return _decorators.PydanticDescriptorProxy(f, dec_info) 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1728 

1729 if func is None: 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1730 return dec 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO

1731 else: 

1732 return dec(func) 1GHopqrghabstMniIJuvwxjkcdyzNKLABCDlmefEFO