[issue40599] Improve error messages with expected keywords

Guido van Rossum report at bugs.python.org
Mon May 11 18:44:46 EDT 2020


Guido van Rossum <guido at python.org> added the comment:

Hmm... The errors get long, and by focusing only on keywords they can be misleading. E.g.

>>> from x import a b c
  File "<stdin>", line 1
    from x import a b c
                    ^
SyntaxError: Invalid syntax. Expected one of: as
>>> 

But the most likely error is omission of a comma.

>>> if x y: pass
  File "<stdin>", line 1
    if x y: pass
         ^
SyntaxError: Invalid syntax. Expected one of: not, is, or, in, and, if
>>> 

But the most likely error is probably a comparison operator.

And so on. Here's a nice one:

>>> /
  File "<stdin>", line 1
    /
    ^
SyntaxError: Invalid syntax. Expected one of: for, pass, lambda, False, global, True, __new_parser__, if, raise, continue, not, break, while, None, del, nonlocal, import, assert, return, class, with, def, try, from, yield
>>> 

(Huh, where did it get __new_parser__?)

The beauty of Python's detail-free syntax error is that it doesn't tell you what it expects -- because parsers are dumb, what the parser expected is rarely what's wrong with your code -- and it requires the user to understand how the parser works to interpret the error message.

----------

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


More information about the Python-bugs-list mailing list