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

Brice Parent contact at brice.xyz
Sat Apr 13 03:38:04 EDT 2019


Le 12/4/19 à 18:02, Viktor Roytman a écrit :
> That is certainly an option for functions that you have defined for 
> yourself, but generally not an option for a function from a library. I 
> am interested in a solution that works in general.

This seems like a very specific problem. And in that case, I'm not sure 
that the solution should come from the language, but there definitely 
are ways to do it using an intermediary calling function.

That calling function would just do one of the following things:

* inspect the signature of the called function and only pass the 
arguments it needs

* contain a lookup table for which arguments are required by which 
function and use it to only pass the ones it needs

* or probably what I would do:

def call_func(func_name, **kwargs):
     try:
         return globals()[func_name](**kwargs)  # Not sure how you're 
doing it
     except TypeError as e:
         stripped_arg = e[len(func_name) + 39:-1]  # There's probably a 
more elegant way of doing it
         # Maybe warn the user that he's using an API that is under 
depreciation process, so that the authors may know they should add 
**kwargs to its signature
         kwargs = del kwargs[stripped_arg]
         return call_func(func_name, **kwargs)  # Try to make the call again

(I haven't tried the thing, it's just an idea of what I'd probably do in 
your situation)


>
> On Friday, April 12, 2019 at 11:48:38 AM UTC-4, Chris Angelico wrote:
>
>     On Sat, Apr 13, 2019 at 1:12 AM Viktor Roytman
>     <viktor... at gmail.com <javascript:>> wrote:
>     >
>     > Currently, unpacking a dict in order to pass its items as
>     keyword arguments to a function will fail if there are keys
>     present in the dict that are invalid keyword arguments:
>     >
>     >     >>> def func(*, a):
>     >     ...     pass
>     >     ...
>     >     >>> func(**{'a': 1, 'b': 2})
>     >     Traceback (most recent call last):
>     >       File "<stdin>", line 1, in <module>
>     >     TypeError: func() got an unexpected keyword argument 'b'
>     >
>     > The standard approach I have encountered in this scenario is to
>     pass in the keyword arguments explicitly like so
>     >
>     >     func(
>     >         a=kwargs_dict["a"],
>     >         b=kwargs_dict["b"],
>     >         c=kwargs_dict["c"],
>     >     )
>     >
>     > But this grows more cumbersome as the number of keyword
>     arguments grows.
>     >
>     > There are a number of other workarounds, such as using a dict
>     comprehension to select only the required keys, but I think it
>     would be more convenient to have this be a feature of the
>     language. I don't know what a nice syntax for this would be, or
>     even how feasible it is.
>     >
>
>     I'm not 100% sure I understand your proposal, so I'm going to restate
>     it; anywhere that I'm misrepresenting you, please clarify!
>
>     Given this function and this dictionary:
>
>     def func(*, a):
>         pass
>
>     args = {'a': 1, 'b': 2}
>
>     you want to call the function, passing the recognized argument 'a'
>     the
>     value from the dict, but ignoring the superfluous 'b'.
>
>     Are you able to alter the function? If so, just add kwargs to it:
>
>     def func(*, a, **_):
>         pass
>
>     and then any unrecognized args will quietly land in the junk
>     dictionary.
>
>     ChrisA
>     _______________________________________________
>     Python-ideas mailing list
>     Python... at python.org <javascript:>
>     https://mail.python.org/mailman/listinfo/python-ideas
>     <https://mail.python.org/mailman/listinfo/python-ideas>
>     Code of Conduct: http://python.org/psf/codeofconduct/
>     <http://python.org/psf/codeofconduct/>
>
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20190413/0eda9694/attachment-0001.html>


More information about the Python-ideas mailing list