Lucas
Lucas

Reputation: 1948

How do I define a "Callable" type that corresponds to a function with variable arguments?

I like to make sure that I have accurate type hints, but how can I write the type of this function using typing.Callable?

def f(x: int, *args, **kwargs): 
    pass

e.g.

Callable[[int, ?????], None]

Upvotes: 2

Views: 1079

Answers (1)

KenHBS
KenHBS

Reputation: 7164

Paraphrasing from the documentation of type hints:

You can use Callable[[Arg1Type, Arg2Type], ReturnType] to specify the input arguments and the ReturnType.

If the types of the input arguments are not known, you could use the ellipsis notation instead: Callable[..., ReturnType].

Upvotes: 1

Related Questions