Re: [Python-ideas] Syntax for allowing extra keys when unpacking a dict as keyword arguments

Hello ! I made *fief*, a small python package allowing just that using a decorator: from fief import filter_effective_parameters as fief @fief def func(a, b): # some implementation # and then, use it as you want to: func(**MY_BIG_CONFIG_DICT_WITH_MANY_WEIRD_KEYS) The code is quite simple. You may want to use it, with modifications (i didn't touch the code for months, maybe years ; it could probably be improved now). Link: https://github.com/aluriak/fief The code: def filter_effective_parameters(func): """Decorator that filter out parameters in kwargs that are not related to any formal parameter of the given function. """ @wraps(func) def wrapper(*args, **kwargs): formal_parameters = frozenset(signature(func).parameters.keys()) return func(*args, **{ arg: value for arg, value in kwargs.items() if arg in formal_parameters }) return wrapper Best regards, --lucas ----- Mail original -----

Viktor is looking for something at the call site, not the function definition site (which he might not control). I wrote calllib (horrible name, I know). It can do this, although I just noticed it needs updating for keyword-only params. But, here it is without keyword-only params:
I don't claim it's an efficient solution, since it inspects the callable every time to extract its params. But it will work for this use case. Or at least it will when I update it for keyword-only params. It works today without keyword-only params. https://pypi.org/project/calllib/ Eric On 4/12/2019 12:17 PM, Lucas Bourneuf wrote:

That is an interesting solution. In the case of a function from another library, you could apply the decorator as needed like. fief(func)(**{'a': 1, 'b': 2}) It looks a little strange, but I've seen stranger. On Friday, April 12, 2019 at 12:18:34 PM UTC-4, Lucas Bourneuf wrote:

On 4/12/2019 1:52 PM, Viktor Roytman wrote:
Indeed. That's not so bad, and I'll look into using fief. And this is definitely better (for various values of "better") than language syntax to achieve the same thing. Eric

Viktor is looking for something at the call site, not the function definition site (which he might not control). I wrote calllib (horrible name, I know). It can do this, although I just noticed it needs updating for keyword-only params. But, here it is without keyword-only params:
I don't claim it's an efficient solution, since it inspects the callable every time to extract its params. But it will work for this use case. Or at least it will when I update it for keyword-only params. It works today without keyword-only params. https://pypi.org/project/calllib/ Eric On 4/12/2019 12:17 PM, Lucas Bourneuf wrote:

That is an interesting solution. In the case of a function from another library, you could apply the decorator as needed like. fief(func)(**{'a': 1, 'b': 2}) It looks a little strange, but I've seen stranger. On Friday, April 12, 2019 at 12:18:34 PM UTC-4, Lucas Bourneuf wrote:

On 4/12/2019 1:52 PM, Viktor Roytman wrote:
Indeed. That's not so bad, and I'll look into using fief. And this is definitely better (for various values of "better") than language syntax to achieve the same thing. Eric
participants (3)
-
Eric V. Smith
-
Lucas Bourneuf
-
Viktor Roytman