Quote from Python docs for Concatenate
:
The last parameter to Concatenate must be a ParamSpec or ellipsis (...).
I know what ParamSpec
is, but the ellipsis here drives me mad. It is not accepted by mypy
:
from typing import Callable, ParamSpec, Concatenate, TypeVar, Generic
_P = ParamSpec('_P')
_T = TypeVar('_T')
class Test(Generic[_P, _T]):
fn: Callable[Concatenate[_P, ...], _T]
E: Unexpected "..." [misc]
E: The last parameter to Concatenate needs to be a ParamSpec [valid-type]
and is not explained anywhere in docs. PEP612 doesn't mention it. Is it just a mistake, appeared as a result of mixing Callable
and Concatenate
together?
This issue is somewhat related and shows syntax with ellipsis literal in Concatenate
:
The specification should be extended to allow either
Concatenate[int, str, ...]
, or[int, str, ...]
, or some other syntax.
But this clearly targets "future syntax".
Note: I'm aware of meaning of ellipsis as Callable
argument, this question is specifically about Concatenate
.
According to PEP-612's grammar, the ellipsis is not permitted in the Concatenate
expression:
We now augment that with two new options: a parameter specification variable (
Callable[P, int]
) or a concatenation on a parameter specification variable (Callable[Concatenate[int, P], int]
).
callable ::= Callable "[" parameters_expression, type_expression "]"
parameters_expression ::=
| "..."
| "[" [ type_expression ("," type_expression)* ] "]"
| parameter_specification_variable
| concatenate "["
type_expression ("," type_expression)* ","
parameter_specification_variable
"]"
where
parameter_specification_variable
is atyping.ParamSpec
variable, declared in the manner as defined above, andconcatenate
istyping.Concatenate
.
However, the support for ellipsis as the last argument for Concatenate was introduced in April 2022 as part of Python 3.11.
No type checker seems to handle this new case though.