-
Notifications
You must be signed in to change notification settings - Fork 250
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Callable with variadic fixed arguments that doesn't mean *args #1301
Comments
TypeVarTuple can be used to solve this. What you want is, Ts = TypeVarTuple("Ts")
C = TypeVar("C", bound=Command)
Callable[[C, *Ts], None] 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 ( |
@drecdroid, PEP 612 appears to indicate that the last argument to I see that the latest Python 3.11 documentation for |
@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. |
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. |
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
What would be the definition of CommandHandler?
Ideally it would be:
But this syntax is not possible.
I've found in the documentation that Concatenate could receive
...
at the end,so it could be:
But this returns an error from Pylance:
One could say Callback Protocol could solve this like:
But it only receives functions that has an implicit variadic argument, it doesn't work with variadic forms of the function.
So far the only way I could solve this is through the Union of many Callables, but it doesn't look like the best solution.
At the end
CommandHandler
can only be defined as:Related:
python/cpython#88954
https://stackoverflow.com/questions/57658879/python-type-hint-for-callable-with-variable-number-of-str-same-type-arguments
The text was updated successfully, but these errors were encountered: