I want to create a generic for functions with keyword arguments, something like this:
# Not valid code
class Args(TypedDict):
a: str
b: int
foo: KWFunc[Args, dict] # Equivalent to (a: str, b: int) -> dict
As non-generic, this could be achieved via Protocol:
R = TypeVar('R')
class KWFunc(Protocol[R]):
def __call__(self, a: str, b: int) -> R:
pass
foo: KWFunc[dict] # Equivalent to (a: str, b: int) -> dict
Also, It is possible to create a generic for function with positional arguments:
R = TypeVar('R')
P = ParamSpec('P')
class KWFunc(Protocol[P, R]):
def __call__(self, *args: P.args, **kwargs: P.kwargs) -> R:
pass
foo: KWFunc[[str, int], dict] # Equivalent to (str, int) -> dict
Is there a way to define generic type for function with keyword arguments as parameter?