Skip to content

Commit eec8071

Browse files
Disallow bare ParamSpec in type aliases (#18174)
Emit errors for both of the following aliases: ```python P = ParamSpec("P") Bad1: TypeAlias = P Bad2: TypeAlias = Concatenate[int, P] ``` This is done by swapping a top-level call to `type.accept(analyzer)` with `analyzer.anal_type(type)`, the latter of which simply calls the former and then performs some checks.
1 parent 21587f0 commit eec8071

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

mypy/typeanal.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ def analyze_type_alias(
180180
)
181181
analyzer.in_dynamic_func = in_dynamic_func
182182
analyzer.global_scope = global_scope
183-
res = type.accept(analyzer)
183+
res = analyzer.anal_type(type, nested=False)
184184
return res, analyzer.aliases_used
185185

186186

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1452,6 +1452,17 @@ y: C[int, str]
14521452
reveal_type(y) # N: Revealed type is "def (builtins.int, builtins.int, builtins.str) -> builtins.int"
14531453
[builtins fixtures/paramspec.pyi]
14541454

1455+
[case testParamSpecInTypeAliasIllegalBare]
1456+
from typing import ParamSpec
1457+
from typing_extensions import Concatenate, TypeAlias
1458+
1459+
P = ParamSpec("P")
1460+
Bad1: TypeAlias = P # E: Invalid location for ParamSpec "P" \
1461+
# N: You can use ParamSpec as the first argument to Callable, e.g., "Callable[P, int]"
1462+
Bad2: TypeAlias = Concatenate[int, P] # E: Invalid location for Concatenate \
1463+
# N: You can use Concatenate as the first argument to Callable
1464+
[builtins fixtures/paramspec.pyi]
1465+
14551466
[case testParamSpecInTypeAliasRecursive]
14561467
from typing import ParamSpec, Callable, Union
14571468

0 commit comments

Comments
 (0)