← Back to index

generics_paramspec_specialization.py

True Positive
False Positive
False Negative
Optional (detected)
Warning or Info
TP: 5
FP: 0
FN: 0
Optional: 0 / 0
1"""
2Tests the handling of a ParamSpec specialization.
3"""
4
5from typing import Any, Callable, Concatenate, Generic, ParamSpec, TypeVar, cast
6
7
8T = TypeVar("T")
9P1 = ParamSpec("P1")
10P2 = ParamSpec("P2")
13class ClassA(Generic[T, P1]):
14 f: Callable[P1, int] = cast(Any, "")
15 x: T = cast(Any, "")
18class ClassB(ClassA[T, P1], Generic[T, P1, P2]):
19 f1: Callable[P1, int] = cast(Any, "")
20 f2: Callable[P2, int] = cast(Any, "")
21 x: T = cast(Any, "")
24def func20(x: ClassA[int, P2]) -> str: # OK
25 return ""
28def func21(x: ClassA[int, Concatenate[int, P2]]) -> str: # OK
29 return ""
32def func22(x: ClassB[int, [int, bool], ...]) -> str: # OK
33 return ""
36def func23(x: ClassA[int, ...]) -> str: # OK
37 return ""
40def func24(x: ClassB[int, [], []]) -> str: # OK
41 return ""
44def func25(x: ClassA[int, int]) -> str: # E
[invalid-type-arguments] Type argument for `ParamSpec` must be either a list of types, `ParamSpec`, `Concatenate`, or `...`
45 return ""
48class ClassC(Generic[P1]):
49 f: Callable[P1, int] = cast(Any, "")
52def func30(x: ClassC[[int, str, bool]]) -> None: # OK
53 x.f(0, "", True) # OK
54 x.f("", "", True) # E
[invalid-argument-type] Argument is incorrect: Expected `int`, found `Literal[""]`
55 x.f(0, "", "") # E
[invalid-argument-type] Argument is incorrect: Expected `bool`, found `Literal[""]`
58def func31(x: ClassC[int, str, bool]) -> None: # OK
59 x.f(0, "", True) # OK
60 x.f("", "", True) # E
[invalid-argument-type] Argument is incorrect: Expected `int`, found `Literal[""]`
61 x.f(0, "", "") # E
[invalid-argument-type] Argument is incorrect: Expected `bool`, found `Literal[""]`