cpython icon indicating copy to clipboard operation
cpython copied to clipboard

Make SyntaxError when mixing except/except* point to the offending clause

Open lysnikolaou opened this issue 3 years ago • 4 comments

After #99160, the SyntaxError points to the try keyword, but I think it would be relatively easy to make it point to the except or except* (whichever one is responsible for the SyntaxError) by extracting different alternatives. Do you think that pointing to the try is good enough? @iritkatriel @pablogsal

  • PR: gh-99215

lysnikolaou avatar Nov 07 '22 18:11 lysnikolaou

How do we know which one is wrong?

iritkatriel avatar Nov 07 '22 18:11 iritkatriel

I'm not sure whether this would work, but I was thinking that we could do something like this:

invalid_try_stmt:
   ...
   | 'try' ':' block* except_block+ a='except' b='*' expression ['as' NAME] ':' block+ {
        RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "cannot have 'except*' on a 'try' statement with 'except'") }
   | 'try' ':' block* except_star_block+ a='except' [expression ['as' NAME]] ':' block+ {
        RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot have 'except' on a 'try' statement with 'except*'") }

I haven't tested it, but I can spend some time on it later today or early tomorrow to see if it'd work.

lysnikolaou avatar Nov 07 '22 18:11 lysnikolaou

Yes, I guess this might be better than the try. Even if the first clause was the wrong one, this points to where the type flips.

iritkatriel avatar Nov 07 '22 18:11 iritkatriel

Correct.

lysnikolaou avatar Nov 07 '22 18:11 lysnikolaou