<div dir="ltr">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.<br><br>On Friday, April 12, 2019 at 11:48:38 AM UTC-4, Chris Angelico wrote:<blockquote class="gmail_quote" style="margin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;">On Sat, Apr 13, 2019 at 1:12 AM Viktor Roytman <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="5EA8g-_4AwAJ" rel="nofollow" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;">viktor...@gmail.com</a>> wrote:
<br>>
<br>> 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:
<br>>
<br>>     >>> def func(*, a):
<br>>     ...     pass
<br>>     ...
<br>>     >>> func(**{'a': 1, 'b': 2})
<br>>     Traceback (most recent call last):
<br>>       File "<stdin>", line 1, in <module>
<br>>     TypeError: func() got an unexpected keyword argument 'b'
<br>>
<br>> The standard approach I have encountered in this scenario is to pass in the keyword arguments explicitly like so
<br>>
<br>>     func(
<br>>         a=kwargs_dict["a"],
<br>>         b=kwargs_dict["b"],
<br>>         c=kwargs_dict["c"],
<br>>     )
<br>>
<br>> But this grows more cumbersome as the number of keyword arguments grows.
<br>>
<br>> 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.
<br>>
<br>
<br>I'm not 100% sure I understand your proposal, so I'm going to restate
<br>it; anywhere that I'm misrepresenting you, please clarify!
<br>
<br>Given this function and this dictionary:
<br>
<br>def func(*, a):
<br>    pass
<br>
<br>args = {'a': 1, 'b': 2}
<br>
<br>you want to call the function, passing the recognized argument 'a' the
<br>value from the dict, but ignoring the superfluous 'b'.
<br>
<br>Are you able to alter the function? If so, just add kwargs to it:
<br>
<br>def func(*, a, **_):
<br>    pass
<br>
<br>and then any unrecognized args will quietly land in the junk dictionary.
<br>
<br>ChrisA
<br>______________________________<wbr>_________________
<br>Python-ideas mailing list
<br><a href="javascript:" target="_blank" gdf-obfuscated-mailto="5EA8g-_4AwAJ" rel="nofollow" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;">Python...@python.org</a>
<br><a href="https://mail.python.org/mailman/listinfo/python-ideas" target="_blank" rel="nofollow" onmousedown="this.href='https://www.google.com/url?q\x3dhttps%3A%2F%2Fmail.python.org%2Fmailman%2Flistinfo%2Fpython-ideas\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFj1EaNHnVmh20FnFPoUi4J-MpfQw';return true;" onclick="this.href='https://www.google.com/url?q\x3dhttps%3A%2F%2Fmail.python.org%2Fmailman%2Flistinfo%2Fpython-ideas\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFj1EaNHnVmh20FnFPoUi4J-MpfQw';return true;">https://mail.python.org/<wbr>mailman/listinfo/python-ideas</a>
<br>Code of Conduct: <a href="http://python.org/psf/codeofconduct/" target="_blank" rel="nofollow" onmousedown="this.href='http://www.google.com/url?q\x3dhttp%3A%2F%2Fpython.org%2Fpsf%2Fcodeofconduct%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHJOrArSUDKkjrnthO6_CznMzkPsA';return true;" onclick="this.href='http://www.google.com/url?q\x3dhttp%3A%2F%2Fpython.org%2Fpsf%2Fcodeofconduct%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHJOrArSUDKkjrnthO6_CznMzkPsA';return true;">http://python.org/psf/<wbr>codeofconduct/</a>
<br></blockquote></div>