[syntax-errors] Limit except* range to * (#16473)

Summary
--
This is a follow-up to #16446 to fix the diagnostic range to point to
the `*` like `pyright` does
(https://github.com/astral-sh/ruff/pull/16446#discussion_r1976900643).

Storing the range in the `ExceptClauseKind::Star` variant feels slightly
awkward, but we don't store the star itself anywhere on the
`ExceptHandler`. And we can't just take `ExceptHandler.start() +
"except".text_len()` because this code appears to be valid:

```python
try: ...
except    *    Error: ...
```

Test Plan
--
Existing tests.
This commit is contained in:
Brent Westbrook 2025-03-04 11:50:09 -05:00 committed by GitHub
parent 1977dda079
commit c8a06a9be8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 94 additions and 16 deletions

View file

@ -7,11 +7,11 @@ input_file: crates/ruff_python_parser/resources/inline/err/except_star_py310.py
```
Module(
ModModule {
range: 0..77,
range: 0..126,
body: [
Try(
StmtTry {
range: 44..76,
range: 44..125,
body: [
Expr(
StmtExpr {
@ -52,6 +52,60 @@ Module(
],
},
),
ExceptHandler(
ExceptHandlerExceptHandler {
range: 77..98,
type_: Some(
Name(
ExprName {
range: 85..93,
id: Name("KeyError"),
ctx: Load,
},
),
),
name: None,
body: [
Expr(
StmtExpr {
range: 95..98,
value: EllipsisLiteral(
ExprEllipsisLiteral {
range: 95..98,
},
),
},
),
],
},
),
ExceptHandler(
ExceptHandlerExceptHandler {
range: 99..125,
type_: Some(
Name(
ExprName {
range: 115..120,
id: Name("Error"),
ctx: Load,
},
),
),
name: None,
body: [
Expr(
StmtExpr {
range: 122..125,
value: EllipsisLiteral(
ExprEllipsisLiteral {
range: 122..125,
},
),
},
),
],
},
),
],
orelse: [],
finalbody: [],
@ -65,8 +119,27 @@ Module(
## Unsupported Syntax Errors
|
1 | # parse_options: {"target-version": "3.10"}
2 | / try: ...
3 | | except* ValueError: ...
| |_______________________^ Syntax Error: Cannot use `except*` on Python 3.10 (syntax was added in Python 3.11)
1 | # parse_options: {"target-version": "3.10"}
2 | try: ...
3 | except* ValueError: ...
| ^ Syntax Error: Cannot use `except*` on Python 3.10 (syntax was added in Python 3.11)
4 | except* KeyError: ...
5 | except * Error: ...
|
|
2 | try: ...
3 | except* ValueError: ...
4 | except* KeyError: ...
| ^ Syntax Error: Cannot use `except*` on Python 3.10 (syntax was added in Python 3.11)
5 | except * Error: ...
|
|
3 | except* ValueError: ...
4 | except* KeyError: ...
5 | except * Error: ...
| ^ Syntax Error: Cannot use `except*` on Python 3.10 (syntax was added in Python 3.11)
|