You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Callable with variadic fixed arguments that doesn't mean *args #1301
Suppose I have a type of function called CommandHandler, that is a function that receives a command as its first parameter but then could have other parameters.
Examples
class Command:
pass
class RegisterUser(Command):
pass
class DeleteUser(Command):
pass
def register_user(command: RegisterUser, db : Db) -> None:
...
def delete_user(command: DeleteUser, dispatch: EventDispatcher, cache: Cache) -> None:
...
mappings : dict[Command, CommandHandler] = {
RegisterUser: register_user,
DeleteUser: delete_user
}
I've found in the documentation that Concatenate could receive ... at the end,
Concatenate is currently only valid when used as the first argument to a Callable. The last parameter to Concatenate must be a ParamSpec or ellipsis (...).
Minor note, most of the time if you accept a callable with return None you probably don't care about return type and should let it be object (Callable[[C, *Ts], object])
@drecdroid, PEP 612 appears to indicate that the last argument to Concatenate must be a ParamSpec, not an ellipsis. The section titled valid use locations indicates that the last argument for Concatenate must be a "parameter_specification_variable". This explains why pyright (the type checker upon which pylance is built) considers an ellipsis an error here.
I see that the latest Python 3.11 documentation for Concatenate indicates that an ellipsis is allowed here as well. Either I'm misinterpreting the PEP, this aspect of Concatenate was modified after the PEP was written, or the 3.11 documentation is incorrect. I'm not sure which.
The Concatenate[P, ...] is an unresolved question in this issue.
The 3.11 typevartuple is not much of a restriction as you can use typing_extensions even on 3.7 and use typevartuple. You'll just need to use Unpack[Ts] instead of *Ts.
Activity
hmc-cs-mdrissi commentedon Dec 2, 2022
TypeVarTuple can be used to solve this. What you want is,
Minor note, most of the time if you accept a callable with return None you probably don't care about return type and should let it be object (
Callable[[C, *Ts], object])erictraut commentedon Dec 2, 2022
@drecdroid, PEP 612 appears to indicate that the last argument to
Concatenatemust be a ParamSpec, not an ellipsis. The section titled valid use locations indicates that the last argument for Concatenate must be a "parameter_specification_variable". This explains why pyright (the type checker upon which pylance is built) considers an ellipsis an error here.I see that the latest Python 3.11 documentation for
Concatenateindicates that an ellipsis is allowed here as well. Either I'm misinterpreting the PEP, this aspect ofConcatenatewas modified after the PEP was written, or the 3.11 documentation is incorrect. I'm not sure which.drecdroid commentedon Dec 2, 2022
@hmc-cs-mdrissi Thanks, good to know, although it appears to be a feature for 3.11.
@erictraut Yes I was confused for that also.
hmc-cs-mdrissi commentedon Dec 3, 2022
The Concatenate[P, ...] is an unresolved question in this issue.
The 3.11 typevartuple is not much of a restriction as you can use typing_extensions even on 3.7 and use typevartuple. You'll just need to use Unpack[Ts] instead of *Ts.