| [case testBasicParamSpec] |
| from typing_extensions import ParamSpec |
| P = ParamSpec('P') |
| [builtins fixtures/tuple.pyi] |
| |
| [case testParamSpecLocations] |
| from typing import Callable, List |
| from typing_extensions import ParamSpec, Concatenate |
| P = ParamSpec('P') |
| |
| x: P # E: ParamSpec "P" is unbound |
| |
| def foo1(x: Callable[P, int]) -> Callable[P, str]: ... |
| |
| def foo2(x: P) -> P: ... # E: Invalid location for ParamSpec "P" \ |
| # N: You can use ParamSpec as the first argument to Callable, e.g., 'Callable[P, int]' |
| |
| # TODO(PEP612): uncomment once we have support for Concatenate |
| # def foo3(x: Concatenate[int, P]) -> int: ... $ E: Invalid location for Concatenate |
| |
| def foo4(x: List[P]) -> None: ... # E: Invalid location for ParamSpec "P" \ |
| # N: You can use ParamSpec as the first argument to Callable, e.g., 'Callable[P, int]' |
| |
| def foo5(x: Callable[[int, str], P]) -> None: ... # E: Invalid location for ParamSpec "P" \ |
| # N: You can use ParamSpec as the first argument to Callable, e.g., 'Callable[P, int]' |
| |
| def foo6(x: Callable[[P], int]) -> None: ... # E: Invalid location for ParamSpec "P" \ |
| # N: You can use ParamSpec as the first argument to Callable, e.g., 'Callable[P, int]' |
| [builtins fixtures/tuple.pyi] |
| |
| [case testParamSpecTemporaryAnyBehaviour] |
| # TODO(PEP612): behaviour tested here should change |
| # This is a test of mypy's temporary behaviour in lieu of full support for ParamSpec |
| from typing import Callable, List, Iterator, TypeVar |
| from typing_extensions import ParamSpec |
| P = ParamSpec('P') |
| T = TypeVar('T') |
| |
| def changes_return_type_to_str(x: Callable[P, int]) -> Callable[P, str]: ... |
| |
| def returns_int(a: str, b: bool) -> int: ... |
| |
| reveal_type(changes_return_type_to_str(returns_int)) # N: Revealed type is "def (*Any, **Any) -> builtins.str" |
| |
| def tmpcontextmanagerlike(x: Callable[P, Iterator[T]]) -> Callable[P, List[T]]: ... |
| |
| @tmpcontextmanagerlike |
| def whatever(x: int) -> Iterator[int]: |
| yield x |
| |
| reveal_type(whatever) # N: Revealed type is "def (*Any, **Any) -> builtins.list[builtins.int*]" |
| reveal_type(whatever(217)) # N: Revealed type is "builtins.list[builtins.int*]" |
| [builtins fixtures/tuple.pyi] |
| |
| [case testGenericParamSpecTemporaryBehaviour] |
| # flags: --python-version 3.10 |
| # TODO(PEP612): behaviour tested here should change |
| from typing import Generic, TypeVar, Callable, ParamSpec |
| |
| T = TypeVar("T") |
| P = ParamSpec("P") |
| |
| class X(Generic[T, P]): # E: Free type variable expected in Generic[...] |
| f: Callable[P, int] # E: The first argument to Callable must be a list of types or "..." |
| x: T |
| [out] |
| |
| [case testInvalidParamSpecType] |
| # flags: --python-version 3.10 |
| from typing import ParamSpec |
| |
| P = ParamSpec("P") |
| |
| class MyFunction(P): # E: Invalid location for ParamSpec "P" \ |
| # N: You can use ParamSpec as the first argument to Callable, e.g., 'Callable[P, int]' |
| ... |
| |
| a: MyFunction[int] # E: "MyFunction" expects no type arguments, but 1 given |