Skip to content

Commit fdb31bb

Browse files
committed
fixup! specify that type variable tuple should have variance
1 parent cf63add commit fdb31bb

3 files changed

Lines changed: 24 additions & 6 deletions

File tree

conformance/tests/generics_typevartuple_basic.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from typing import Generic, NewType, TypeVarTuple, assert_type
88

99
Ts = TypeVarTuple("Ts")
10+
Ts1 = TypeVarTuple("Ts1")
1011

1112

1213
class Array1(Generic[*Ts]):
@@ -103,5 +104,9 @@ def func3(x: Array[Height], y: Array[Width], z: Array[Height, Width]):
103104
# > Only a single type variable tuple may appear in a type parameter list.
104105

105106

107+
class Array3(Generic[*Ts, *Ts1]): # E
108+
...
109+
110+
106111
class Array3[*Ts1, *Ts2]: # E
107112
...

conformance/tests/generics_typevartuple_variance.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,22 @@ def f(self) -> tuple[*OutTs]:
3737
]()
3838

3939

40-
InTs = TypeVarTuple("InTs", contravariant=True)
40+
Ts = TypeVarTuple("Ts")
41+
42+
class InvariantTypeVarTupleOld(Generic[*Ts]):
43+
def in_f(self, *args: *InTs) -> None: # OK
44+
raise NotImplementedError
45+
46+
def out_f(self) -> tuple[*InTs]: # OK
47+
raise NotImplementedError
4148

4249

50+
obj_old: InvariantTypeVarTupleOld[object] = InvariantTypeVarTupleOld[int]() # E
51+
int_old: InvariantTypeVarTupleOld[int] = InvariantTypeVarTupleOld[object]() # E
52+
53+
54+
InTs = TypeVarTuple("InTs", contravariant=True)
55+
4356
class ContravariantTypeVarTupleOld(Generic[*InTs]):
4457
def in_f(self, *args: *InTs) -> None: # OK
4558
raise NotImplementedError
@@ -51,8 +64,8 @@ def out_f(self) -> tuple[*InTs]: # E
5164
in_obj_old: ContravariantTypeVarTupleOld[object] = ContravariantTypeVarTupleOld[int]() # E
5265
in_int_old: ContravariantTypeVarTupleOld[int] = ContravariantTypeVarTupleOld[object]() # OK
5366

54-
OutTs = TypeVarTuple("OutTs", covariant=True)
5567

68+
OutTs = TypeVarTuple("OutTs", covariant=True)
5669

5770
class CovariantTypeVarTupleOld(Generic[*OutTs]):
5871
def in_f(self, *args: *OutTs) -> None: # E

docs/spec/generics.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2724,10 +2724,10 @@ instance (a concrete anonymous class that is assumed to meet the bounds or
27242724
constraints of the type parameter). In the ``upper`` specialized class,
27252725
specialize the target type parameter with:
27262726

2727-
an ``object`` instance for a type variable.
2728-
a ``*tuple[object, ...]`` value for a type variable tuple.
2729-
a for a parameter specification, a 'top signature' value, i.e. a type
2730-
that represents the super type of every possible signature.
2727+
an ``object`` instance for a type variable. a ``*tuple[object, ...]``
2728+
value for a type variable tuple. For a parameter specification,
2729+
a 'top signature' value, i.e. a type that represents the super type of
2730+
every possible signature.
27312731

27322732
This specialization ignores the type parameter's upper bound or constraints.
27332733
In the ``lower`` specialized class, specialize the target type parameter with

0 commit comments

Comments
 (0)