Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you express a Python Callable with no arguments?

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?

like image 226
kingledion Avatar asked Oct 09 '19 23:10

kingledion


People also ask

Can a Python function have no arguments?

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.

How do you type callable in Python?

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.

What is Type callable?

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.

What means callable Python?

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.


1 Answers

It requires a sequence of argument types, so if there are no types, you pass it an empty sequence:

Callable[[], str]
like image 144
juanpa.arrivillaga Avatar answered Oct 20 '22 14:10

juanpa.arrivillaga