Skip to main content

PydanticModelField

This class represents a field within a Pydantic model and provides utilities for managing its metadata and type information during static analysis. It supports operations such as converting fields into method arguments, expanding complex types, and serializing field data for persistence. The class is designed to integrate with type-checking plugins to handle features like aliases, default values, and generic type inheritance.

Attributes

AttributeTypeDescription
namestrThe original name of the field as defined in the Pydantic model class.
alias`strNone`
is_frozenboolIndicates whether the field is immutable and cannot be modified after the model is initialized.
has_dynamic_aliasboolIndicates whether the field's alias is determined dynamically at runtime rather than being a static string.
has_defaultboolIndicates whether the field has a default value or a default factory assigned to it.
strict`boolNone`
lineintThe line number in the source code where the field is defined for error reporting and mapping.
columnintThe column number in the source code where the field definition begins.
type`TypeNone`
infoTypeInfoThe TypeInfo object representing the Pydantic model class that contains this field.

Constructor

Signature

def PydanticModelField(
name: str,
alias: str | None,
is_frozen: bool,
has_dynamic_alias: bool,
has_default: bool,
strict: bool | None,
line: int,
column: int,
type: Type | None,
info: TypeInfo
) - > null

Parameters

NameTypeDescription
namestrThe name of the field.
alias`strNone`
is_frozenboolIndicates if the field is immutable.
has_dynamic_aliasboolIndicates if the field has a dynamically generated alias.
has_defaultboolIndicates if the field has a default value.
strict`boolNone`
lineintThe line number where the field is defined in the source code.
columnintThe column number where the field is defined in the source code.
type`TypeNone`
infoTypeInfoThe TypeInfo object associated with the model.

Signature

def PydanticModelField(
name: string,
alias: string | null,
is_frozen: boolean,
has_dynamic_alias: boolean,
has_default: boolean,
strict: boolean | null,
line: integer,
column: integer,
type: Type | null,
info: TypeInfo
)

Parameters

NameTypeDescription
namestringThe Python attribute name of the field.
alias`stringnull`
is_frozenbooleanIndicates whether the field is immutable after model instantiation.
has_dynamic_aliasbooleanIndicates if the field's alias is determined at runtime rather than statically defined.
has_defaultbooleanIndicates if the field has a default value or a default factory.
strict`booleannull`
lineintegerThe source code line number where the field is defined.
columnintegerThe source code column offset where the field is defined.
type`Typenull`
infoTypeInfoThe mypy TypeInfo of the class that owns this field.

Methods


to_argument()

@classmethod
def to_argument(
current_info: TypeInfo,
typed: boolean,
model_strict: boolean,
force_optional: boolean,
use_alias: boolean,
api: SemanticAnalyzerPluginInterface,
force_typevars_invariant: boolean,
is_root_model_root: boolean
) - > Argument

Based on mypy.plugins.dataclasses.DataclassAttribute.to_argument.

Parameters

NameTypeDescription
current_infoTypeInfoThe TypeInfo of the model currently being analyzed.
typedbooleanWhether to include explicit type annotations in the generated argument.
model_strictbooleanThe default strictness setting of the parent model to use if the field doesn't specify one.
force_optionalbooleanIf true, forces the argument to be treated as optional regardless of default values.
use_aliasbooleanWhether to use the field's alias as the argument name instead of the attribute name.
apiSemanticAnalyzerPluginInterfaceThe mypy semantic analyzer interface for type lookups.
force_typevars_invariantbooleanWhether to force type variables to be invariant to avoid variance errors in method signatures.
is_root_model_rootbooleanIndicates if this field represents the 'root' field of a Pydantic RootModel.

Returns

TypeDescription
ArgumentA mypy Argument object suitable for inclusion in a generated method signature like init.

expand_type()

@classmethod
def expand_type(
current_info: TypeInfo,
api: SemanticAnalyzerPluginInterface,
force_typevars_invariant: boolean,
include_root_type: boolean
) - > Type | null

Based on mypy.plugins.dataclasses.DataclassAttribute.expand_type.

Parameters

NameTypeDescription
current_infoTypeInfoThe TypeInfo used as the context for expanding generic type variables.
apiSemanticAnalyzerPluginInterfaceThe mypy semantic analyzer interface used to access options like strict_optional.
force_typevars_invariantbooleanIf true, modifies type variables to be invariant during expansion.
include_root_typebooleanIf true, allows the type to be a Union of the model and its root type for Pydantic RootModels.

Returns

TypeDescription
`Typenull`

to_var()

@classmethod
def to_var(
current_info: TypeInfo,
api: SemanticAnalyzerPluginInterface,
use_alias: boolean,
force_typevars_invariant: boolean
) - > Var

Based on mypy.plugins.dataclasses.DataclassAttribute.to_var.

Parameters

NameTypeDescription
current_infoTypeInfoThe TypeInfo used for type expansion context.
apiSemanticAnalyzerPluginInterfaceThe mypy semantic analyzer interface.
use_aliasbooleanWhether to name the variable using the field's alias.
force_typevars_invariantbooleanWhether to force invariance on type variables during type expansion.

Returns

TypeDescription
VarA mypy Var object representing the field as a class variable.

serialize()

@classmethod
def serialize() - > JsonDict

Based on mypy.plugins.dataclasses.DataclassAttribute.serialize.

Returns

TypeDescription
JsonDictA dictionary representation of the field metadata for mypy caching.

deserialize()

@classmethod
def deserialize(
info: TypeInfo,
data: JsonDict,
api: SemanticAnalyzerPluginInterface
) - > [PydanticModelField](../v1/mypy/pydanticmodelfield.md?sid=pydantic_v1_mypy_pydanticmodelfield)

Based on mypy.plugins.dataclasses.DataclassAttribute.deserialize.

Parameters

NameTypeDescription
infoTypeInfoThe TypeInfo of the class owning this field.
dataJsonDictThe serialized field data.
apiSemanticAnalyzerPluginInterfaceThe mypy semantic analyzer interface used to fix up types during deserialization.

Returns

TypeDescription
[PydanticModelField](../v1/mypy/pydanticmodelfield.md?sid=pydantic_v1_mypy_pydanticmodelfield)A new instance of PydanticModelField reconstructed from the serialized data.

expand_typevar_from_subtype()

@classmethod
def expand_typevar_from_subtype(
sub_type: TypeInfo,
api: SemanticAnalyzerPluginInterface
) - > null

Expands type vars in the context of a subtype when an attribute is inherited from a generic super type.

Parameters

NameTypeDescription
sub_typeTypeInfoThe TypeInfo of the inheriting subtype used to map generic parameters.
apiSemanticAnalyzerPluginInterfaceThe mypy semantic analyzer interface.

Returns

TypeDescription
nullNone