Is there the way with `Callable` and `**` to accept `**kwargs: int` like `Callable` with `*tuple[int, ...]` to accept `*args: int`?

Callable with ... can accept **kwargs: int as shown below:

from collections.abc import Callable

def func(**kwargs: int) -> None:
    print(kwargs)

          # ↓↓↓
v: Callable[..., None] = func 

Now, is there the way with Callable and ** to accept **kwargs: int like Callable with *tuple[int, ...] to accept *args: int as shown below?

from collections.abc import Callable

def func(*args: int) -> None:
    print(args)

           # ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
v: Callable[[*tuple[int, ...]], None] = func

Callable can’t currently express complex signatures. The official workaround is to define a Protocol with a __call__ method instead:

from typing import Protocol

class CallableWithIntKwargs(Protocol):
    def __call__(self, **kwargs: int): ...

def func(**kwargs: int) -> None: pass

v: CallableWithIntKwargs = func
3 Likes