On Feb 9, 2020, at 11:49, Andrew Barnert <abarn...@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
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/DM4XOWIGBUES6JGCQBFG5QQNVN4TNBMG/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to