classification
Title: With '*args' or even bare '*' in def/call argument list, trailing comma causes SyntaxError
Type: enhancement Stage:
Components: Interpreter Core Versions: Python 3.3
process
Status: closed Resolution: duplicate
Dependencies: Superseder: Allow trailing comma in any function argument list.
View: 9232
Assigned To: Nosy List: brett.cannon, loewis, mark.dickinson, rhettinger, zuo
Priority: normal Keywords:

Created on 2010-12-12 04:29 by zuo, last changed 2010-12-13 23:57 by ncoghlan. This issue is now closed.

Messages (7)
msg123823 - (view) Author: Jan Kaliszewski (zuo) Date: 2010-12-12 04:29
Let examples speak:

    def x(a, z): pass      # this is ok
    def x(a, z,): pass     # this is ok
    def x(a, *, z): pass   # this is ok in Py3k
    def x(a, *, z,): pass  # but this causes SyntaxError (!)

    def x(a, *args): pass      # this is ok
    def x(a, *args,): pass     # but this causes SyntaxError
    def x(a, **kwargs): pass   # this is ok
    def x(a, **kwargs,): pass  # but this causes SyntaxError
    def x(a, *args, z): pass   # this is ok in Py3k
    def x(a, *args, z,): pass  # but this causes SyntaxError (!)

And similarly -- calls:

    def x(*args, **kwargs): pass
    x(1, *(2,3,4))        # this is ok
    x(1, *(2,3,4),)       # but this causes SyntaxError
    x(1, **{5: 6})        # this is ok
    x(1, **{5: 6},)       # but this causes SyntaxError
    x(1, 2, 3, 4, z=5)    # this is ok
    x(1, *(2,3,4), z=5)   # this is ok in Py2.6+ and Py3k
    x(1, *(2,3,4), z=5,)  # but this causes SyntaxError (!)

Similar problem was discussed years ago as a docs bug -- see: issue798652, but -- as inconsistent with intuition and a general Python rule that trailing commas are ok in argument lists and sequence literals (except empty ones) -- it seems to be rather a language syntax definition issue.

Now, after introducing new syntax possibilities in Py2.6 and especially Py3k (see the examples above), the issue is even much more painful.

Also, please consider that Py3k's function annotations encourage to format argument list in one-argument-per-line-manner:

    def my_func(
        spam:"Very tasty and nutritious piece of food",
        ham:"For experts only",
        *more_spam:"Not less tasty and not less nutritious!",
        spammish:"Nobody expects this!"='Inquisition',
    ):
        ...
msg123824 - (view) Author: Jan Kaliszewski (zuo) Date: 2010-12-12 04:36
s/**{5: 6}/**{'5': 6}/g (but it's a detail)
msg123825 - (view) Author: Jan Kaliszewski (zuo) Date: 2010-12-12 05:02
Oops, I see the problem was partly addressed @ issue9232.

But:

* the proposed patch doesn't fix calls (but defs only),

* shouldn't be the issue considered as a bug -- and fixed also in 2.7 and 3.1?
msg123828 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2010-12-12 07:54
It's clearly not a bug: the documented grammar matches the implemented grammar. Asking for more abstract consistency is a feature request. This also clearly falls under the PEP 3003 moratorium.
msg123847 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2010-12-12 19:59
The current behavior for function definitions is beneficial because a trailing comma in the argument list is likely to signal a real error (omitted variable).

In contrast, the trailing comma for lists is useful because entries on separate lines in a repeating pattern that makes it easy to add or remove entries.

The use cases for both function definitions and lists are best served by the current behavior, more so that a notion driven by "foolish consistency".

Recommend rejecting and closing.
msg123848 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2010-12-12 20:30
I'm with Raymond; this is unneeded consistency. I honestly would rather see what little support there is for a trailing comma to go away, but w/o looking at the grammar I am willing to bet that would be a pain to get right and not be worth the effort.
msg123850 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2010-12-12 20:51
Ok, so closing as rejected.
History
Date User Action Args
2010-12-13 23:57:44ncoghlansetresolution: rejected -> duplicate
superseder: Allow trailing comma in any function argument list.
2010-12-12 20:51:06loewissetstatus: open -> closed
resolution: rejected
messages: + msg123850
2010-12-12 20:30:33brett.cannonsetnosy: + brett.cannon
messages: + msg123848
2010-12-12 19:59:11rhettingersetnosy: + rhettinger
messages: + msg123847
2010-12-12 16:36:27r.david.murraysetnosy: + mark.dickinson
2010-12-12 07:54:40loewissetversions: - Python 3.1, Python 2.7, Python 3.2
nosy: + loewis

messages: + msg123828

type: behavior -> enhancement
2010-12-12 05:02:11zuosetmessages: + msg123825
2010-12-12 04:36:03zuosetmessages: + msg123824
2010-12-12 04:29:59zuocreate