From 7ca3972ba343d25bfd2d9204110510c619153466 Mon Sep 17 00:00:00 2001 From: Christian Bundy Date: Thu, 16 Dec 2021 12:31:26 -0800 Subject: [PATCH 1/5] 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', From 5dc2d27d2b8c22001982080d3bac349c0dd9c58e Mon Sep 17 00:00:00 2001 From: Christian Bundy Date: Thu, 16 Dec 2021 12:41:44 -0800 Subject: [PATCH 2/5] Add changes/3175-christianbundy.md --- changes/3175-christianbundy.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 changes/3175-christianbundy.md diff --git a/changes/3175-christianbundy.md b/changes/3175-christianbundy.md new file mode 100644 index 0000000000..3faf22eaf1 --- /dev/null +++ b/changes/3175-christianbundy.md @@ -0,0 +1 @@ +Add support for Mypy 0.920 From fa172fc12982f0bd87f28d8c834279681ca510c5 Mon Sep 17 00:00:00 2001 From: Samuel Colvin Date: Sat, 18 Dec 2021 20:26:44 +0000 Subject: [PATCH 3/5] type ignore on import --- pydantic/mypy.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pydantic/mypy.py b/pydantic/mypy.py index c5ea8f1e5f..7de5e8e40d 100644 --- a/pydantic/mypy.py +++ b/pydantic/mypy.py @@ -17,7 +17,7 @@ try: from mypy.types import TypeVarDef -except ImportError: +except ImportError: # type: ignore # Backward-compatible with TypeVarDef from Mypy 0.910. from mypy.types import TypeVarType as TypeVarDef # type: ignore[misc] From 1ce234d36f316c0f4ea86743a3315845a5b365a6 Mon Sep 17 00:00:00 2001 From: Samuel Colvin Date: Sat, 18 Dec 2021 20:30:44 +0000 Subject: [PATCH 4/5] :facepalm: --- pydantic/mypy.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pydantic/mypy.py b/pydantic/mypy.py index 7de5e8e40d..7abece31e2 100644 --- a/pydantic/mypy.py +++ b/pydantic/mypy.py @@ -17,7 +17,7 @@ try: from mypy.types import TypeVarDef -except ImportError: # type: ignore +except ImportError: # pragma: no cover # Backward-compatible with TypeVarDef from Mypy 0.910. from mypy.types import TypeVarType as TypeVarDef # type: ignore[misc] From dd2f639dad661291764e86c5be82c71ecaa8cc3c Mon Sep 17 00:00:00 2001 From: Samuel Colvin Date: Sat, 18 Dec 2021 20:42:07 +0000 Subject: [PATCH 5/5] coverage, again --- pydantic/mypy.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pydantic/mypy.py b/pydantic/mypy.py index 7abece31e2..c2120992f4 100644 --- a/pydantic/mypy.py +++ b/pydantic/mypy.py @@ -368,7 +368,7 @@ def add_construct_method(self, fields: List['PydanticModelField']) -> None: ctx.cls.info.names[self_tvar_name] = SymbolTableNode(MDEF, self_tvar_expr) # Backward-compatible with TypeVarDef from Mypy 0.910. - if isinstance(tvd, TypeVarType): + if isinstance(tvd, TypeVarType): # pragma: no cover self_type = tvd else: self_type = TypeVarType(tvd)