[Python-ideas] Reporting unmatched parentheses in SyntaxError messages?

Stephen J. Turnbull stephen at xemacs.org
Wed Jul 8 10:02:24 CEST 2015

Nick Coghlan writes:

 > One of the more opaque error messages new Python users can encounter
 > is a syntax error due to unmatched parentheses:
 >     File "/home/me/myfile.py", line 11
 >         data = func()
 >         ^
 >     SyntaxError: invalid syntax

Agreed.  Could be worse, though, it could be Lisp!

 > While I have no idea how we could implement it, I'm wondering if that
 > might be clearer if the error message instead looked more like this:
 >     File "/home/me/myfile.py", line 11
 >         data = func()
 >         ^
 >     SyntaxError: invalid syntax (Unmatched '(' on line 10)

I think I would prefer "Expected ')'".  I think that typos like

    a = ((1, "one"),
         (2, "two)",
         (3, "three"))
    data = func()

are likely to be fairly common (I make them often enough!), but I
don't see how you're going to get the parser to identify the line
containing "couple #2" as the source of the error (without a *really*
dubious heuristic).

 > I'm not sure it would be feasible though - we generate syntax errors
 > from a range of locations where we don't have access to the original
 > token data any more :(

Is the problem that we don't know which line the unmatched parenthesis
was on, or that we don't even know that the syntax error is an
unmatched parenthesis?

More information about the Python-ideas mailing list