Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
lysnikolaou opened this issue Nov 7, 2022 · 4 comments
Closed

Comments

@lysnikolaou
Copy link
Member

lysnikolaou commented Nov 7, 2022

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

@iritkatriel
Copy link
Member

How do we know which one is wrong?

@lysnikolaou
Copy link
Member Author

lysnikolaou commented Nov 7, 2022

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.

@iritkatriel
Copy link
Member

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.

@lysnikolaou
Copy link
Member Author

Correct.

lysnikolaou added a commit to lysnikolaou/cpython that referenced this issue Nov 7, 2022
lysnikolaou added a commit to lysnikolaou/cpython that referenced this issue Nov 7, 2022
miss-islington pushed a commit that referenced this issue Nov 20, 2022
lysnikolaou added a commit to lysnikolaou/cpython that referenced this issue Nov 20, 2022
…them (pythonGH-99215)

Automerge-Triggered-By: GH:lysnikolaou
(cherry picked from commit 9c4232a)
lysnikolaou added a commit that referenced this issue Nov 20, 2022
… them (GH-99215) (GH-99622)

gh-99211: Point to except/except* on syntax errors when mixing them (GH-99215)

(cherry picked from commit 9c4232a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants