Initial Checks
Description
Pydantic provides a way to create models dynamically (create_model).
The problem is that mypy always complain about these models:
a.py content:
from pydantic import create_model, BaseModel
from pydantic.fields import Field
GeneratedModel = create_model(
"MyModel",
some_field=(int, Field(default=0))
)
class MyModel(BaseModel):
a: int
model: GeneratedModel
$ mypy a.py
a.py:12: error: Variable "a.GeneratedModel" is not valid as a type [valid-type]
a.py:12: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
Found 1 error in 1 file (checked 1 source file)
Currently, you are forced to add # type: ignore[valid-type] on each line where this model is used, which is a problem because it adds a lot of noise but also disables the check for all variables in the same line as this type: ignore.
I've asked in mypy github project and developers suggested pydantic's mypy plugin could be updated to use get_dynamic_class_hook for pydantic's create_model use-case.
Affected Components
Initial Checks
Description
Pydantic provides a way to create models dynamically (create_model).
The problem is that mypy always complain about these models:
a.py content:
$ mypy a.pyCurrently, you are forced to add # type: ignore[valid-type] on each line where this model is used, which is a problem because it adds a lot of noise but also disables the check for all variables in the same line as this type: ignore.
I've asked in mypy github project and developers suggested pydantic's mypy plugin could be updated to use
get_dynamic_class_hookfor pydantic'screate_modeluse-case.Affected Components
.model_dump()and.model_dump_json()model_construct(), pickling, private attributes, ORM mode