Skip to main content

PydanticModelTransformer

Transform the BaseModel subclass according to the plugin settings.

Attributes

AttributeTypeDescription
tracked_config_fieldsset[str] = {'extra', 'frozen', 'from_attributes', 'populate_by_name', 'validate_by_alias', 'validate_by_name', 'alias_generator', 'strict'}A set of field configs that the plugin has to track their value.

Constructor

Signature

def PydanticModelTransformer(
cls: ClassDef,
reason: Expression | Statement,
api: SemanticAnalyzerPluginInterface,
plugin_config: [PydanticPluginConfig](../v1/mypy/pydanticpluginconfig.md?sid=pydantic_v1_mypy_pydanticpluginconfig)
) - > None

Parameters

NameTypeDescription
clsClassDefThe class definition to be transformed.
reason`ExpressionStatement`
apiSemanticAnalyzerPluginInterfaceThe interface to the mypy semantic analyzer.
plugin_config[PydanticPluginConfig](../v1/mypy/pydanticpluginconfig.md?sid=pydantic_v1_mypy_pydanticpluginconfig)Configuration settings for the Pydantic mypy plugin.

Methods


transform()

@classmethod
def transform() - > bool

Configures the BaseModel subclass according to the plugin settings.

Returns

TypeDescription
boolTrue if the transformation was successful, False if some definitions are not ready and require another pass

adjust_decorator_signatures()

@classmethod
def adjust_decorator_signatures()

Teach mypy that functions decorated with pydantic validators or serializers act as class methods by marking them as such.


collect_config()

@classmethod
def collect_config() - > [ModelConfigData](../v1/mypy/modelconfigdata.md?sid=pydantic_v1_mypy_modelconfigdata)

Collects the values of the config attributes that are used by the plugin, accounting for parent classes.

Returns

TypeDescription
[ModelConfigData](../v1/mypy/modelconfigdata.md?sid=pydantic_v1_mypy_modelconfigdata)The aggregated configuration data for the model

collect_fields_and_class_vars()

@classmethod
def collect_fields_and_class_vars(
model_config: [ModelConfigData](../v1/mypy/modelconfigdata.md?sid=pydantic_v1_mypy_modelconfigdata),
is_root_model: bool
) - > tuple[list[[PydanticModelField](../v1/mypy/pydanticmodelfield.md?sid=pydantic_v1_mypy_pydanticmodelfield)]| None, list[[PydanticModelClassVar](pydanticmodelclassvar.md?sid=pydantic_mypy_pydanticmodelclassvar)]| None]

Collects the fields for the model, accounting for parent classes.

Parameters

NameTypeDescription
model_config[ModelConfigData](../v1/mypy/modelconfigdata.md?sid=pydantic_v1_mypy_modelconfigdata)The configuration settings used to determine field behavior
is_root_modelboolWhether the model is a Pydantic RootModel

Returns

TypeDescription
`tuple[list[PydanticModelField]None, list[PydanticModelClassVar]

collect_field_or_class_var_from_stmt()

@classmethod
def collect_field_or_class_var_from_stmt(
stmt: AssignmentStmt,
model_config: [ModelConfigData](../v1/mypy/modelconfigdata.md?sid=pydantic_v1_mypy_modelconfigdata),
class_vars: dict[str, [PydanticModelClassVar](pydanticmodelclassvar.md?sid=pydantic_mypy_pydanticmodelclassvar)]
) - > PydanticModelField | PydanticModelClassVar | None

Get pydantic model field from statement.

Parameters

NameTypeDescription
stmtAssignmentStmtThe statement.
model_config[ModelConfigData](../v1/mypy/modelconfigdata.md?sid=pydantic_v1_mypy_modelconfigdata)Configuration settings for the model.
class_varsdict[str, [PydanticModelClassVar](pydanticmodelclassvar.md?sid=pydantic_mypy_pydanticmodelclassvar)]ClassVars already known to be defined on the model.

Returns

TypeDescription
`PydanticModelFieldPydanticModelClassVar

add_initializer()

@classmethod
def add_initializer(
fields: list[[PydanticModelField](../v1/mypy/pydanticmodelfield.md?sid=pydantic_v1_mypy_pydanticmodelfield)],
config: [ModelConfigData](../v1/mypy/modelconfigdata.md?sid=pydantic_v1_mypy_modelconfigdata),
is_settings: bool,
is_root_model: bool
)

Adds a fields-aware __init__ method to the class.

Parameters

NameTypeDescription
fieldslist[[PydanticModelField](../v1/mypy/pydanticmodelfield.md?sid=pydantic_v1_mypy_pydanticmodelfield)]The list of fields to include in the initializer signature
config[ModelConfigData](../v1/mypy/modelconfigdata.md?sid=pydantic_v1_mypy_modelconfigdata)The model configuration data
is_settingsboolWhether the class is a BaseSettings subclass
is_root_modelboolWhether the class is a RootModel

add_model_construct_method()

@classmethod
def add_model_construct_method(
fields: list[[PydanticModelField](../v1/mypy/pydanticmodelfield.md?sid=pydantic_v1_mypy_pydanticmodelfield)],
config: [ModelConfigData](../v1/mypy/modelconfigdata.md?sid=pydantic_v1_mypy_modelconfigdata),
is_settings: bool,
is_root_model: bool
)

Adds a fully typed model_construct classmethod to the class.

Parameters

NameTypeDescription
fieldslist[[PydanticModelField](../v1/mypy/pydanticmodelfield.md?sid=pydantic_v1_mypy_pydanticmodelfield)]The list of fields to include in the construct method
config[ModelConfigData](../v1/mypy/modelconfigdata.md?sid=pydantic_v1_mypy_modelconfigdata)The model configuration data
is_settingsboolWhether the class is a BaseSettings subclass
is_root_modelboolWhether the class is a RootModel

