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

Todd toddrjen at gmail.com
Wed Jul 8 11:03:42 CEST 2015

On Jul 8, 2015 10:02 AM, "Stephen J. Turnbull" <stephen at xemacs.org> wrote:
> Nick Coghlan writes:
>  > 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).

True, but we can definitely say it occurs on or after the first line in
your example. So could we do something like:

     File "/home/me/myfile.py", line 11
         data = func()
   SyntaxError: Unmatched '(' starting somewhere after line 7

That would at least allow you to narrow down where to look for the problem.

Of course in cases where the starting point is the current line or the
previous line you could, in principle, have simpler exception messages. It
may not be worth the increased complexity or decreased consistency, though.

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

I think there are two problems. First, it isn't clear what the problem is.
Second, it is misleading about where the problem occurs.

So I think the goal would be to have an exception that states what the
problem is and doesn't give the wrong place to look for the problem.  It
doesn't have to give the right place, but it can't give the wrong place.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150708/b556893d/attachment-0001.html>

More information about the Python-ideas mailing list