From 03981f3e31b6515258ce1cd09885f77fdb0d647b Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Tue, 7 Feb 2023 17:02:39 -0800 Subject: [PATCH 1/2] Fix crash on deferred value constrained TypeVar Fixes #14631 --- mypy/types.py | 6 +++++- test-data/unit/check-typevar-values.test | 9 +++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/mypy/types.py b/mypy/types.py index 90d33839c693..077627eefb17 100644 --- a/mypy/types.py +++ b/mypy/types.py @@ -595,7 +595,11 @@ def __hash__(self) -> int: def __eq__(self, other: object) -> bool: if not isinstance(other, TypeVarType): return NotImplemented - return self.id == other.id and self.upper_bound == other.upper_bound + return ( + self.id == other.id + and self.upper_bound == other.upper_bound + and self.values == other.values + ) def serialize(self) -> JsonDict: assert not self.id.is_meta_var() diff --git a/test-data/unit/check-typevar-values.test b/test-data/unit/check-typevar-values.test index d5a94f96fae7..a4a4d68bd9fe 100644 --- a/test-data/unit/check-typevar-values.test +++ b/test-data/unit/check-typevar-values.test @@ -702,3 +702,12 @@ class Indexable: [builtins fixtures/tuple.pyi] [builtins fixtures/classmethod.pyi] + +[case testTypeVarWithValueDeferral] +from typing import TypeVar, Callable + +T = TypeVar("T", "A", "B") +Func = Callable[[], T] + +class A: ... +class B: ... From 61914c2dac17b54fd9d8b1087dcb3391b8ac1720 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Wed, 8 Feb 2023 11:18:32 -0800 Subject: [PATCH 2/2] also add to hash --- mypy/types.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mypy/types.py b/mypy/types.py index 077627eefb17..f02f862688e4 100644 --- a/mypy/types.py +++ b/mypy/types.py @@ -590,7 +590,7 @@ def accept(self, visitor: TypeVisitor[T]) -> T: return visitor.visit_type_var(self) def __hash__(self) -> int: - return hash((self.id, self.upper_bound)) + return hash((self.id, self.upper_bound, tuple(self.values))) def __eq__(self, other: object) -> bool: if not isinstance(other, TypeVarType):