Skip to content

Commit 7af46ce

Browse files
authored
Forbid extra ParamSpec arguments (#12024)
* Forbid extra `ParamSpec` arguments * Change error message
1 parent e266cdf commit 7af46ce

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

mypy/semanal.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3292,6 +3292,15 @@ def process_paramspec_declaration(self, s: AssignmentStmt) -> bool:
32923292
if not self.check_typevarlike_name(call, name, s):
32933293
return False
32943294

3295+
# ParamSpec is different from a regular TypeVar:
3296+
# arguments are not semantically valid. But, allowed in runtime.
3297+
# So, we need to warn users about possible invalid usage.
3298+
if len(call.args) > 1:
3299+
self.fail(
3300+
"Only the first argument to ParamSpec has defined semantics",
3301+
s,
3302+
)
3303+
32953304
# PEP 612 reserves the right to define bound, covariant and contravariant arguments to
32963305
# ParamSpec in a later PEP. If and when that happens, we should do something
32973306
# on the lines of process_typevar_parameters

test-data/unit/check-parameter-specification.test

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@ from typing_extensions import ParamSpec
33
P = ParamSpec('P')
44
[builtins fixtures/tuple.pyi]
55

6+
[case testInvalidParamSpecDefinitions]
7+
from typing import ParamSpec
8+
9+
P1 = ParamSpec("P1", covariant=True) # E: Only the first argument to ParamSpec has defined semantics
10+
P2 = ParamSpec("P2", contravariant=True) # E: Only the first argument to ParamSpec has defined semantics
11+
P3 = ParamSpec("P3", bound=int) # E: Only the first argument to ParamSpec has defined semantics
12+
P4 = ParamSpec("P4", int, str) # E: Only the first argument to ParamSpec has defined semantics
13+
P5 = ParamSpec("P5", covariant=True, bound=int) # E: Only the first argument to ParamSpec has defined semantics
14+
[builtins fixtures/tuple.pyi]
15+
616
[case testParamSpecLocations]
717
from typing import Callable, List
818
from typing_extensions import ParamSpec, Concatenate

0 commit comments

Comments
 (0)