classification
Title: SyntaxError: trailing comma not allowed ... misleading
Type: Stage: resolved
Components: Parser Versions: Python 3.10, Python 3.9
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: aroberge, lukasz.langa, lys.nikolaou, miss-islington, pablogsal
Priority: normal Keywords: patch

Created on 2021-08-18 11:48 by aroberge, last changed 2021-08-18 21:04 by lukasz.langa. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 27814 merged pablogsal, 2021-08-18 18:09
PR 27816 merged miss-islington, 2021-08-18 20:09
PR 27817 merged lukasz.langa, 2021-08-18 20:40
Messages (8)
msg399837 - (view) Author: Andre Roberge (aroberge) * Date: 2021-08-18 11:48
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...
msg399839 - (view) Author: Andre Roberge (aroberge) * Date: 2021-08-18 12:03
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
msg399843 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2021-08-18 13:52
Hummm, interesting.

I wonder if this only happens with keywords or if this can be reproduced with other constructs
msg399859 - (view) Author: Andre Roberge (aroberge) * Date: 2021-08-18 17:40
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).
msg399860 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2021-08-18 18:06
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
msg399867 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-08-18 20:09
New changeset b2f68b190035540872072ac1d2349e7745e85596 by Pablo Galindo Salgado in branch 'main':
bpo-44947: Refine the syntax error for trailing commas in import statements (GH-27814)
https://github.com/python/cpython/commit/b2f68b190035540872072ac1d2349e7745e85596
msg399868 - (view) Author: miss-islington (miss-islington) Date: 2021-08-18 20:32
New changeset 846a10fc45ef83b41d1b1b568a9ee8012f37c8c2 by Miss Islington (bot) in branch '3.10':
bpo-44947: Refine the syntax error for trailing commas in import statements (GH-27814)
https://github.com/python/cpython/commit/846a10fc45ef83b41d1b1b568a9ee8012f37c8c2
msg399871 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-08-18 21:04
New changeset 4e4d35d3325fb1e05dad43bd1ec73f14c3c5dc0a 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)
https://github.com/python/cpython/commit/4e4d35d3325fb1e05dad43bd1ec73f14c3c5dc0a
History
Date User Action Args
2021-08-18 21:04:56lukasz.langasetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2021-08-18 21:04:07lukasz.langasetmessages: + msg399871
2021-08-18 20:40:28lukasz.langasetpull_requests: + pull_request26282
2021-08-18 20:32:09miss-islingtonsetmessages: + msg399868
2021-08-18 20:09:37lukasz.langasetnosy: + lukasz.langa
messages: + msg399867
2021-08-18 20:09:29miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request26281
2021-08-18 18:09:54pablogsalsetkeywords: + patch
stage: patch review
pull_requests: + pull_request26279
2021-08-18 18:06:49pablogsalsetmessages: + msg399860
2021-08-18 17:40:06arobergesetmessages: + msg399859
2021-08-18 13:52:23pablogsalsetmessages: + msg399843
2021-08-18 12:03:02arobergesetmessages: + msg399839
versions: + Python 3.9
2021-08-18 11:48:58arobergecreate