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?
7.1. A function in Python is defined with the def keyword. Functions do not have declared return types. A function without an explicit return statement returns None . In the case of no arguments and no return value, the definition is very simple.
Callable type; Callable[[int], str] is a function of (int) -> str. The subscription syntax must always be used with exactly two values: the argument list and the return type. The argument list must be a list of types or an ellipsis; the return type must be a single type.
Callable is the type you use to indicate a callable. Most python types that support the () operator are of the type collections. abc. Callable . Examples include functions, classmethod s, staticmethod s, bound methods and lambdas.
callable() in Python In general, a callable is something that can be called. This built-in method in Python checks and returns True if the object passed appears to be callable, but may not be, otherwise False.
It requires a sequence of argument types, so if there are no types, you pass it an empty sequence:
Callable[[], str]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With