MCPcopy
hub / github.com/pydantic/pydantic / FieldInfo

Class FieldInfo

pydantic/fields.py:106–869  ·  view source on GitHub ↗

This class holds information about a field. `FieldInfo` is used for any field definition regardless of whether the [`Field()`][pydantic.fields.Field] function is explicitly used. !!! warning The `FieldInfo` class is meant to expose information about a field in a Pydantic model

Source from the content-addressed store, hash-verified

104
105@final
106class FieldInfo(_repr.Representation):
107 """This class holds information about a field.
108
109 `FieldInfo` is used for any field definition regardless of whether the [`Field()`][pydantic.fields.Field]
110 function is explicitly used.
111
112 !!! warning
113 The `FieldInfo` class is meant to expose information about a field in a Pydantic model or dataclass.
114 `FieldInfo` instances shouldn't be instantiated directly, nor mutated.
115
116 If you need to derive a new model from another one and are willing to alter `FieldInfo` instances,
117 refer to this [dynamic model example](../examples/dynamic_models.md).
118
119 Attributes:
120 annotation: The type annotation of the field.
121 default: The default value of the field.
122 default_factory: A callable to generate the default value. The callable can either take 0 arguments
123 (in which case it is called as is) or a single argument containing the already validated data.
124 alias: The alias name of the field.
125 alias_priority: The priority of the field's alias.
126 validation_alias: The validation alias of the field.
127 serialization_alias: The serialization alias of the field.
128 title: The title of the field.
129 field_title_generator: A callable that takes a field name and returns title for it.
130 description: The description of the field.
131 examples: List of examples of the field.
132 exclude: Whether to exclude the field from the model serialization.
133 exclude_if: A callable that determines whether to exclude a field during serialization based on its value.
134 discriminator: Field name or Discriminator for discriminating the type in a tagged union.
135 deprecated: A deprecation message, an instance of `warnings.deprecated` or the `typing_extensions.deprecated` backport,
136 or a boolean. If `True`, a default deprecation message will be emitted when accessing the field.
137 json_schema_extra: A dict or callable to provide extra JSON schema properties.
138 frozen: Whether the field is frozen.
139 validate_default: Whether to validate the default value of the field.
140 repr: Whether to include the field in representation of the model.
141 init: Whether the field should be included in the constructor of the dataclass.
142 init_var: Whether the field should _only_ be included in the constructor of the dataclass, and not stored.
143 kw_only: Whether the field should be a keyword-only argument in the constructor of the dataclass.
144 metadata: The metadata list. Contains all the data that isn't expressed as direct `FieldInfo` attributes, including:
145
146 * Type-specific constraints, such as `gt` or `min_length` (these are converted to metadata classes such as `annotated_types.Gt`).
147 * Any other arbitrary object used within [`Annotated`][typing.Annotated] metadata
148 (e.g. [custom types handlers](../concepts/types.md#as-an-annotation) or any object not recognized by Pydantic).
149 """
150
151 # TODO PEP 747: use TypeForm:
152 annotation: type[Any] | None
153 default: Any
154 default_factory: Callable[[], Any] | Callable[[dict[str, Any]], Any] | None
155 alias: str | None
156 alias_priority: int | None
157 validation_alias: str | AliasPath | AliasChoices | None
158 serialization_alias: str | None
159 title: str | None
160 field_title_generator: Callable[[str, FieldInfo], str] | None
161 description: str | None
162 examples: list[Any] | None
163 exclude: bool | None

Calls

no outgoing calls