This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Argument Clinic should understand *args and **kwargs parameters
Type: enhancement Stage: patch review
Components: Argument Clinic, Demos and Tools Versions: Python 3.7
Status: open Resolution:
Dependencies: Superseder:
Assigned To: serhiy.storchaka Nosy List: BTaskaya, colorfulappl, gphemsley, kj, larry, meador.inge, methane, ncoghlan, rhettinger, serhiy.storchaka, vstinner, zach.ware
Priority: normal Keywords: patch

Created on 2014-01-18 01:55 by larry, last changed 2022-04-11 14:57 by admin.

Pull Requests
URL Status Linked Edit
PR 18609 merged BTaskaya, 2020-02-24 17:23
PR 27211 merged kj, 2021-07-17 09:09
PR 30286 colorfulappl, 2021-12-31 07:52
PR 32092 open colorfulappl, 2022-03-24 08:27
Messages (10)
msg208380 - (view) Author: Larry Hastings (larry) * (Python committer) Date: 2014-01-18 01:55
Argument Clinic currently prevents the "impl" function from ever seeing the "args" tuple or the "kwargs" dict.  There should be a way to ask it to pass those values in to the "impl" function.
msg225514 - (view) Author: Larry Hastings (larry) * (Python committer) Date: 2014-08-19 05:37
So, let's think about this for a minute.  What's the API that we *want* here?

If your function has the signature
   (a, b, c=20, *args)
and you call it with
   (1, 2, 3, 4, 5)
should "args" be (4, 5), or (1, 2, 3, 4, 5)?

I assert that the impl function should get the same "args" (and "kwargs") that a Python function would--that is, post-argument-processing.  In the above example "args" should get (4, 5).

This might be somewhat painful to do in round 1, where we're still leveraging off PyArg_ParseTuple*.  But in the future it'll be cheaper to do it this way.  In any case, it's the right API, so that's what we should do.

(Adding Nick just to see if he agrees--he had a use case for *args in the builtin module.)
msg225531 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2014-08-19 12:40
Yes, I agree we should follow the Python level semantics, and only capture the excess positional arguments. For the record, the four builtins I flagged as needing this in order to add introspection information:

- 2 positional only args, arbitrary additional args
- checks size with PyTuple_GET_SIZE
- uses PyTuple_GET_ITEM x2 + PyTuple_GetSlice to unpack

- only arbitrary position args, iterates and extracts using PyTuple_GetItem
- uses PyArg_ParseTupleAndKeywords with an empty tuple to extract the keyword-only args

- use a shared helper function min_max
- uses the args tuple directly if size > 1
- otherwise uses PyArg_UnpackTuple to extract the supplied iterable
- uses PyArg_ParseTupleAndKeywords with an empty tuple to extract the keyword-only args
- this "one arg means iterable" style API might need to be a special case...
msg264959 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-05-06 10:24
I think at first step we can support var-positional parameter only when there are no other positional parameters, and var-keyword parameter only when there are no other keyword parameters. So print, max and dict.update will be supported, but __build_class__, map and functools.partial are not.
msg285642 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-01-17 14:21
I'll try to implement the support of var-positional parameters.
msg285647 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2017-01-17 15:03
In case it is helpful, here's my list of examples where the AC and existing signature objects are insufficiently expressive:

type(name, bases, mapping)
    two different signatures depending on type   

range(start, stop)
range(start, stop, step)

dict.pop(key[, default])
   default of None has different meaning than missing default
   which raises KeyError when the key is missing

itertools.permutations(iterable[, r])
   where the absence of *r* implies r=len(iterable)

bisect.bisect_right(a, x[, lo[, hi]]) -> index
   where the absence of *hi* implies hi=len(a)

min(iterable, *[, default=obj, key=func]) -> value
min(arg1, arg2, *args, *[, key=func]) -> value
   has two signatures depending on the number of
   positional arguments and a keyword argument
   only used in the first signature.  It's implementation
   is also shared with max().

dict() -> new empty dictionary
dict(mapping) -> new dictionary initialized from a mapping object's
   (key, value) pairs
dict(iterable) -> new dictionary initialized as if via:
   d = {}
   for k, v in iterable:
       d[k] = v
dict(**kwargs) -> new dictionary initialized with the name=value pairs
   in the keyword argument list.  For example:  dict(one=1, two=2)

def sumseq(seq, a=0, b=None):
    # Pure python code with nullable int
    if b is None:
        b = len(seq)
    return sum(seq[a:b])
msg285654 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2017-01-17 15:20
FYI I started to work on a different Argument Clinic enhancement, issue #29299: "Argument Clinic: Fix signature of optional positional-only arguments".
msg285660 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-01-17 15:49
Thank you for your examples Raymond, but they don't directly related to this issue, implementing support of var-positional and var-keyword parameters. I believe that it is possible to solve it, and the solution is complex, but is not extremal hard. I'm working on the first part of this.

Your examples show other, more hard issue. It looks to me that the only solution of that issue is to add support of multiple signatures for functions. But this can break the API of the inspect module.
msg285779 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2017-01-19 11:47
Once this feature will be implemented, print() should be modified to use Argument Clinic: see the issue #29296.
msg397721 - (view) Author: Batuhan Taskaya (BTaskaya) * (Python committer) Date: 2021-07-17 11:09
New changeset f88e138a1aa3b9a9e013963e4fd7d5cce6a0b85c by Ken Jin in branch 'main':
bpo-20291: Fix MSVC warnings in getargs.c (GH-27211)
Date User Action Args
2022-04-11 14:57:57adminsetgithub: 64490
2022-03-24 08:27:39colorfulapplsetpull_requests: + pull_request30177
2021-12-31 07:52:19colorfulapplsetnosy: + colorfulappl
pull_requests: + pull_request28526
2021-07-17 11:09:25BTaskayasetmessages: + msg397721
2021-07-17 09:09:32kjsetnosy: + kj
pull_requests: + pull_request25749
2020-02-24 17:23:47BTaskayasetkeywords: + patch
nosy: + BTaskaya

pull_requests: + pull_request18009
stage: needs patch -> patch review
2017-12-23 04:23:15gphemsleysetnosy: + gphemsley
2017-01-20 07:51:29vstinnerunlinkissue29296 dependencies
2017-01-19 11:47:14vstinnersetmessages: + msg285779
2017-01-17 15:49:52serhiy.storchakasetmessages: + msg285660
2017-01-17 15:20:27vstinnersetnosy: + vstinner
messages: + msg285654
2017-01-17 15:03:03rhettingersetnosy: + rhettinger
messages: + msg285647
2017-01-17 14:21:01serhiy.storchakasetassignee: larry -> serhiy.storchaka
messages: + msg285642
versions: + Python 3.7, - Python 3.4, Python 3.5
2017-01-17 12:35:39methanesetnosy: + methane
2017-01-17 12:35:20methanelinkissue29296 dependencies
2016-05-06 10:24:38serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg264959
2015-02-25 15:29:05serhiy.storchakasetcomponents: + Argument Clinic
2014-08-19 12:40:52ncoghlansetmessages: + msg225531
2014-08-19 05:37:31larrysetnosy: + ncoghlan
messages: + msg225514
2014-08-13 18:35:44zach.waresetnosy: + zach.ware

type: behavior -> enhancement
components: + Demos and Tools
versions: + Python 3.5
2014-01-18 01:55:25larrycreate