kingledion
kingledion

Reputation: 2500

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?

Upvotes: 57

Views: 18499

Answers (1)

juanpa.arrivillaga
juanpa.arrivillaga

Reputation: 95908

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

Callable[[], str]

Upvotes: 87

Related Questions