From 5af8b75b421443693b3a6f2d37260300d06b7ba4 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Sun, 24 Oct 2021 11:06:31 +0300 Subject: [PATCH 1/2] Now `TypeVar` with values correctly handles `is_subtype` check --- mypy/subtypes.py | 5 +++-- test-data/unit/check-typevar-values.test | 13 +++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/mypy/subtypes.py b/mypy/subtypes.py index 753fa74b7744..bd3bc4de85ea 100644 --- a/mypy/subtypes.py +++ b/mypy/subtypes.py @@ -295,8 +295,9 @@ def visit_instance(self, left: Instance) -> bool: if call: return self._is_subtype(call, right) return False - else: - return False + if isinstance(right, TypeVarType) and right.values: + return left in right.values + return False def visit_type_var(self, left: TypeVarType) -> bool: right = self.right diff --git a/test-data/unit/check-typevar-values.test b/test-data/unit/check-typevar-values.test index 3f77996ec959..def2274670ba 100644 --- a/test-data/unit/check-typevar-values.test +++ b/test-data/unit/check-typevar-values.test @@ -631,3 +631,16 @@ def g(s: S) -> Callable[[S], None]: ... def f(x: S) -> None: h = g(x) h(x) + +[case testTypeVarWithValuesAndSuper] +from typing import Generic, TypeVar + +AnyStr = TypeVar('AnyStr', bytes, str) + +class Foo(Generic[AnyStr]): + def method1(self, s: AnyStr) -> None: + pass + +class Bar(Foo[AnyStr]): + def method1(self, s: AnyStr) -> None: + super().method1(s) From a7d3a34c1c5b57fddafcdc97b0b39950a1005419 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Sun, 24 Oct 2021 15:51:38 +0300 Subject: [PATCH 2/2] Fixes subtypes condition --- mypy/subtypes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mypy/subtypes.py b/mypy/subtypes.py index bd3bc4de85ea..98397d84c157 100644 --- a/mypy/subtypes.py +++ b/mypy/subtypes.py @@ -296,7 +296,7 @@ def visit_instance(self, left: Instance) -> bool: return self._is_subtype(call, right) return False if isinstance(right, TypeVarType) and right.values: - return left in right.values + return any(self._is_subtype(left, r_val) for r_val in right.values) return False def visit_type_var(self, left: TypeVarType) -> bool: