Allow dynamic models created with create_model() to be used as annotations in the Mypy plugin#12879
Merged
Viicos merged 4 commits intopydantic:mainfrom Mar 7, 2026
Merged
Conversation
13 tasks
Implement a dynamic class hook so that variables assigned from create_model() are recognized by mypy as proper class types rather than plain variables. This allows them to be used in type annotations, isinstance() checks, and as function parameter types. The hook handles the __base__ keyword argument so the synthetic class inherits from the correct base when specified.
3d14ef8 to
3dd07ba
Compare
Contributor
Coverage reportClick to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||
Viicos
requested changes
Mar 5, 2026
Member
Viicos
left a comment
There was a problem hiding this comment.
Please also apply the following diff:
diff --git a/tests/mypy/modules/create_model_var.py b/tests/mypy/modules/create_model_var.py
new file mode 100644
index 000000000..cc2c72b1b
--- /dev/null
+++ b/tests/mypy/modules/create_model_var.py
@@ -0,0 +1,12 @@
+from pydantic import BaseModel, create_model
+
+
+class Model(BaseModel):
+ a: int
+
+
+SubModel = create_model('SubModel', __base__=Model)
+
+
+class Main(BaseModel):
+ sub: SubModel
diff --git a/tests/mypy/outputs/mypy-plugin_ini/create_model_var.py b/tests/mypy/outputs/mypy-plugin_ini/create_model_var.py
new file mode 100644
index 000000000..cc2c72b1b
--- /dev/null
+++ b/tests/mypy/outputs/mypy-plugin_ini/create_model_var.py
@@ -0,0 +1,12 @@
+from pydantic import BaseModel, create_model
+
+
+class Model(BaseModel):
+ a: int
+
+
+SubModel = create_model('SubModel', __base__=Model)
+
+
+class Main(BaseModel):
+ sub: SubModel
diff --git a/tests/mypy/test_mypy.py b/tests/mypy/test_mypy.py
index 679d184f9..4fa052776 100644
--- a/tests/mypy/test_mypy.py
+++ b/tests/mypy/test_mypy.py
@@ -109,6 +109,7 @@ cases: list[ParameterSet | tuple[str, str]] = [
('mypy-plugin.ini', 'root_models.py'),
('mypy-plugin.ini', 'plugin_strict_fields.py'),
('mypy-plugin.ini', 'final_with_default.py'),
+ ('mypy-plugin.ini', 'create_model_var.py'),
('mypy-plugin-strict-no-any.ini', 'dataclass_no_any.py'),
('mypy-plugin-very-strict.ini', 'metaclass_args.py'),
('pyproject-plugin-no-strict-optional.toml', 'no_strict_optional.py'),```get_dynamic_class_hook() for create_model() in mypy plugin
Contributor
Author
|
Thanks for the feedback! Updated: Added test cases for create_model_var as requested. Let me know if there's anything else. |
b68db51 to
abe4228
Compare
Fix typo where CREATE_MODEL_FULLNAMES (plural) was used instead of CREATE_MODEL_FULLNAME (singular), which was causing F821 lint errors.
7ef81c1 to
796db88
Compare
get_dynamic_class_hook() for create_model() in mypy plugincreate_model() to be used as annotations in the Mypy plugin
Viicos
approved these changes
Mar 7, 2026
1 task
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Change Summary
Add a
get_dynamic_class_hookto the pydantic mypy plugin so that variables assigned fromcreate_model()are recognized as proper class types. This allows them to be used in type annotations,isinstance()checks, and as function parameter types without mypy errors.The hook also handles the
__base__keyword argument so the synthetic class inherits from the correct base when specified.Related issue number
fix #12031
Checklist
make lintandmake format