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

Terry Reedy tjreedy at udel.edu
Thu Jul 9 00:03:11 CEST 2015


On 7/8/2015 1:53 AM, Nick Coghlan wrote:
> 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

> 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 :(

Could that be changed?

An alternate approach is a separate fence-matcher function.  Before I 
switched to Python 17+ years ago, I wrote a table-driven 
finite-state-machine matcher in C and a complete table for K&R/C89 C, 
which included info that openers were be be ignored within comments and 
strings. It reported the line and column of unclosed openers. I wrote it 
for my own use because I was frustrated by poor C compiler error messages.

I have occasionally thought about developing a table for Python (and 
rewriting in Python), but indents and dedents are not trivial.  (Even 
tokenizer.py does not handle \t indents correctly.)  Maybe I should 
think a bit harder.  Idle has an option to syntax-check a module without 
running it.  If compile messages are not improved, it would certainly be 
sensible to run a separate fence-checker at least when check-only is 
requested, for better error messages.  These could potentially include 
'missing :' when a header 'opened' by 
for/while/if/elif/else/class/def/with is not closed by ':'.

-- 
Terry Jan Reedy



More information about the Python-ideas mailing list