Toothpick Anemone
Toothpick Anemone

Reputation: 4644

Can an object have a `Callable` type hint without specifying input arguments and return value?

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 _

Upvotes: 3

Views: 1922

Answers (2)

Pétur Ingi Egilsson
Pétur Ingi Egilsson

Reputation: 4442

Yes.

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

Upvotes: 0

chepner
chepner

Reputation: 530990

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.

Upvotes: 4

Related Questions