How do you express a Python Callable with no arguments?
Question:
In the docs for the Python typing
package, it says:
It is possible to declare the return type of a callable without specifying the call signature by substituting a literal ellipsis for the list of arguments in the type hint: Callable[..., ReturnType].
On the other hand, it also says:
Callable[..., ReturnType]
(literal ellipsis) can be used to type hint a callable taking any number of arguments and returning ReturnType.
I want to express a function that takes no arguments but returns a string. The ellipsis seems to indicate that there are some unspecified arguments. I want to express that that there definately are zero arguements.
Do I have any alternatives to using Callable[..., str]
in my type hint?
Answers:
It requires a sequence of argument types, so if there are no types, you pass it an empty sequence:
Callable[[], str]
In the docs for the Python typing
package, it says:
It is possible to declare the return type of a callable without specifying the call signature by substituting a literal ellipsis for the list of arguments in the type hint:
Callable[..., ReturnType].
On the other hand, it also says:
Callable[..., ReturnType]
(literal ellipsis) can be used to type hint a callable taking any number of arguments and returning ReturnType.
I want to express a function that takes no arguments but returns a string. The ellipsis seems to indicate that there are some unspecified arguments. I want to express that that there definately are zero arguements.
Do I have any alternatives to using Callable[..., str]
in my type hint?
It requires a sequence of argument types, so if there are no types, you pass it an empty sequence:
Callable[[], str]