MCPcopy
hub / github.com/pydantic/pydantic / __init__

Method __init__

pydantic/fields.py:228–288  ·  view source on GitHub ↗

This class should generally not be initialized directly; instead, use the `pydantic.fields.Field` function or one of the constructor classmethods. See the signature of `pydantic.fields.Field` for more details about the expected arguments.

(self, **kwargs: Unpack[_FieldInfoInputs])

Source from the content-addressed store, hash-verified

226 }
227
228 def __init__(self, **kwargs: Unpack[_FieldInfoInputs]) -> None:
229 """This class should generally not be initialized directly; instead, use the `pydantic.fields.Field` function
230 or one of the constructor classmethods.
231
232 See the signature of `pydantic.fields.Field` for more details about the expected arguments.
233 """
234 # Tracking the explicitly set attributes is necessary to correctly merge `Field()` functions
235 # (e.g. with `Annotated[int, Field(alias='a'), Field(alias=None)]`, even though `None` is the default value,
236 # we need to track that `alias=None` was explicitly set):
237 self._attributes_set = {k: v for k, v in kwargs.items() if v is not _Unset and k not in self.metadata_lookup}
238 kwargs = {k: _DefaultValues.get(k) if v is _Unset else v for k, v in kwargs.items()} # type: ignore
239 self.annotation = kwargs.get('annotation')
240
241 # Note: in theory, the second `pop()` arguments are not required below, as defaults are already set from `_DefaultsValues`.
242 default = kwargs.pop('default', PydanticUndefined)
243 if default is Ellipsis:
244 self.default = PydanticUndefined
245 self._attributes_set.pop('default', None)
246 else:
247 self.default = default
248
249 self.default_factory = kwargs.pop('default_factory', None)
250
251 if self.default is not PydanticUndefined and self.default_factory is not None:
252 raise TypeError('cannot specify both default and default_factory')
253
254 self.alias = kwargs.pop('alias', None)
255 self.validation_alias = kwargs.pop('validation_alias', None)
256 self.serialization_alias = kwargs.pop('serialization_alias', None)
257 alias_is_set = any(alias is not None for alias in (self.alias, self.validation_alias, self.serialization_alias))
258 self.alias_priority = kwargs.pop('alias_priority', None) or 2 if alias_is_set else None
259 self.title = kwargs.pop('title', None)
260 self.field_title_generator = kwargs.pop('field_title_generator', None)
261 self.description = kwargs.pop('description', None)
262 self.examples = kwargs.pop('examples', None)
263 self.exclude = kwargs.pop('exclude', None)
264 self.exclude_if = kwargs.pop('exclude_if', None)
265 self.discriminator = kwargs.pop('discriminator', None)
266 # For compatibility with FastAPI<=0.110.0, we preserve the existing value if it is not overridden
267 self.deprecated = kwargs.pop('deprecated', getattr(self, 'deprecated', None))
268 self.repr = kwargs.pop('repr', True)
269 self.json_schema_extra = kwargs.pop('json_schema_extra', None)
270 self.validate_default = kwargs.pop('validate_default', None)
271 self.frozen = kwargs.pop('frozen', None)
272 # currently only used on dataclasses
273 self.init = kwargs.pop('init', None)
274 self.init_var = kwargs.pop('init_var', None)
275 self.kw_only = kwargs.pop('kw_only', None)
276
277 self.metadata = self._collect_metadata(kwargs) # type: ignore
278
279 # Private attributes:
280 self._qualifiers: set[Qualifier] = set()
281 # Used to rebuild FieldInfo instances:
282 self._complete = True
283 self._original_annotation: Any = PydanticUndefined
284 self._original_assignment: Any = PydanticUndefined
285 # Used to track whether the `FieldInfo` instance represents the data about a field (and is exposed in `model_fields`/`__pydantic_fields__`),

Callers

nothing calls this directly

Calls 3

_collect_metadataMethod · 0.95
itemsMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected