From b99c37e3d0f451ab36ca77fc53ca09947faff112 Mon Sep 17 00:00:00 2001 From: Selim Belhaouane Date: Thu, 8 Oct 2020 16:07:22 -0400 Subject: [PATCH] Fix #1770 (#1771) --- changes/1770-selimb.md | 1 + pydantic/mypy.py | 2 +- tests/mypy/modules/plugin_success.py | 10 ++++++++++ 3 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 changes/1770-selimb.md diff --git a/changes/1770-selimb.md b/changes/1770-selimb.md new file mode 100644 index 0000000..bfd7284 --- /dev/null +++ b/changes/1770-selimb.md @@ -0,0 +1 @@ +Fix false positive from mypy plugin when a class nested within a `BaseModel` is named `Model`. diff --git a/pydantic/mypy.py b/pydantic/mypy.py index 4881d8d..18d7ef0 100644 --- a/pydantic/mypy.py +++ b/pydantic/mypy.py @@ -328,7 +328,7 @@ class PydanticModelTransformer: construct_arguments = [fields_set_argument] + construct_arguments obj_type = ctx.api.named_type('__builtins__.object') - self_tvar_name = 'Model' + self_tvar_name = '_PydanticBaseModel' # Make sure it does not conflict with other names in the class tvar_fullname = ctx.cls.fullname + '.' + self_tvar_name tvd = TypeVarDef(self_tvar_name, tvar_fullname, -1, [], obj_type) self_tvar_expr = TypeVarExpr(self_tvar_name, tvar_fullname, [], obj_type) diff --git a/tests/mypy/modules/plugin_success.py b/tests/mypy/modules/plugin_success.py index 8b0c6af..af4b8e3 100644 --- a/tests/mypy/modules/plugin_success.py +++ b/tests/mypy/modules/plugin_success.py @@ -123,3 +123,13 @@ p = AddProject(name='x', slug='y', description='z') class TypeAliasAsAttribute(BaseModel): __type_alias_attribute__ = Union[str, bytes] + + +class NestedModel(BaseModel): + class Model(BaseModel): + id: str + + model: Model + + +_ = NestedModel.Model