3

The following code...

import typing
func:typing.Callable[[int, float], str]

Annotates func as a callable accepting two inputs. The two inputs are an int and a float. It also indicates that the return value is a string.

Is it possible to type hint something as a callable without specifying input argument types or output type?

For example:

def decorator(f:Callable):
    def _(*args, **kwargs)
        r = f(*map(str, args), **kwargs)
        return r
    return _
Toothpick Anemone
  • 4,290
  • 2
  • 20
  • 42
  • What do you mean by "possible"? – juanpa.arrivillaga Nov 08 '19 at 01:13
  • An answer to your question can be found here: [How can I specify the function type in my type hints?](https://stackoverflow.com/questions/37835179/how-can-i-specify-the-function-type-in-my-type-hints) – Georgy Nov 08 '19 at 09:03

2 Answers2

4

The typing module explicitly states you can use ... in place of the signature, thought the return type seems mandatory.

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].

Callable[..., Any] would appear to be equivalent to your requested Callable annotation.

chepner
  • 497,756
  • 71
  • 530
  • 681
0

Yes.

a bare Callable in an annotation is equivalent to Callable[..., Any] -- PEP-0484

Pétur Ingi Egilsson
  • 4,368
  • 5
  • 44
  • 72