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.
Attributes
| Attribute | Type | Description |
|---|
| annotation | `type[Any] | None` |
| default | Any | The default value of the field. |
| default_factory | `Callable[[], Any] | Callable[[dict[str, Any]], Any] |
| alias | `str | None` |
| alias_priority | `int | None` |
| validation_alias | `str | AliasPath |
| serialization_alias | `str | None` |
| title | `str | None` |
| field_title_generator | `Callable[[str, FieldInfo], str] | None` |
| description | `str | None` |
| examples | `list[Any] | None` |
| exclude | `bool | None` |
| exclude_if | `Callable[[Any], bool] | None` |
| discriminator | `str | types.Discriminator |
| deprecated | `Deprecated | str |
| json_schema_extra | `JsonDict | Callable[[JsonDict], None] |
| frozen | `bool | None` |
| validate_default | `bool | None` |
| repr | bool | Whether to include the field in representation of the model. |
| init | `bool | None` |
| init_var | `bool | None` |
| kw_only | `bool | None` |
| metadata | list[Any] | The metadata list containing type-specific constraints and arbitrary objects used within Annotated metadata. |
| metadata_lookup | `ClassVar[dict[str, Callable[[Any], Any] | None]]` |
Constructor
Signature
def FieldInfo(
**kwargs: Unpack[_FieldInfoInputs]
) - > null
Parameters
| Name | Type | Description |
|---|
| **kwargs | Unpack[_FieldInfoInputs] | Keyword arguments containing field configuration such as annotation, default, alias, title, description, and metadata constraints. |
Methods
from_field()
@classmethod
def from_field(
default: Any = PydanticUndefined,
**kwargs: Unpack[_FromFieldInfoInputs]
) - > [FieldInfo](../v1/fields/fieldinfo.md?sid=pydantic_v1_fields_fieldinfo)
Create a new FieldInfo object with the Field function.
Parameters
| Name | Type | Description |
|---|
| default | Any = PydanticUndefined | The default value for the field. Defaults to Undefined. |
| **kwargs | Unpack[_FromFieldInfoInputs] | Additional arguments dictionary. |
Returns
| Type | Description |
|---|
[FieldInfo](../v1/fields/fieldinfo.md?sid=pydantic_v1_fields_fieldinfo) | A new FieldInfo object with the given parameters. |
from_annotation()
@classmethod
def from_annotation(
annotation: type[Any],
_source: AnnotationSource
) - > [FieldInfo](../v1/fields/fieldinfo.md?sid=pydantic_v1_fields_fieldinfo)
Creates a FieldInfo instance from a bare annotation.
Parameters
| Name | Type | Description |
|---|
| annotation | type[Any] | An annotation object. |
| _source | AnnotationSource | The source of the annotation used for internal tracking. |
Returns
| Type | Description |
|---|
[FieldInfo](../v1/fields/fieldinfo.md?sid=pydantic_v1_fields_fieldinfo) | An instance of the field metadata. |
from_annotated_attribute()
@classmethod
def from_annotated_attribute(
annotation: type[Any],
default: Any,
_source: AnnotationSource
) - > [FieldInfo](../v1/fields/fieldinfo.md?sid=pydantic_v1_fields_fieldinfo)
Create FieldInfo from an annotation with a default value.
Parameters
| Name | Type | Description |
|---|
| annotation | type[Any] | The type annotation of the field. |
| default | Any | The default value of the field. |
| _source | AnnotationSource | The source of the annotation used for internal tracking. |
Returns
| Type | Description |
|---|
[FieldInfo](../v1/fields/fieldinfo.md?sid=pydantic_v1_fields_fieldinfo) | A field object with the passed values. |
merge_field_infos()
@classmethod
def merge_field_infos(
*field_infos: [FieldInfo](../v1/fields/fieldinfo.md?sid=pydantic_v1_fields_fieldinfo),
**overrides: Any
) - > [FieldInfo](../v1/fields/fieldinfo.md?sid=pydantic_v1_fields_fieldinfo)
Merge FieldInfo instances keeping only explicitly set attributes.
Parameters
| Name | Type | Description |
|---|
| *field_infos | [FieldInfo](../v1/fields/fieldinfo.md?sid=pydantic_v1_fields_fieldinfo) | The FieldInfo instances to be merged. |
| **overrides | Any | Attribute overrides to apply to the final merged instance. |
Returns
| Type | Description |
|---|
[FieldInfo](../v1/fields/fieldinfo.md?sid=pydantic_v1_fields_fieldinfo) | A merged FieldInfo instance. |
deprecation_message()
@classmethod
def deprecation_message() - > str | None
The deprecation message to be emitted, or None if not set.
Returns
default_factory_takes_validated_data()
@classmethod
def default_factory_takes_validated_data() - > bool | None
Whether the provided default factory callable has a validated data parameter.
Returns
| Type | Description |
|---|
| `bool | None` |
get_default()
@classmethod
def get_default(
call_default_factory: bool = False,
validated_data: dict[str, Any]| None = None
) - > Any
Get the default value.
Parameters
| Name | Type | Description |
|---|
| call_default_factory | bool = False | Whether to call the default factory or not. |
| validated_data | `dict[str, Any] | None` = None |
Returns
| Type | Description |
|---|
Any | The default value, calling the default factory if requested or PydanticUndefined if not set. |
is_required()
@classmethod
def is_required() - > bool
Check if the field is required (i.e., does not have a default value or factory).
Returns
| Type | Description |
|---|
bool | True if the field is required, False otherwise. |
rebuild_annotation()
@classmethod
def rebuild_annotation() - > Any
Attempts to rebuild the original annotation for use in function signatures.
Returns
| Type | Description |
|---|
Any | The rebuilt annotation, potentially wrapped in Annotated if metadata exists. |
apply_typevars_map()
@classmethod
def apply_typevars_map(
typevars_map: Mapping[TypeVar, Any]| None,
globalns: GlobalsNamespace | None,
localns: MappingNamespace | None
) - > None
Apply a typevars_map to the annotation.
Parameters
| Name | Type | Description |
|---|
| typevars_map | `Mapping[TypeVar, Any] | None` |
| globalns | `GlobalsNamespace | None` |
| localns | `MappingNamespace | None` |
Returns
asdict()
@classmethod
def asdict() - > _FieldInfoAsDict
Return a dictionary representation of the FieldInfo instance.
Returns
| Type | Description |
|---|
_FieldInfoAsDict | A dictionary containing annotation, metadata, and attribute mapping. |