Skip to content

Commit 825f144

Browse files
committed
specify that parameter specification should have variance
1 parent 9ba065d commit 825f144

File tree

8 files changed

+193
-6
lines changed

8 files changed

+193
-6
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
conformant = "Unsupported"
2+
conformance_automated = "Fail"
3+
errors_diff = """
4+
Line 21: Expected 1 errors
5+
Line 29: Expected 1 errors
6+
Line 42: Expected 1 errors
7+
Line 22: Unexpected errors ['generics_paramspec_variance.py:22: error: Incompatible types in assignment (expression has type "CovariantParamSpec[[int]]", variable has type "CovariantParamSpec[[object]]") [assignment]']
8+
Line 24: Unexpected errors ['generics_paramspec_variance.py:24: error: The variance and bound arguments to ParamSpec do not have defined semantics yet [misc]']
9+
Line 28: Unexpected errors ['generics_paramspec_variance.py:28: error: Missing return statement [empty-body]']
10+
Line 34: Unexpected errors ['generics_paramspec_variance.py:34: error: The variance and bound arguments to ParamSpec do not have defined semantics yet [misc]']
11+
Line 43: Unexpected errors ['generics_paramspec_variance.py:43: error: Incompatible types in assignment (expression has type "CovariantParamSpecOld[[int]]", variable has type "CovariantParamSpecOld[[object]]") [assignment]']
12+
"""
13+
output = """
14+
generics_paramspec_variance.py:13: error: Incompatible types in assignment (expression has type "ContravariantParamSpec[[int]]", variable has type "ContravariantParamSpec[[object]]") [assignment]
15+
generics_paramspec_variance.py:22: error: Incompatible types in assignment (expression has type "CovariantParamSpec[[int]]", variable has type "CovariantParamSpec[[object]]") [assignment]
16+
generics_paramspec_variance.py:24: error: The variance and bound arguments to ParamSpec do not have defined semantics yet [misc]
17+
generics_paramspec_variance.py:28: error: Missing return statement [empty-body]
18+
generics_paramspec_variance.py:31: error: Incompatible types in assignment (expression has type "ContravariantParamSpecOld[[int]]", variable has type "ContravariantParamSpecOld[[object]]") [assignment]
19+
generics_paramspec_variance.py:34: error: The variance and bound arguments to ParamSpec do not have defined semantics yet [misc]
20+
generics_paramspec_variance.py:38: error: Missing return statement [empty-body]
21+
generics_paramspec_variance.py:43: error: Incompatible types in assignment (expression has type "CovariantParamSpecOld[[int]]", variable has type "CovariantParamSpecOld[[object]]") [assignment]
22+
"""
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
conformant = "Unsupported"
2+
conformance_automated = "Fail"
3+
errors_diff = """
4+
Line 13: Expected 1 errors
5+
Line 21: Expected 1 errors
6+
Line 29: Expected 1 errors
7+
Line 38: Expected 1 errors
8+
Line 14: Unexpected errors ['`ContravariantParamSpec[[object]]` is not assignable to `ContravariantParamSpec[[int]]` [bad-assignment]']
9+
Line 22: Unexpected errors ['`CovariantParamSpec[[int]]` is not assignable to `CovariantParamSpec[[object]]` [bad-assignment]']
10+
Line 24: Unexpected errors ['Unexpected keyword argument `contravariant` to ParamSpec [invalid-param-spec]']
11+
Line 32: Unexpected errors ['`ContravariantParamSpecOld[[object]]` is not assignable to `ContravariantParamSpecOld[[int]]` [bad-assignment]']
12+
Line 34: Unexpected errors ['Unexpected keyword argument `covariant` to ParamSpec [invalid-param-spec]']
13+
Line 43: Unexpected errors ['`CovariantParamSpecOld[[int]]` is not assignable to `CovariantParamSpecOld[[object]]` [bad-assignment]']
14+
"""
15+
output = """
16+
ERROR generics_paramspec_variance.py:14:39-71: `ContravariantParamSpec[[object]]` is not assignable to `ContravariantParamSpec[[int]]` [bad-assignment]
17+
ERROR generics_paramspec_variance.py:22:39-64: `CovariantParamSpec[[int]]` is not assignable to `CovariantParamSpec[[object]]` [bad-assignment]
18+
ERROR generics_paramspec_variance.py:24:24-42: Unexpected keyword argument `contravariant` to ParamSpec [invalid-param-spec]
19+
ERROR generics_paramspec_variance.py:31:49-81: `ContravariantParamSpecOld[[int]]` is not assignable to `ContravariantParamSpecOld[[object]]` [bad-assignment]
20+
ERROR generics_paramspec_variance.py:32:46-81: `ContravariantParamSpecOld[[object]]` is not assignable to `ContravariantParamSpecOld[[int]]` [bad-assignment]
21+
ERROR generics_paramspec_variance.py:34:26-40: Unexpected keyword argument `covariant` to ParamSpec [invalid-param-spec]
22+
ERROR generics_paramspec_variance.py:42:43-74: `CovariantParamSpecOld[[object]]` is not assignable to `CovariantParamSpecOld[[int]]` [bad-assignment]
23+
ERROR generics_paramspec_variance.py:43:46-74: `CovariantParamSpecOld[[int]]` is not assignable to `CovariantParamSpecOld[[object]]` [bad-assignment]
24+
"""
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
conformant = "Unsupported"
2+
conformance_automated = "Fail"
3+
errors_diff = """
4+
Line 29: Expected 1 errors
5+
Line 38: Expected 1 errors
6+
Line 14: Unexpected errors ['generics_paramspec_variance.py:14:39 - error: Type "ContravariantParamSpec[(object)]" is not assignable to declared type "ContravariantParamSpec[(int)]"']
7+
Line 22: Unexpected errors ['generics_paramspec_variance.py:22:39 - error: Type "CovariantParamSpec[(int)]" is not assignable to declared type "CovariantParamSpec[(object)]"']
8+
Line 24: Unexpected errors ['generics_paramspec_variance.py:24:24 - error: "contravariant" is unknown parameter to ParamSpec (reportGeneralTypeIssues)']
9+
Line 32: Unexpected errors ['generics_paramspec_variance.py:32:46 - error: Type "ContravariantParamSpecOld[(object)]" is not assignable to declared type "ContravariantParamSpecOld[(int)]"']
10+
Line 34: Unexpected errors ['generics_paramspec_variance.py:34:26 - error: "covariant" is unknown parameter to ParamSpec (reportGeneralTypeIssues)']
11+
Line 43: Unexpected errors ['generics_paramspec_variance.py:43:46 - error: Type "CovariantParamSpecOld[(int)]" is not assignable to declared type "CovariantParamSpecOld[(object)]"']
12+
"""
13+
output = """
14+
generics_paramspec_variance.py:13:42 - error: Type "ContravariantParamSpec[(int)]" is not assignable to declared type "ContravariantParamSpec[(object)]"
15+
  "ContravariantParamSpec[(int)]" is not assignable to "ContravariantParamSpec[(object)]"
16+
    Type parameter "InP@ContravariantParamSpec" is invariant, but "(int)" is not the same as "(object)" (reportAssignmentType)
17+
generics_paramspec_variance.py:14:39 - error: Type "ContravariantParamSpec[(object)]" is not assignable to declared type "ContravariantParamSpec[(int)]"
18+
  "ContravariantParamSpec[(object)]" is not assignable to "ContravariantParamSpec[(int)]"
19+
    Type parameter "InP@ContravariantParamSpec" is invariant, but "(object)" is not the same as "(int)" (reportAssignmentType)
20+
generics_paramspec_variance.py:21:36 - error: Type "CovariantParamSpec[(object)]" is not assignable to declared type "CovariantParamSpec[(int)]"
21+
  "CovariantParamSpec[(object)]" is not assignable to "CovariantParamSpec[(int)]"
22+
    Type parameter "OutP@CovariantParamSpec" is invariant, but "(object)" is not the same as "(int)" (reportAssignmentType)
23+
generics_paramspec_variance.py:22:39 - error: Type "CovariantParamSpec[(int)]" is not assignable to declared type "CovariantParamSpec[(object)]"
24+
  "CovariantParamSpec[(int)]" is not assignable to "CovariantParamSpec[(object)]"
25+
    Type parameter "OutP@CovariantParamSpec" is invariant, but "(int)" is not the same as "(object)" (reportAssignmentType)
26+
generics_paramspec_variance.py:24:24 - error: "contravariant" is unknown parameter to ParamSpec (reportGeneralTypeIssues)
27+
generics_paramspec_variance.py:31:49 - error: Type "ContravariantParamSpecOld[(int)]" is not assignable to declared type "ContravariantParamSpecOld[(object)]"
28+
  "ContravariantParamSpecOld[(int)]" is not assignable to "ContravariantParamSpecOld[(object)]"
29+
    Type parameter "InP@ContravariantParamSpecOld" is invariant, but "(int)" is not the same as "(object)" (reportAssignmentType)
30+
generics_paramspec_variance.py:32:46 - error: Type "ContravariantParamSpecOld[(object)]" is not assignable to declared type "ContravariantParamSpecOld[(int)]"
31+
  "ContravariantParamSpecOld[(object)]" is not assignable to "ContravariantParamSpecOld[(int)]"
32+
    Type parameter "InP@ContravariantParamSpecOld" is invariant, but "(object)" is not the same as "(int)" (reportAssignmentType)
33+
generics_paramspec_variance.py:34:26 - error: "covariant" is unknown parameter to ParamSpec (reportGeneralTypeIssues)
34+
generics_paramspec_variance.py:42:43 - error: Type "CovariantParamSpecOld[(object)]" is not assignable to declared type "CovariantParamSpecOld[(int)]"
35+
  "CovariantParamSpecOld[(object)]" is not assignable to "CovariantParamSpecOld[(int)]"
36+
    Type parameter "OutP@CovariantParamSpecOld" is invariant, but "(object)" is not the same as "(int)" (reportAssignmentType)
37+
generics_paramspec_variance.py:43:46 - error: Type "CovariantParamSpecOld[(int)]" is not assignable to declared type "CovariantParamSpecOld[(object)]"
38+
  "CovariantParamSpecOld[(int)]" is not assignable to "CovariantParamSpecOld[(object)]"
39+
    Type parameter "OutP@CovariantParamSpecOld" is invariant, but "(int)" is not the same as "(object)" (reportAssignmentType)
40+
"""

