I heavily use python typing support from python 3.
Recently I was trying to pass a function as an argument and I do not find any help for using kwargs
in typing.Callable
signature.
Please check the code below and the comments.
import typing
# some function with a type signature
def fn1_as_arg_with_kwargs(a: int, b: float) -> float:
return a + b
# some function with a type signature
def fn2_as_arg_with_kwargs(a: int, b: float) -> float:
return a * b
# function that get callables as arg
# this works with typing
def function_executor(
a: int,
b: float,
fn: typing.Callable[[int, float], float]):
return fn(a, b)
# But what if I want to name my kwargs
# (something like below which does not work)
# ... this will help me more complex scenarios
# ... or am I expecting a lot from python3 ;)
def function_executor(
a: int,
b: float,
fn: typing.Callable[["a": int, "b": float], float]):
return fn(a=a, b=b)
**kwargs
, which simply collects the named args, i.e. it depends on how the function is called. Use other appropriate and specific types instead (e.g. NamedTuple, Dataclass, Enum, custom classes), that is to say explicitate each argument as usual