[issue44947] SyntaxError: trailing comma not allowed ... misleading

Andre Roberge report at bugs.python.org
Wed Aug 18 07:48:58 EDT 2021


New submission from Andre Roberge <andre.roberge at gmail.com>:

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...

----------
components: Parser
messages: 399837
nosy: aroberge, lys.nikolaou, pablogsal
priority: normal
severity: normal
status: open
title: SyntaxError: trailing comma not allowed ... misleading
versions: Python 3.10

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue44947>
_______________________________________


More information about the Python-bugs-list mailing list