Skip to content

Commit 1fed961

Browse files
committed
Check typevar defaults with is_same_type instead of full equality
1 parent e867132 commit 1fed961

File tree

4 files changed

+47
-4
lines changed

4 files changed

+47
-4
lines changed

mypy/checker.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2658,7 +2658,7 @@ def check_typevar_defaults(self, tvars: Sequence[TypeVarLikeType]) -> None:
26582658
continue
26592659
if not is_subtype(tv.default, tv.upper_bound):
26602660
self.fail("TypeVar default must be a subtype of the bound type", tv)
2661-
if tv.values and not any(tv.default == value for value in tv.values):
2661+
if tv.values and not any(is_same_type(tv.default, value) for value in tv.values):
26622662
self.fail("TypeVar default must be one of the constraint types", tv)
26632663

26642664
def check_enum(self, defn: ClassDef) -> None:

mypy/checkexpr.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -6210,7 +6210,7 @@ def visit_type_var_expr(self, e: TypeVarExpr) -> Type:
62106210
):
62116211
if not is_subtype(p_default, e.upper_bound):
62126212
self.chk.fail("TypeVar default must be a subtype of the bound type", e)
6213-
if e.values and not any(p_default == value for value in e.values):
6213+
if e.values and not any(is_same_type(p_default, value) for value in e.values):
62146214
self.chk.fail("TypeVar default must be one of the constraint types", e)
62156215
return AnyType(TypeOfAny.special_form)
62166216

test-data/unit/check-python312.test

+12
Original file line numberDiff line numberDiff line change
@@ -2042,3 +2042,15 @@ tuple[*tuple[int, ...], *tuple[int, ...]] # E: More than one Unpack in a type i
20422042
b: tuple[*tuple[int, ...], *tuple[int, ...]] # E: More than one Unpack in a type is not allowed
20432043
[builtins fixtures/tuple.pyi]
20442044
[typing fixtures/typing-full.pyi]
2045+
2046+
[case testPEP695TypeVarDefaultAliases]
2047+
from typing_extensions import TypeVar
2048+
2049+
type K = int
2050+
type V = int
2051+
type L = list[int]
2052+
2053+
T1 = TypeVar("T1", str, K, default=K)
2054+
T2 = TypeVar("T2", str, K, default=V)
2055+
T3 = TypeVar("T3", str, L, default=L)
2056+
[builtins fixtures/tuple.pyi]

test-data/unit/check-typevar-defaults.test

+33-2
Original file line numberDiff line numberDiff line change
@@ -729,8 +729,6 @@ class C(Generic[_I]): pass
729729
t: type[C] | int = C
730730
[builtins fixtures/tuple.pyi]
731731

732-
733-
734732
[case testGenericTypeAliasWithDefaultTypeVarPreservesNoneInDefault]
735733
from typing_extensions import TypeVar
736734
from typing import Generic, Union
@@ -749,3 +747,36 @@ MyA = A[T1, int]
749747
a: MyA = A(None, 10)
750748
reveal_type(a.a) # N: Revealed type is "Union[builtins.int, None]"
751749
[builtins fixtures/tuple.pyi]
750+
751+
[case testTypeVarDefaultAliasesTypeAliasType]
752+
from typing_extensions import TypeAliasType, TypeVar
753+
754+
K = TypeAliasType("K", int)
755+
V = TypeAliasType("V", int)
756+
L = TypeAliasType("L", list[int])
757+
T1 = TypeVar("T1", str, K, default=K)
758+
T2 = TypeVar("T2", str, K, default=V)
759+
T3 = TypeVar("T3", str, L, default=L)
760+
[builtins fixtures/tuple.pyi]
761+
762+
[case testTypeVarDefaultAliasesImplicitAlias]
763+
from typing_extensions import TypeVar
764+
765+
K = int
766+
V = int
767+
L = list[int]
768+
T1 = TypeVar("T1", str, K, default=K)
769+
T2 = TypeVar("T2", str, K, default=V)
770+
T3 = TypeVar("T3", str, L, default=L)
771+
[builtins fixtures/tuple.pyi]
772+
773+
[case testTypeVarDefaultAliasesExplicitAlias]
774+
from typing_extensions import TypeAlias, TypeVar
775+
776+
K: TypeAlias = int
777+
V: TypeAlias = int
778+
L: TypeAlias = list[int]
779+
T1 = TypeVar("T1", str, K, default=K)
780+
T2 = TypeVar("T2", str, K, default=V)
781+
T3 = TypeVar("T3", str, L, default=L)
782+
[builtins fixtures/tuple.pyi]

0 commit comments

Comments
 (0)