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
| Attribute | Type | Description |
|---|---|---|
| name | str | The original name of the field as defined in the Pydantic model class. |
| alias | `str | None` |
| is_frozen | bool | Indicates whether the field is immutable and cannot be modified after the model is initialized. |
| has_dynamic_alias | bool | Indicates whether the field's alias is determined dynamically at runtime rather than being a static string. |
| has_default | bool | Indicates whether the field has a default value or a default factory assigned to it. |
| strict | `bool | None` |
| line | int | The line number in the source code where the field is defined for error reporting and mapping. |
| column | int | The column number in the source code where the field definition begins. |
| type | `Type | None` |
| info | TypeInfo | The 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
| Name | Type | Description |
|---|---|---|
| name | str | The name of the field. |
| alias | `str | None` |
| is_frozen | bool | Indicates if the field is immutable. |
| has_dynamic_alias | bool | Indicates if the field has a dynamically generated alias. |
| has_default | bool | Indicates if the field has a default value. |
| strict | `bool | None` |
| line | int | The line number where the field is defined in the source code. |
| column | int | The column number where the field is defined in the source code. |
| type | `Type | None` |
| info | TypeInfo | The 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
| Name | Type | Description |
|---|---|---|
| name | string | The Python attribute name of the field. |
| alias | `string | null` |
| is_frozen | boolean | Indicates whether the field is immutable after model instantiation. |
| has_dynamic_alias | boolean | Indicates if the field's alias is determined at runtime rather than statically defined. |
| has_default | boolean | Indicates if the field has a default value or a default factory. |
| strict | `boolean | null` |
| line | integer | The source code line number where the field is defined. |
| column | integer | The source code column offset where the field is defined. |
| type | `Type | null` |
| info | TypeInfo | The 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
| Name | Type | Description |
|---|---|---|
| current_info | TypeInfo | The TypeInfo of the model currently being analyzed. |
| typed | boolean | Whether to include explicit type annotations in the generated argument. |
| model_strict | boolean | The default strictness setting of the parent model to use if the field doesn't specify one. |
| force_optional | boolean | If true, forces the argument to be treated as optional regardless of default values. |
| use_alias | boolean | Whether to use the field's alias as the argument name instead of the attribute name. |
| api | SemanticAnalyzerPluginInterface | The mypy semantic analyzer interface for type lookups. |
| force_typevars_invariant | boolean | Whether to force type variables to be invariant to avoid variance errors in method signatures. |
| is_root_model_root | boolean | Indicates if this field represents the 'root' field of a Pydantic RootModel. |
Returns
| Type | Description |
|---|---|
Argument | A 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
| Name | Type | Description |
|---|---|---|
| current_info | TypeInfo | The TypeInfo used as the context for expanding generic type variables. |
| api | SemanticAnalyzerPluginInterface | The mypy semantic analyzer interface used to access options like strict_optional. |
| force_typevars_invariant | boolean | If true, modifies type variables to be invariant during expansion. |
| include_root_type | boolean | If true, allows the type to be a Union of the model and its root type for Pydantic RootModels. |
Returns
| Type | Description |
|---|---|
| `Type | null` |
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
| Name | Type | Description |
|---|---|---|
| current_info | TypeInfo | The TypeInfo used for type expansion context. |
| api | SemanticAnalyzerPluginInterface | The mypy semantic analyzer interface. |
| use_alias | boolean | Whether to name the variable using the field's alias. |
| force_typevars_invariant | boolean | Whether to force invariance on type variables during type expansion. |
Returns
| Type | Description |
|---|---|
Var | A mypy Var object representing the field as a class variable. |
serialize()
@classmethod
def serialize() - > JsonDict
Based on mypy.plugins.dataclasses.DataclassAttribute.serialize.
Returns
| Type | Description |
|---|---|
JsonDict | A 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
| Name | Type | Description |
|---|---|---|
| info | TypeInfo | The TypeInfo of the class owning this field. |
| data | JsonDict | The serialized field data. |
| api | SemanticAnalyzerPluginInterface | The mypy semantic analyzer interface used to fix up types during deserialization. |
Returns
| Type | Description |
|---|---|
[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
| Name | Type | Description |
|---|---|---|
| sub_type | TypeInfo | The TypeInfo of the inheriting subtype used to map generic parameters. |
| api | SemanticAnalyzerPluginInterface | The mypy semantic analyzer interface. |
Returns
| Type | Description |
|---|---|
null | None |