简体   繁体   中英

Is literal ellipsis really valid as ParamSpec last argument?

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 a typing.ParamSpec variable, declared in the manner as defined above, and concatenate is typing.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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM