Skip to content
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

Raise syntax error if there are both except and except* in the same try block #14860

Closed
dhruvmanila opened this issue Dec 9, 2024 · 0 comments · Fixed by #14895
Closed

Raise syntax error if there are both except and except* in the same try block #14860

dhruvmanila opened this issue Dec 9, 2024 · 0 comments · Fixed by #14895
Labels
bug Something isn't working help wanted Contributions especially welcome parser Related to the parser

Comments

@dhruvmanila
Copy link
Member

The task is to fix this TODO:

// TODO(dhruvmanila): Raise syntax error if there are both 'except' and 'except*'
// on the same 'try'
// test_err try_stmt_mixed_except_kind
// try:
// pass
// except:
// pass
// except* ExceptionGroup:
// pass
// try:
// pass
// except* ExceptionGroup:
// pass
// except:
// pass

I'd consider this as a bug because the AST marks the entire try block as containing an except* and because the parser doesn't raise a syntax error, the formatter will add the * to all except block (https://play.ruff.rs/059d8f35-5f66-4be5-887e-9f38fa75b40e):

  try:
      pass
- except:
+ except*:
      pass
  except* ExceptionGroup:
      pass

CPython parser (not the compiler) also raises a syntax error:

$ pbpaste | python -m ast -
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/Users/dhruv/.pyenv/versions/3.12.1/lib/python3.12/ast.py", line 1834, in <module>
    main()
  File "/Users/dhruv/.pyenv/versions/3.12.1/lib/python3.12/ast.py", line 1830, in main
    tree = parse(source, args.infile.name, args.mode, type_comments=args.no_type_comments)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/dhruv/.pyenv/versions/3.12.1/lib/python3.12/ast.py", line 52, in parse
    return compile(source, filename, mode, flags,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<stdin>", line 5
    except* ExceptionGroup:
    ^^^^^^^
SyntaxError: cannot have both 'except' and 'except*' on the same 'try'
@dhruvmanila dhruvmanila added bug Something isn't working parser Related to the parser help wanted Contributions especially welcome labels Dec 9, 2024
TheBits pushed a commit to TheBits/ruff that referenced this issue Dec 11, 2024
This PR adds a syntax error if the parser encounters a `TryStmt` that
has except clauses both with and without a star.

The displayed error points to each except clause that contradicts the
original except clause kind. So, for example,

```python
try:
    ....
except:     #<-- we assume this is the desired except kind
    ....
except*:    #<---  error will point here
    ....
except*:    #<--- and here
    ....
```

Closes astral-sh#14860
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Contributions especially welcome parser Related to the parser
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant