On Feb 9, 2020, at 11:49, Andrew Barnert <abarnert@yahoo.com> wrote:
If there isn’t a library that puts it all together the way you want
I figured there probably was, so I decided to take a look. I got a whole page of possibly useful things, and the first one I looked at, typical, shows this as its first example: @typic.al def multi(a: int, b: int): return a + b … which does exactly what you want here. Meanwhile, here’s something I slapped together in however long it was from my last email to a couple minutes before this one, to show how you could get started in case none of the many PyPI libraries is exactly what you want: def implicit_cast(f): sig = inspect.signature(f) def cast(param, arg): typ = sig.parameters[param].annotation if typ is sig.empty: return arg if isinstance(typ, str): typ = eval(typ) return typ(arg) @functools.wraps(f) def wrapper(*args, **kw): bound = sig.bind(*args, **kw) bound.apply_defaults() castargs = {param: cast(param, arg) for param, arg in bound.arguments.items()} return f(**castargs) return wrapper @implicit_cast def f(x: int, y=2, *, z) -> int: return x+y+z