Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

With '*args' or even bare '*' in def/call argument list, trailing comma causes SyntaxError #54891

Closed
zuo mannequin opened this issue Dec 12, 2010 · 9 comments
Closed
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement

Comments

@zuo
Copy link
Mannequin

zuo mannequin commented Dec 12, 2010

BPO 10682
Nosy @loewis, @brettcannon, @rhettinger, @mdickinson, @gst, @mlissner
Superseder
  • bpo-9232: Allow trailing comma in any function argument list.
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2010-12-12.20:51:06.497>
    created_at = <Date 2010-12-12.04:29:59.913>
    labels = ['interpreter-core', 'type-feature']
    title = "With '*args' or even bare '*' in def/call argument list, trailing comma causes SyntaxError"
    updated_at = <Date 2016-03-29.18:48:27.058>
    user = 'https://bugs.python.org/zuo'

    bugs.python.org fields:

    activity = <Date 2016-03-29.18:48:27.058>
    actor = 'Mike.Lissner'
    assignee = 'none'
    closed = True
    closed_date = <Date 2010-12-12.20:51:06.497>
    closer = 'loewis'
    components = ['Interpreter Core']
    creation = <Date 2010-12-12.04:29:59.913>
    creator = 'zuo'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 10682
    keywords = []
    message_count = 9.0
    messages = ['123823', '123824', '123825', '123828', '123847', '123848', '123850', '246696', '262619']
    nosy_count = 7.0
    nosy_names = ['loewis', 'brett.cannon', 'rhettinger', 'mark.dickinson', 'zuo', 'gstarck', 'Mike.Lissner']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = None
    status = 'closed'
    superseder = '9232'
    type = 'enhancement'
    url = 'https://bugs.python.org/issue10682'
    versions = ['Python 3.3']

    @zuo
    Copy link
    Mannequin Author

    zuo mannequin commented Dec 12, 2010

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

    @zuo zuo mannequin added type-bug An unexpected behavior, bug, or error interpreter-core (Objects, Python, Grammar, and Parser dirs) labels Dec 12, 2010
    @zuo
    Copy link
    Mannequin Author

    zuo mannequin commented Dec 12, 2010

    s/{5: 6}/{'5': 6}/g (but it's a detail)

    @zuo
    Copy link
    Mannequin Author

    zuo mannequin commented Dec 12, 2010

    Oops, I see the problem was partly addressed @ bpo-9232.

    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?

    @loewis
    Copy link
    Mannequin

    loewis mannequin commented Dec 12, 2010

    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.

    @loewis loewis mannequin added type-feature A feature request or enhancement and removed type-bug An unexpected behavior, bug, or error labels Dec 12, 2010
    @rhettinger
    Copy link
    Contributor

    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.

    @brettcannon
    Copy link
    Member

    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.

    @loewis
    Copy link
    Mannequin

    loewis mannequin commented Dec 12, 2010

    Ok, so closing as rejected.

    @loewis loewis mannequin closed this as completed Dec 12, 2010
    @gst
    Copy link
    Mannequin

    gst mannequin commented Jul 13, 2015

    unneeded consistency

    "unneeded" consistency !:?

    I'd say that either there is a "full or complete" consistency on the mentionned point/element of syntax, or there is none .. but an unneeded one.. or an half-one, isn't "consistent consistency" by then ;)

    @mlissner
    Copy link
    Mannequin

    mlissner mannequin commented Mar 29, 2016

    This is an old issue, but where I run into it frequently is when I use the format function and string interpolation. For example, this throws a SyntaxError:

    "The name of the person is {name_first} {name_last}".format(
    **my_obj.__dict__,
    )

    Because strings tend to be fairly long, it's pretty common that the arguments to format end up on their own line.

    I was always taught to use trailing commas in Python, and I'm fanatical about ensuring they're there. It's a smart part of the language that saves you from many bugs and much typing when copy/pasting/tweaking.

    This is the first time I've ever run into an implementation bug in CPython, and at least from the post on StackOverflow, this looks like the parser isn't obeying the grammar: https://stackoverflow.com/questions/16950394/python-why-is-this-invalid-syntax

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants