Skip to content

Commit

Permalink
Add support for removing TypeVarDef in mypy 0.920 (#3175)
Browse files Browse the repository at this point in the history
* Add support for removing TypeVarDef in mypy 0.920

* Add changes/3175-christianbundy.md

* type ignore on import

* 🤦

* coverage, again

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
  • Loading branch information
christianbundy and samuelcolvin committed Dec 18, 2021
1 parent 7eaa582 commit 49f946d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
1 change: 1 addition & 0 deletions changes/3175-christianbundy.md
@@ -0,0 +1 @@
Add support for Mypy 0.920
17 changes: 15 additions & 2 deletions pydantic/mypy.py
Expand Up @@ -14,6 +14,14 @@

warnings.warn('No TOML parser installed, cannot read configuration from `pyproject.toml`.')
toml = None # type: ignore

try:
from mypy.types import TypeVarDef
except ImportError: # pragma: no cover
# Backward-compatible with TypeVarDef from Mypy 0.910.
from mypy.types import TypeVarType as TypeVarDef # type: ignore[misc]


from mypy.errorcodes import ErrorCode
from mypy.nodes import (
ARG_NAMED,
Expand Down Expand Up @@ -59,7 +67,6 @@
Type,
TypeOfAny,
TypeType,
TypeVarDef,
TypeVarType,
UnionType,
get_proper_type,
Expand Down Expand Up @@ -359,7 +366,13 @@ def add_construct_method(self, fields: List['PydanticModelField']) -> None:
tvd = TypeVarDef(self_tvar_name, tvar_fullname, -1, [], obj_type)
self_tvar_expr = TypeVarExpr(self_tvar_name, tvar_fullname, [], obj_type)
ctx.cls.info.names[self_tvar_name] = SymbolTableNode(MDEF, self_tvar_expr)
self_type = TypeVarType(tvd)

# Backward-compatible with TypeVarDef from Mypy 0.910.
if isinstance(tvd, TypeVarType): # pragma: no cover
self_type = tvd
else:
self_type = TypeVarType(tvd)

add_method(
ctx,
'construct',
Expand Down

0 comments on commit 49f946d

Please sign in to comment.