Repository: pydantic/pydantic

Issue:
Nested `Model` model definition raises "Type variable cannot be used as an expression" in mypy with plugin
# Bug

Output of `python -c "import pydantic.utils; print(pydantic.utils.version_info())"`:
```
             pydantic version: 1.6.1
            pydantic compiled: False
                 install path: D:\sand\pydantic-nested\.venv\Lib\site-packages\pydantic
               python version: 3.7.6 (tags/v3.7.6:43364a7ae0, Dec 18 2019, 23:46:00) [MSC v.1916 32 bit (Intel)]
                     platform: Windows-10-10.0.19559-SP0
     optional deps. installed: ['typing-extensions']
```
Additionally, I'm using `mypy==0.782` (latest as of today).

```py
# bug.py
import pydantic

class Job(pydantic.BaseModel):
    class Model(pydantic.BaseModel):
        id: str
        md5: str

    model: Model

Job.Model  # error on this line
```
```ini
# setup.cfg
[mypy]
plugins = pydantic.mypy
```
```
$ mypy bug.py 
bug.py:10: error: Type variable "Article.Model" cannot be used as an expression
Found 1 error in 1 file (checked 1 source file)
```
If I rename `Model` to anything else, the mypy error disappears.
Nested `Model` model definition raises "Type variable cannot be used as an expression" in mypy with plugin
# Bug

Output of `python -c "import pydantic.utils; print(pydantic.utils.version_info())"`:
```
             pydantic version: 1.6.1
            pydantic compiled: False
                 install path: D:\sand\pydantic-nested\.venv\Lib\site-packages\pydantic
               python version: 3.7.6 (tags/v3.7.6:43364a7ae0, Dec 18 2019, 23:46:00) [MSC v.1916 32 bit (Intel)]
                     platform: Windows-10-10.0.19559-SP0
     optional deps. installed: ['typing-extensions']
```
Additionally, I'm using `mypy==0.782` (latest as of today).

```py
# bug.py
import pydantic

class Job(pydantic.BaseModel):
    class Model(pydantic.BaseModel):
        id: str
        md5: str

    model: Model

Job.Model  # error on this line
```
```ini
# setup.cfg
[mypy]
plugins = pydantic.mypy
```
```
$ mypy bug.py 
bug.py:10: error: Type variable "Article.Model" cannot be used as an expression
Found 1 error in 1 file (checked 1 source file)
```
If I rename `Model` to anything else, the mypy error disappears.


Discussion:
Believe I've found the fix. Will submit PR shortly.
Believe I've found the fix. Will submit PR shortly.

Resolving patch:
diff --git a/pydantic/mypy.py b/pydantic/mypy.py
--- a/pydantic/mypy.py
+++ b/pydantic/mypy.py
@@ -328,7 +328,7 @@ def add_construct_method(self, fields: List['PydanticModelField']) -> None:
         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)


Test patch:
diff --git a/tests/mypy/modules/plugin_success.py b/tests/mypy/modules/plugin_success.py
--- a/tests/mypy/modules/plugin_success.py
+++ b/tests/mypy/modules/plugin_success.py
@@ -123,3 +123,13 @@ class AddProject:
 
 class TypeAliasAsAttribute(BaseModel):
     __type_alias_attribute__ = Union[str, bytes]
+
+
+class NestedModel(BaseModel):
+    class Model(BaseModel):
+        id: str
+
+    model: Model
+
+
+_ = NestedModel.Model