set_frozen()

@classmethod
def set_frozen(
fields: list[[PydanticModelField](../v1/mypy/pydanticmodelfield.md?sid=pydantic_v1_mypy_pydanticmodelfield)],
api: SemanticAnalyzerPluginInterface,
frozen: bool
)

Marks all fields as properties so that attempts to set them trigger mypy errors.

Parameters

NameTypeDescription
fieldslist[[PydanticModelField](../v1/mypy/pydanticmodelfield.md?sid=pydantic_v1_mypy_pydanticmodelfield)]The fields to be marked as frozen
apiSemanticAnalyzerPluginInterfaceThe mypy semantic analyzer interface
frozenboolWhether the entire model is configured as frozen

get_config_update()

@classmethod
def get_config_update(
name: str,
arg: Expression,
lax_extra: bool
) - > ModelConfigData | None

Determines the config update due to a single kwarg in the ConfigDict definition.

Parameters

NameTypeDescription
namestrThe name of the configuration field
argExpressionThe expression assigned to the configuration field
lax_extraboolIf True, suppresses errors for uninterpretable 'extra' values

Returns

TypeDescription
`ModelConfigDataNone`

get_has_default()

@classmethod
def get_has_default(
stmt: AssignmentStmt
) - > bool

Returns a boolean indicating whether the field defined in stmt is a required field.

Parameters

NameTypeDescription
stmtAssignmentStmtThe assignment statement defining the field

Returns

TypeDescription
boolTrue if the field has a default value or factory, False otherwise

get_strict()

@classmethod
def get_strict(
stmt: AssignmentStmt
) - > bool | None

Returns a the strict value of a field if defined, otherwise None.

Parameters

NameTypeDescription
stmtAssignmentStmtThe assignment statement defining the field

Returns

TypeDescription
`boolNone`

get_alias_info()

@classmethod
def get_alias_info(
stmt: AssignmentStmt
) - > tuple[str | None, bool]

Returns a pair (alias, has_dynamic_alias), extracted from the declaration of the field defined in stmt.

Parameters

NameTypeDescription
stmtAssignmentStmtThe assignment statement defining the field

Returns

TypeDescription
`tuple[strNone, bool]`

is_field_frozen()

@classmethod
def is_field_frozen(
stmt: AssignmentStmt
) - > bool

Returns whether the field is frozen, extracted from the declaration of the field defined in stmt.

Parameters

NameTypeDescription
stmtAssignmentStmtThe assignment statement defining the field

Returns

TypeDescription
boolTrue if the field is explicitly marked as frozen in its Field definition

get_field_arguments()

@classmethod
def get_field_arguments(
fields: list[[PydanticModelField](../v1/mypy/pydanticmodelfield.md?sid=pydantic_v1_mypy_pydanticmodelfield)],
typed: bool,
model_strict: bool,
use_alias: bool,
requires_dynamic_aliases: bool,
is_settings: bool,
is_root_model: bool,
force_typevars_invariant: bool
) - > list[Argument]

Helper function used during the construction of the __init__ and model_construct method signatures.

Parameters

NameTypeDescription
fieldslist[[PydanticModelField](../v1/mypy/pydanticmodelfield.md?sid=pydantic_v1_mypy_pydanticmodelfield)]The fields to convert into method arguments
typedboolWhether to include type annotations in the arguments
model_strictboolWhether the model is in strict mode
use_aliasboolWhether to use field aliases for argument names
requires_dynamic_aliasesboolWhether dynamic aliases require making arguments optional
is_settingsboolWhether the model is a BaseSettings subclass
is_root_modelboolWhether the model is a RootModel
force_typevars_invariantboolWhether to force type variables to be invariant

Returns

TypeDescription
list[Argument]A list of mypy Argument instances for use in the generated signatures

should_init_forbid_extra()

@classmethod
def should_init_forbid_extra(
fields: list[[PydanticModelField](../v1/mypy/pydanticmodelfield.md?sid=pydantic_v1_mypy_pydanticmodelfield)],
config: [ModelConfigData](../v1/mypy/modelconfigdata.md?sid=pydantic_v1_mypy_modelconfigdata)
) - > bool

Indicates whether the generated __init__ should get a **kwargs at the end of its signature.

Parameters

NameTypeDescription
fieldslist[[PydanticModelField](../v1/mypy/pydanticmodelfield.md?sid=pydantic_v1_mypy_pydanticmodelfield)]The fields defined on the model
config[ModelConfigData](../v1/mypy/modelconfigdata.md?sid=pydantic_v1_mypy_modelconfigdata)The model configuration data

Returns

TypeDescription
boolTrue if extra arguments should be forbidden in the initializer

is_dynamic_alias_present()

@classmethod
def is_dynamic_alias_present(
fields: list[[PydanticModelField](../v1/mypy/pydanticmodelfield.md?sid=pydantic_v1_mypy_pydanticmodelfield)],
has_alias_generator: bool
) - > bool

Returns whether any fields on the model have a "dynamic alias", i.e., an alias that cannot be determined during static analysis.

Parameters

NameTypeDescription
fieldslist[[PydanticModelField](../v1/mypy/pydanticmodelfield.md?sid=pydantic_v1_mypy_pydanticmodelfield)]The fields to check for dynamic aliases
has_alias_generatorboolWhether the model has an alias generator configured

Returns

TypeDescription
boolTrue if any field has a dynamic alias or if an alias generator is used on un-aliased fields