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

SyntaxError: trailing comma not allowed ... misleading #89110

Closed
aroberge mannequin opened this issue Aug 18, 2021 · 8 comments
Closed

SyntaxError: trailing comma not allowed ... misleading #89110

aroberge mannequin opened this issue Aug 18, 2021 · 8 comments
Labels
3.9 only security fixes 3.10 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs)

Comments

@aroberge
Copy link
Mannequin

aroberge mannequin commented Aug 18, 2021

BPO 44947
Nosy @aroberge, @ambv, @lysnikolaou, @pablogsal, @miss-islington
PRs
  • bpo-44947: Refine the syntax error for trailing commas in import statements #27814
  • [3.10] bpo-44947: Refine the syntax error for trailing commas in import statements (GH-27814) #27816
  • [3.9] bpo-44947: Refine the syntax error for trailing commas in import statements (GH-27814) #27817
  • 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 2021-08-18.21:04:56.153>
    created_at = <Date 2021-08-18.11:48:58.213>
    labels = ['interpreter-core', '3.9', '3.10']
    title = 'SyntaxError: trailing comma not allowed ... misleading'
    updated_at = <Date 2021-08-18.21:04:56.153>
    user = 'https://github.com/aroberge'

    bugs.python.org fields:

    activity = <Date 2021-08-18.21:04:56.153>
    actor = 'lukasz.langa'
    assignee = 'none'
    closed = True
    closed_date = <Date 2021-08-18.21:04:56.153>
    closer = 'lukasz.langa'
    components = ['Parser']
    creation = <Date 2021-08-18.11:48:58.213>
    creator = 'aroberge'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 44947
    keywords = ['patch']
    message_count = 8.0
    messages = ['399837', '399839', '399843', '399859', '399860', '399867', '399868', '399871']
    nosy_count = 5.0
    nosy_names = ['aroberge', 'lukasz.langa', 'lys.nikolaou', 'pablogsal', 'miss-islington']
    pr_nums = ['27814', '27816', '27817']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue44947'
    versions = ['Python 3.9', 'Python 3.10']

    @aroberge
    Copy link
    Mannequin Author

    aroberge mannequin commented Aug 18, 2021

    Consider the following four slightly different examples:

    Python 3.10.0rc1 ...

    >>> from math import sin and cos
      File "<stdin>", line 1
        from math import sin and cos
                             ^^^
    SyntaxError: invalid syntax
    
    
    >>> from math import sin, cos, and tan
      File "<stdin>", line 1
        from math import sin, cos, and tan
                                   ^^^
    SyntaxError: trailing comma not allowed without surrounding parentheses
    
    
    >>> from math import (sin, cos,) and tan
      File "<stdin>", line 1
        from math import (sin, cos,) and tan
                                     ^^^
    SyntaxError: invalid syntax
    
    
    >>> from math import sin, cos and tan
      File "<stdin>", line 1
        from math import sin, cos and tan
                                  ^^^
    SyntaxError: invalid syntax

    ====
    In all four cases, the keyword 'and' is correctly identified as causing the error. In the second case, the message given may suggest that adding parentheses is all that is needed to correct the problem; however, that is "obviously" not the case as shown in the third case.

    **Perhaps** when a _keyword_ like 'and' is identified as a problem, a generally better message would be something like

    SyntaxError: the keyword 'and' is not allowed here

    leaving out all guesses like 'surrounding by parentheses', "meaning == instead of =", 'perhaps forgot a comma', etc., which are sometimes added by Python 3.10+ ?

    I am fully and painfully aware that attempting to provide helpful and accurate error message is challenging...

    @aroberge aroberge mannequin added 3.10 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) labels Aug 18, 2021
    @aroberge
    Copy link
    Mannequin Author

    aroberge mannequin commented Aug 18, 2021

    This message is not new to Python 3.10 as it is also shown with Python 3.9.5.

    >>> from math import sin, cos, and tan
      File "<stdin>", line 1
        from math import sin, cos, and tan
                                   ^
    SyntaxError: trailing comma not allowed without surrounding parentheses

    @aroberge aroberge mannequin added 3.9 only security fixes labels Aug 18, 2021
    @pablogsal
    Copy link
    Member

    Hummm, interesting.

    I wonder if this only happens with keywords or if this can be reproduced with other constructs

    @aroberge
    Copy link
    Mannequin Author

    aroberge mannequin commented Aug 18, 2021

    Based on what I just read on https://github.com/davidhalter/parso/blob/master/parso/python/issue_list.txt
    I gather that this exception is only raised in the context of an import statement with a trailing comma (usually followed by nothing).

    @pablogsal
    Copy link
    Member

    The problem is not the keyword, is that we are parsing correctly until the "and" and we find the comma there. It happens with anything that invalidates more items:

    >>> from math import sin, cos, $ tan
      File "<stdin>", line 1
        from math import sin, cos, $ tan
                                   ^
    SyntaxError: trailing comma not allowed without surrounding parentheses

    @ambv
    Copy link
    Contributor

    ambv commented Aug 18, 2021

    New changeset b2f68b1 by Pablo Galindo Salgado in branch 'main':
    bpo-44947: Refine the syntax error for trailing commas in import statements (GH-27814)
    b2f68b1

    @miss-islington
    Copy link
    Contributor

    New changeset 846a10f by Miss Islington (bot) in branch '3.10':
    bpo-44947: Refine the syntax error for trailing commas in import statements (GH-27814)
    846a10f

    @ambv
    Copy link
    Contributor

    ambv commented Aug 18, 2021

    New changeset 4e4d35d by Łukasz Langa in branch '3.9':
    [3.9] bpo-44947: Refine the syntax error for trailing commas in import statements (GH-27814) (GH-27817)
    4e4d35d

    @ambv ambv closed this as completed Aug 18, 2021
    @ambv ambv closed this as completed Aug 18, 2021
    @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
    3.9 only security fixes 3.10 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs)
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants