Skip to content
Advertisement

Python: What is the typing signature for print?

What is the typing signature for print or similar function which takes a variable number of arguments of any type, when defined as a Callable parameter?

Concretely, what is the declaration of output_function here?

JavaScript

Update: clarified as a Callable parameter.

Advertisement

Answer

From PEP 484

Arbitrary argument lists can as well be type annotated, so that the definition:

JavaScript

is acceptable and it means that, e.g., all of the following represent function calls with valid types of arguments:

JavaScript

So print would be:

JavaScript

I don’t think you can apply this to a Callable though. From the docs for typing,

There is no syntax to indicate optional or keyword arguments; such function types are rarely used as callback types. Callable[..., ReturnType] (literal ellipsis) can be used to type hint a callable taking any number of arguments and returning ReturnType

Advertisement