conformance/results/results.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,13 @@ <h3>Python Type System Conformance Test Results</h3>
335335
<th class="column col2 conformant">Pass</th>
336336
<th class="column col2 conformant">Pass</th>
337337
</tr>
338+
<tr><th class="column col1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;generics_paramspec_variance</th>
339+
<th class="column col2 not-conformant">Unsupported</th>
340+
<th class="column col2 not-conformant">Unsupported</th>
341+
<th class="column col2 not-conformant">Unsupported</th>
342+
<th class="column col2 not-conformant">Unsupported</th>
343+
<th class="column col2 not-conformant">Unsupported</th>
344+
</tr>
338345
<tr><th class="column col1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;generics_scoping</th>
339346
<th class="column col2 conformant">Pass</th>
340347
<th class="column col2 conformant">Pass</th>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
conformant = "Unsupported"
2+
conformance_automated = "Fail"
3+
errors_diff = """
4+
Line 13: Expected 1 errors
5+
Line 21: Expected 1 errors
6+
Line 31: Expected 1 errors
7+
Line 42: Expected 1 errors
8+
Line 24: Unexpected errors ['generics_paramspec_variance.py:24:7: error[invalid-paramspec] The variance and bound arguments for `ParamSpec` do not have defined semantics yet']
9+
Line 27: Unexpected errors ['generics_paramspec_variance.py:27:33: error[invalid-argument-type] `ParamSpec` is not a valid argument to `Generic`']
10+
Line 28: Unexpected errors ['generics_paramspec_variance.py:28:23: error[empty-body] Function always implicitly returns `None`, which is not assignable to return type `(...) -> Unknown`', 'generics_paramspec_variance.py:28:32: error[invalid-type-form] Variable of type `ParamSpec` is not allowed in a return type annotation', 'generics_paramspec_variance.py:28:32: error[invalid-type-form] The first argument to `Callable` must be either a list of types, ParamSpec, Concatenate, or `...`']
11+
Line 34: Unexpected errors ['generics_paramspec_variance.py:34:8: error[invalid-paramspec] The variance and bound arguments for `ParamSpec` do not have defined semantics yet']
12+
Line 37: Unexpected errors ['generics_paramspec_variance.py:37:29: error[invalid-argument-type] `ParamSpec` is not a valid argument to `Generic`']
13+
Line 39: Unexpected errors ['generics_paramspec_variance.py:39:34: error[invalid-type-form] Variable of type `ParamSpec` is not allowed in a parameter annotation', 'generics_paramspec_variance.py:39:34: error[invalid-type-form] The first argument to `Callable` must be either a list of types, ParamSpec, Concatenate, or `...`']
14+
"""
15+
output = """
16+
generics_paramspec_variance.py:24:7: error[invalid-paramspec] The variance and bound arguments for `ParamSpec` do not have defined semantics yet
17+
generics_paramspec_variance.py:27:33: error[invalid-argument-type] `ParamSpec` is not a valid argument to `Generic`
18+
generics_paramspec_variance.py:28:23: error[empty-body] Function always implicitly returns `None`, which is not assignable to return type `(...) -> Unknown`
19+
generics_paramspec_variance.py:28:32: error[invalid-type-form] Variable of type `ParamSpec` is not allowed in a return type annotation
20+
generics_paramspec_variance.py:28:32: error[invalid-type-form] The first argument to `Callable` must be either a list of types, ParamSpec, Concatenate, or `...`
21+
generics_paramspec_variance.py:29:34: error[invalid-type-form] Variable of type `ParamSpec` is not allowed in a parameter annotation
22+
generics_paramspec_variance.py:29:34: error[invalid-type-form] The first argument to `Callable` must be either a list of types, ParamSpec, Concatenate, or `...`
23+
generics_paramspec_variance.py:34:8: error[invalid-paramspec] The variance and bound arguments for `ParamSpec` do not have defined semantics yet
24+
generics_paramspec_variance.py:37:29: error[invalid-argument-type] `ParamSpec` is not a valid argument to `Generic`
25+
generics_paramspec_variance.py:38:23: error[empty-body] Function always implicitly returns `None`, which is not assignable to return type `(...) -> Unknown`
26+
generics_paramspec_variance.py:38:32: error[invalid-type-form] Variable of type `ParamSpec` is not allowed in a return type annotation
27+
generics_paramspec_variance.py:38:32: error[invalid-type-form] The first argument to `Callable` must be either a list of types, ParamSpec, Concatenate, or `...`
28+
generics_paramspec_variance.py:39:34: error[invalid-type-form] Variable of type `ParamSpec` is not allowed in a parameter annotation
29+
generics_paramspec_variance.py:39:34: error[invalid-type-form] The first argument to `Callable` must be either a list of types, ParamSpec, Concatenate, or `...`
30+
"""
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
conformant = "Unsupported"
2+
conformance_automated = "Fail"
3+
errors_diff = """
4+
Line 21: Expected 1 errors
5+
Line 29: Expected 1 errors
6+
Line 42: Expected 1 errors
7+
Line 22: Unexpected errors ['generics_paramspec_variance.py:22: error: Incompatible types in assignment (expression has type "CovariantParamSpec[[int]]", variable has type "CovariantParamSpec[[object]]") [assignment]']
8+
Line 24: Unexpected errors ['generics_paramspec_variance.py:24: error: The variance and bound arguments to ParamSpec do not have defined semantics yet [misc]']
9+
Line 28: Unexpected errors ['generics_paramspec_variance.py:28: error: Missing return statement [empty-body]']
10+
Line 34: Unexpected errors ['generics_paramspec_variance.py:34: error: The variance and bound arguments to ParamSpec do not have defined semantics yet [misc]']
11+
Line 43: Unexpected errors ['generics_paramspec_variance.py:43: error: Incompatible types in assignment (expression has type "CovariantParamSpecOld[[int]]", variable has type "CovariantParamSpecOld[[object]]") [assignment]']
12+
"""
13+
output = """
14+
generics_paramspec_variance.py:13: error: Incompatible types in assignment (expression has type "ContravariantParamSpec[[int]]", variable has type "ContravariantParamSpec[[object]]") [assignment]
15+
generics_paramspec_variance.py:22: error: Incompatible types in assignment (expression has type "CovariantParamSpec[[int]]", variable has type "CovariantParamSpec[[object]]") [assignment]
16+
generics_paramspec_variance.py:24: error: The variance and bound arguments to ParamSpec do not have defined semantics yet [misc]
17+
generics_paramspec_variance.py:28: error: Missing return statement [empty-body]
18+
generics_paramspec_variance.py:31: error: Incompatible types in assignment (expression has type "ContravariantParamSpecOld[[int]]", variable has type "ContravariantParamSpecOld[[object]]") [assignment]
19+
generics_paramspec_variance.py:34: error: The variance and bound arguments to ParamSpec do not have defined semantics yet [misc]
20+
generics_paramspec_variance.py:38: error: Missing return statement [empty-body]
21+
generics_paramspec_variance.py:43: error: Incompatible types in assignment (expression has type "CovariantParamSpecOld[[int]]", variable has type "CovariantParamSpecOld[[object]]") [assignment]
22+
"""
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
"""
2+
Tests variance of ParamSpec.
3+
"""
4+
5+
# Specification: https://typing.readthedocs.io/en/latest/spec/generics.html#semantics
6+
7+
8+
from typing import Callable, Generic, ParamSpec
9+
10+
class ContravariantParamSpec[**InP]:
11+
def f(self, *args: InP.args, **kwargs: InP.kwargs): ...
12+
13+
in_obj: ContravariantParamSpec[object] = ContravariantParamSpec[int]() # E
14+
in_int: ContravariantParamSpec[int] = ContravariantParamSpec[object]() # OK
15+
16+
17+
class CovariantParamSpec[**OutP]:
18+
def f(self, fn: Callable[OutP, None]) -> None: ...
19+
20+
21+
out_int: CovariantParamSpec[int] = CovariantParamSpec[object]() # E
22+
out_obj: CovariantParamSpec[object] = CovariantParamSpec[int]() # OK
23+
24+
InP = ParamSpec("InP", contravariant=True)
25+
26+
27+
class ContravariantParamSpecOld(Generic[InP]):
28+
def in_f(self) -> Callable[InP, None]: ... # OK
29+
def out_f(self, fn: Callable[InP, None]) -> None: ... # E
30+
31+
in_obj_old: ContravariantParamSpecOld[object] = ContravariantParamSpecOld[int]() # E
32+
in_int_old: ContravariantParamSpecOld[int] = ContravariantParamSpecOld[object]() # OK
33+
34+
OutP = ParamSpec("OutP", covariant=True)
35+
36+
37+
class CovariantParamSpecOld(Generic[OutP]):
38+
def in_f(self) -> Callable[OutP, None]: ... # E
39+
def out_f(self, fn: Callable[OutP, None]) -> None: ... # OK
40+
41+
42+
out_int_old: CovariantParamSpecOld[int] = CovariantParamSpecOld[object]() # E
43+
out_obj_old: CovariantParamSpecOld[object] = CovariantParamSpecOld[int]() # OK

docs/spec/generics.rst

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2712,13 +2712,12 @@ The algorithm for computing the variance of a type parameter is as follows.
27122712

27132713
For each type parameter in a generic class:
27142714

2715-
1. If the type parameter is variadic (``TypeVarTuple``) or a parameter
2716-
specification (``ParamSpec``), it is always considered invariant. No further
2717-
inference is needed.
2715+
1. If the type parameter is variadic (``TypeVarTuple``) it is always
2716+
considered invariant. No further inference is needed.
27182717

2719-
2. If the type parameter comes from a traditional ``TypeVar`` declaration and
2720-
is not specified as ``infer_variance`` (see below), its variance is specified
2721-
by the ``TypeVar`` constructor call. No further inference is needed.
2718+
2. If the type parameter comes from a traditional ``TypeVar``/``ParamSpec``
2719+
declaration and is not specified as ``infer_variance`` (see below), its
2720+
variance is specified by the constructor call. No further inference is needed.
27222721

27232722
3. Create two specialized versions of the class. We'll refer to these as
27242723
``upper`` and ``lower`` specializations. In both of these specializations,

0 commit comments

Comments
 (0)