<div dir="ltr">Any time I am using a function from a library that accepts keyword arguments. For example, an ORM model constructor that accepts fields as keyword arguments (like Django).<br><br>On Friday, April 12, 2019 at 12:57:43 PM UTC-4, Rhodri James wrote:<blockquote class="gmail_quote" style="margin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;">On 12/04/2019 16:10, Viktor Roytman wrote:
<br>> Currently, unpacking a dict in order to pass its items as keyword arguments
<br>> to a function will fail if there are keys present in the dict that are
<br>> 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
<br>> 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
<br>> to select only the required keys, but I think it would be more convenient
<br>> to have this be a feature of the language. I don't know what a nice syntax
<br>> for this would be, or even how feasible it is.
<br>
<br>What circumstance do you want to do this in that simply passing the 
<br>dictionary as itself won't do for?
<br>
<br>-- 
<br>Rhodri James *-* Kynesim Ltd
<br>______________________________<wbr>_________________
<br>Python-ideas mailing list
<br><a href="javascript:" target="_blank" gdf-obfuscated-mailto="aMjIorT8AwAJ" 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>