[Python-ideas] Syntax for allowing extra keys when unpacking a dict as keyword arguments
viktor.roytman at gmail.com
Fri Apr 12 14:46:29 EDT 2019
> > 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 do some validation on your dictionary of
> keyword arguments? Otherwise you are setting yourself up for a world of
> pain. If you do that, you should take the opportunity to decide what to
> do with invalid keys.
The specific pain point that motivated this was constructing many
interrelated models using a dict. So, for example, if there is a User model
with a related Address model, and the input is
user_kwargs = dict(
then passing this information in to the User constructor directly will
fail, because User.address is a related model, not a simple field.
I agree that most of the time you should want unexpected keyword arguments
to raise an exception, but in specific circumstances it can be helpful to
extract only the pieces of a dict that are relevant.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-ideas