From 7ca3972ba343d25bfd2d9204110510c619153466 Mon Sep 17 00:00:00 2001 From: Christian Bundy Date: Thu, 16 Dec 2021 12:31:26 -0800 Subject: [PATCH] Add support for removing TypeVarDef in mypy 0.920 --- pydantic/mypy.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/pydantic/mypy.py b/pydantic/mypy.py index 119c1a07c1..c5ea8f1e5f 100644 --- a/pydantic/mypy.py +++ b/pydantic/mypy.py @@ -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: + # 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, @@ -59,7 +67,6 @@ Type, TypeOfAny, TypeType, - TypeVarDef, TypeVarType, UnionType, get_proper_type, @@ -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): + self_type = tvd + else: + self_type = TypeVarType(tvd) + add_method( ctx, 'construct',