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

2019-04-13 Thread Chris Angelico
On Sun, Apr 14, 2019 at 10:15 AM Juancarlo Añez wrote: > > > > On Sat, Apr 13, 2019 at 9:02 AM Chris Angelico wrote: >> >> Would be really nice to be able to spell this as a dict/set intersection. >> >> func(**(d & {'a', 'b', 'c'})) > > > That would be _very_ consistent with the ongoing

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

2019-04-13 Thread Juancarlo Añez
On Sat, Apr 13, 2019 at 9:02 AM Chris Angelico wrote: > Would be really nice to be able to spell this as a dict/set intersection. > > func(**(d & {'a', 'b', 'c'})) > That would be _very_ consistent with the ongoing discussions about operators over dicts. -- Juancarlo *Añez*

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

2019-04-13 Thread Viktor Roytman
On Saturday, April 13, 2019 at 9:03:54 AM UTC-4, Chris Angelico wrote: > > On Sat, Apr 13, 2019 at 11:00 PM Juancarlo Añez > wrote: > > func(**{k:v for k, v in d.items() if k in ('a','b','c')) > > > > Would be really nice to be able to spell this as a dict/set intersection. > > func(**(d & {'a',

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

2019-04-13 Thread Chris Angelico
On Sat, Apr 13, 2019 at 11:00 PM Juancarlo Añez wrote: > func(**{k:v for k, v in d.items() if k in ('a','b','c')) > Would be really nice to be able to spell this as a dict/set intersection. func(**(d & {'a', 'b', 'c'})) ChrisA ___ Python-ideas

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

2019-04-13 Thread Juancarlo Añez
On Fri, Apr 12, 2019 at 11:10 AM Viktor Roytman wrote: > > 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"], > ) >

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

2019-04-12 Thread Amber Yust
I'm not a fan of this idea for two (related) reasons: 1) This seems like something that's a relatively niche case to *want* to do rather than doing unintentionally. 2) This vastly increases the chance of silent bugs in code. With regards to #1, the main examples I've seen posited in this thread

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

2019-04-12 Thread Greg Ewing
Bruce Leban wrote: I think this is a real problem given the frequent convention that you can freely add fields to json objects with the additional fields to be ignored. Unpacking json objects isn't something one does every day, so I don't think it would be worth adding syntax just for this.

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

2019-04-12 Thread Viktor Roytman
> > That seems to me to be quite different issue. Just throwing invalid stuff > on the ground in this scenario will avoid a crash but lose data. This seems > much worse to me than the crash. > Throwing it away does seem extreme. Maybe something that indicates what's left over? In other

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

2019-04-12 Thread Anders Hovmöller
> On 12 Apr 2019, at 19:16, Eric V. Smith wrote: > > I don't want to speak for the OP, but I have a similar use case (which is why > I wrote calllib). My use case is: I have number of callables that I don't > control. I also have a dict of parameters that the callables might take as >

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

2019-04-12 Thread Eric V. Smith
On 4/12/2019 1:52 PM, Viktor Roytman 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. Indeed. That's not so bad, and

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

2019-04-12 Thread Eric V. Smith
On 4/12/2019 11:29 AM, Rhodri James wrote: On 12/04/2019 16:10, Viktor Roytman 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): ... 

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

2019-04-12 Thread Nathaniel Smith
I don't think it's possible to make this work reliably. In particular, it's an important feature of python that you can make wrappers that pass through arguments and are equivalent to the original function: def original(a=0): ... def wrapper(*args, **kwargs): return original(*args,

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

2019-04-12 Thread Viktor Roytman
> > > 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). > > That's not the same issue at all, if I'm understanding you correctly. > In any case, surely you need to

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

2019-04-12 Thread Rhodri James
Re-ordered to avoid top-posting... On 12/04/2019 18:50, Viktor Roytman wrote: On Friday, April 12, 2019 at 12:57:43 PM UTC-4, Rhodri James wrote: On 12/04/2019 16:10, Viktor Roytman wrote: Currently, unpacking a dict in order to pass its items as keyword arguments to a function will fail

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

2019-04-12 Thread Eric V. Smith
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: >>> from

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

2019-04-12 Thread Viktor Roytman
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: > > Hello !

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

2019-04-12 Thread Viktor Roytman
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). On Friday, April 12, 2019 at 12:57:43 PM UTC-4, Rhodri James wrote: > > On 12/04/2019 16:10, Viktor Roytman wrote: > >

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

2019-04-12 Thread Rhodri James
On 12/04/2019 16:10, Viktor Roytman 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,

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

2019-04-12 Thread Lucas Bourneuf
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)

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

2019-04-12 Thread Viktor Roytman
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. On Friday, April 12, 2019 at 11:48:38 AM UTC-4, Chris Angelico wrote: > > On Sat, Apr 13, 2019 at 1:12 AM

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

2019-04-12 Thread Viktor Roytman
I could see this being an option, but to someone unfamiliar with it, it might seem strange that * unpacks iterables, ** unpacks dicts, and *** is a special thing only for keyword arguments that mostly behaves like **. On Friday, April 12, 2019 at 11:26:37 AM UTC-4, Bruce Leban wrote: > > > On

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

2019-04-12 Thread Chris Angelico
On Sat, Apr 13, 2019 at 1:12 AM Viktor Roytman 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 > ... >

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

2019-04-12 Thread Bruce Leban
On Fri, Apr 12, 2019, 8:12 AM Viktor Roytman > >>> func(**{'a': 1, 'b': 2}) > Traceback (most recent call last): > File "", line 1, in > TypeError: func() got an unexpected keyword argument 'b' > Perhaps func(***kws)? I think this is a real problem given the frequent

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

2019-04-12 Thread Viktor Roytman
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):