Title: Avoid conflicts when pass arbitrary keyword arguments to Python function
Type: enhancement Stage: resolved
Components: Interpreter Core Versions: Python 3.8
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: gvanrossum, lkollar, mariocj89, serhiy.storchaka, steve.dower
Priority: normal Keywords: patch

Created on 2019-04-03 14:14 by serhiy.storchaka, last changed 2019-09-11 11:05 by steve.dower. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 12674 closed serhiy.storchaka, 2019-04-03 14:15
PR 13700 serhiy.storchaka, 2019-06-01 06:40
Messages (4)
msg339392 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-04-03 14:14
This is yet one alternative to PEP 570. It does not solve all problems that PEP 570 is purposed to solve, but it significantly reduces the need in positional-only parameters.

Currently the problem with implementing in Python functions that should accept arbitrary keyword arguments is that argument names can conflict with names of other parameters (in particularly "self"). For example, look at the function

    def log(fmt, **kwargs):

You cannot call log('Format: {fmt}', fmt='binary'), because the argument for parameter "fmt" is specified twice: as positional argument and as keyword argument.

The idea is that if the function has the var-keyword parameter, then keyword arguments with names which match passed positional arguments will be saved into the var-keyword dict instead of be error.

The advantage of this idea over alternatives is that it does not need changing the user code. Implementing this feature will fix the user code that we do not even see. Most functions that otherwise would need positional only parameters (over 60 in the stdlib) will be automatically fixed by this feature. We could revert the deprecations added in issue36492 and simplify few functions that used the *args hack before.

The change itself is very simple, just modification of few lines in ceval.c and

The disadvantage is that it does not help with optional parameters. For example:

    def make_dict(dict=(), **kwargs):
        res = {}
        return res

make_dict(dict={}, list=[]) will still return {'list': []} instead of {'dict': {}, list: []}. You still need to use the *args hack to get the latter result. But there are not much such functions.

This idea was proposed by Steve [1].

msg339410 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2019-04-03 18:29
This will require a PEP discussion before it can move forward.
msg340605 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-04-21 10:34
Steve, do you mind to create a PEP? I can help with examples and comments, but it is too hard to me to write it myself.
msg340631 - (view) Author: Steve Dower (steve.dower) * (Python committer) Date: 2019-04-22 02:16
No, now there is syntax that anyone using built in functions will have already learned I see no reason to create another way to solve the problem.
Date User Action Args
2019-09-11 11:05:42steve.dowersetstatus: open -> closed
resolution: rejected
stage: patch review -> resolved
2019-06-01 06:40:08serhiy.storchakasetpull_requests: + pull_request13606
2019-04-22 02:16:49steve.dowersetmessages: + msg340631
2019-04-21 10:34:03serhiy.storchakasetmessages: + msg340605
2019-04-03 18:29:41gvanrossumsetmessages: + msg339410
2019-04-03 17:33:54lkollarsetnosy: + lkollar
2019-04-03 15:12:55mariocj89setnosy: + mariocj89
2019-04-03 14:15:44serhiy.storchakasetkeywords: + patch
stage: patch review
pull_requests: + pull_request12601
2019-04-03 14:14:02serhiy.storchakacreate