Skip to content

Commit f046c5d

Browse files
committed
fixup! specify that parameter specification should have variance
fixup! fixup! specify that parameter specification should have variance
1 parent 03cf715 commit f046c5d

3 files changed

Lines changed: 28 additions & 6 deletions

File tree

conformance/tests/generics_paramspec_variance.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@
77

88
from typing import Callable, Generic, ParamSpec
99

10+
11+
class InvariantParamSpec[**InOutP]:
12+
a: Callable[InOutP, None]
13+
14+
in_out_obj: InvariantParamSpec[object] = InvariantParamSpec[int]() # E
15+
in_out_int: InvariantParamSpec[int] = InvariantParamSpec[object]() # E
16+
17+
1018
class ContravariantParamSpec[**InP]:
1119
def f(self, *args: InP.args, **kwargs: InP.kwargs): ...
1220

conformance/tests/generics_typevartuple_variance.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,19 @@
77

88
from typing import Generic, TypeVarTuple
99

10+
class InvariantTypeVarTuple[*InOutTs]:
11+
a: tuple[*InOutTs]
12+
13+
in_out_obj: InvariantTypeVarTuple[object] = InvariantTypeVarTuple[int]() # E
14+
in_out_int: InvariantTypeVarTuple[int] = InvariantTypeVarTuple[object]() # E
15+
in_out_int = InvariantTypeVarTuple[int]() # E
16+
17+
1018
class ContravariantTypeVarTuple[*InTs]:
1119
def f(self, t: tuple[*InTs]):
1220
raise NotImplementedError
1321

14-
in_obj: ContravariantTypeVarTuple[object] = ContravariantTypeVarTuple[int]() # E
22+
in_obj: ContravariantTypeVarTuple[object, object] = ContravariantTypeVarTuple[int]() # E
1523
in_int: ContravariantTypeVarTuple[int] = ContravariantTypeVarTuple[object]() # OK
1624

1725

@@ -22,6 +30,10 @@ def f(self) -> tuple[*OutTs]:
2230

2331
out_int: CovariantTypeVarTuple[int] = CovariantTypeVarTuple[object]() # E
2432
out_obj: CovariantTypeVarTuple[object] = CovariantTypeVarTuple[int]() # OK
33+
out_multiple: CovariantTypeVarTuple[float, float] = CovariantTypeVarTuple[
34+
int, # OK
35+
object, # E
36+
]()
2537

2638

2739
InTs = TypeVarTuple("InTs", contravariant=True)

docs/spec/generics.rst

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -678,8 +678,9 @@ Prior to 3.12, the ``ParamSpec`` constructor can be used.
678678
P = ParamSpec("WrongName") # Rejected because P =/= WrongName
679679
680680
The runtime should accept ``bound``\ s and ``covariant`` and ``contravariant``
681-
arguments in the declaration just as ``typing.TypeVar`` does, but for ``bound`` we
682-
will defer the standardization of the semantics of this option to a later PEP.
681+
arguments in the declaration just as ``typing.TypeVar`` does.
682+
683+
We defer the standardization of the semantics of the ``bound`` option to a later PEP.
683684

684685
.. _`paramspec_valid_use_locations`:
685686

@@ -2712,8 +2713,8 @@ The algorithm for computing the variance of a type parameter is as follows.
27122713
For each type parameter in a generic class:
27132714

27142715
1. If the type parameter comes from a traditional
2715-
``TypeVar``/``TypeVarTuple``/``ParamSpec`` declaration and is not specified
2716-
as ``infer_variance`` (see below), its variance is specified by the
2716+
``TypeVar``/``TypeVarTuple``/``ParamSpec`` declaration and is not constructed
2717+
with ``infer_variance=True`` (see below), its variance is specified by the
27172718
constructor call. No further inference is needed.
27182719

27192720
2. Create two specialized versions of the class. We'll refer to these as
@@ -2725,7 +2726,8 @@ specialize the target type parameter with:
27252726

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

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

0 commit comments

Comments
 (0)