Transform the BaseModel subclass according to the plugin settings.
Attributes
| Attribute | Type | Description |
|---|
| tracked_config_fields | set[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
| Name | Type | Description |
|---|
| cls | ClassDef | The class definition to be transformed. |
| reason | `Expression | Statement` |
| api | SemanticAnalyzerPluginInterface | The 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
@classmethod
def transform() - > bool
Configures the BaseModel subclass according to the plugin settings.
Returns
| Type | Description |
|---|
bool | True 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
| Type | Description |
|---|
[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
| Name | Type | Description |
|---|
| model_config | [ModelConfigData](../v1/mypy/modelconfigdata.md?sid=pydantic_v1_mypy_modelconfigdata) | The configuration settings used to determine field behavior |
| is_root_model | bool | Whether the model is a Pydantic RootModel |
Returns
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
| Name | Type | Description |
|---|
| stmt | AssignmentStmt | The statement. |
| model_config | [ModelConfigData](../v1/mypy/modelconfigdata.md?sid=pydantic_v1_mypy_modelconfigdata) | Configuration settings for the model. |
| class_vars | dict[str, [PydanticModelClassVar](pydanticmodelclassvar.md?sid=pydantic_mypy_pydanticmodelclassvar)] | ClassVars already known to be defined on the model. |
Returns
| Type | Description |
|---|
| `PydanticModelField | PydanticModelClassVar |
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
| Name | Type | Description |
|---|
| fields | list[[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_settings | bool | Whether the class is a BaseSettings subclass |
| is_root_model | bool | Whether 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
| Name | Type | Description |
|---|
| fields | list[[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_settings | bool | Whether the class is a BaseSettings subclass |
| is_root_model | bool | Whether 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
| Name | Type | Description |
|---|
| fields | list[[PydanticModelField](../v1/mypy/pydanticmodelfield.md?sid=pydantic_v1_mypy_pydanticmodelfield)] | The fields to be marked as frozen |
| api | SemanticAnalyzerPluginInterface | The mypy semantic analyzer interface |
| frozen | bool | Whether 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
| Name | Type | Description |
|---|
| name | str | The name of the configuration field |
| arg | Expression | The expression assigned to the configuration field |
| lax_extra | bool | If True, suppresses errors for uninterpretable 'extra' values |
Returns
| Type | Description |
|---|
| `ModelConfigData | None` |
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
| Name | Type | Description |
|---|
| stmt | AssignmentStmt | The assignment statement defining the field |
Returns
| Type | Description |
|---|
bool | True 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
| Name | Type | Description |
|---|
| stmt | AssignmentStmt | The assignment statement defining the field |
Returns
| Type | Description |
|---|
| `bool | None` |
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
| Name | Type | Description |
|---|
| stmt | AssignmentStmt | The assignment statement defining the field |
Returns
| Type | Description |
|---|
| `tuple[str | None, 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
| Name | Type | Description |
|---|
| stmt | AssignmentStmt | The assignment statement defining the field |
Returns
| Type | Description |
|---|
bool | True 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
| Name | Type | Description |
|---|
| fields | list[[PydanticModelField](../v1/mypy/pydanticmodelfield.md?sid=pydantic_v1_mypy_pydanticmodelfield)] | The fields to convert into method arguments |
| typed | bool | Whether to include type annotations in the arguments |
| model_strict | bool | Whether the model is in strict mode |
| use_alias | bool | Whether to use field aliases for argument names |
| requires_dynamic_aliases | bool | Whether dynamic aliases require making arguments optional |
| is_settings | bool | Whether the model is a BaseSettings subclass |
| is_root_model | bool | Whether the model is a RootModel |
| force_typevars_invariant | bool | Whether to force type variables to be invariant |
Returns
| Type | Description |
|---|
list[Argument] | A list of mypy Argument instances for use in the generated signatures |
@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
| Name | Type | Description |
|---|
| fields | list[[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
| Type | Description |
|---|
bool | True 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
| Name | Type | Description |
|---|
| fields | list[[PydanticModelField](../v1/mypy/pydanticmodelfield.md?sid=pydantic_v1_mypy_pydanticmodelfield)] | The fields to check for dynamic aliases |
| has_alias_generator | bool | Whether the model has an alias generator configured |
Returns
| Type | Description |
|---|
bool | True if any field has a dynamic alias or if an alias generator is used on un-aliased fields |