Issue 10682: With 'args' or even bare '' in def/call argument list, trailing comma causes SyntaxError (original) (raw)

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: , 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',
):
    ...