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 discussio

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 mailing

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"], > ) > func(**

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 a

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. R

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 words

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 4:00 PM, Anders Hovmöller wrote: 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 tha

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 > param

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

2019-04-12 Thread Eric V. Smith
- > De: "Viktor Roytman" > > À: "python-ideas" > > Envoyé: Vendredi 12 Avril 2019 18:01:43 > Objet: Re: [Python-ideas] Syntax for allowing extra keys when unpacking a dict as keyword arguments > I could see this being an

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, **kwar

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 if

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

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

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

2019-04-12 Thread Viktor Roytman
return wrapper > > Best regards, > --lucas > > > > ----- Mail original ----- > > De: "Viktor Roytman" > > > À: "python-ideas" > > > Envoyé: Vendredi 12 Avril 2019 18:01:43 > > Objet: Re: [Python-ideas] Syntax for al

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: > > Curr

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, 'b

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

2019-04-12 Thread Lucas Bourneuf
for arg, value in kwargs.items() if arg in formal_parameters }) return wrapper Best regards, --lucas - Mail original - > De: "Viktor Roytman" > À: "python-ideas" > Envoyé: Vendredi 12 Avril 2019 18:01:43 > Objet: Re: [Pytho

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 Fr

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 conventio

[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):