← Back to index

generics_paramspec_basic.py

True Positive
False Positive
False Negative
Optional (detected)
Warning or Info
TP: 2
FP: 0
FN: 5
Optional: 0 / 0
1"""
2Tests basic usage of ParamSpec.
3"""
4
5# Specification: https://typing.readthedocs.io/en/latest/spec/generics.html#paramspec-variables
6
7from typing import Any, Callable, Concatenate, ParamSpec, TypeAlias
8
9P = ParamSpec("P") # OK
10WrongName = ParamSpec("NotIt") # E: name inconsistency
[invalid-paramspec] The name of a `ParamSpec` (`NotIt`) must match the name of the variable it is assigned to (`WrongName`)
13# > Valid use locations
15TA1: TypeAlias = P # E
Expected a ty diagnostic for this line
17TA2: TypeAlias = Callable[P, None] # OK
18TA3: TypeAlias = Callable[Concatenate[int, P], None] # OK
19TA4: TypeAlias = Callable[..., None] # OK
20TA5: TypeAlias = Callable[..., None] # OK
23def func1(x: P) -> P: # E
[empty-body] Function always implicitly returns `None`, which is not assignable to return type `P@func1`
24 ...
27def func2(x: Concatenate[int, P]) -> int: # E
Expected a ty diagnostic for this line
28 raise NotImplementedError
31def func3(x: list[P]) -> None: # E
Expected a ty diagnostic for this line
32 ...
35def func4(x: Callable[[int, str], P]) -> None: # E
Expected a ty diagnostic for this line
36 ...
39def func5(*args: P, **kwargs: P) -> None: # E
Expected a ty diagnostic for this line
40 ...