Modify parsing of raise with cause when exception is absent (#15049)

When confronted with `raise from exc` the parser will now create a
`StmtRaise` that has `None` for the exception and `exc` for the cause.

Before, the parser created a `StmtRaise` with `from` for the exception,
no cause, and a spurious expression `exc` afterwards.
This commit is contained in:
Dylan 2024-12-19 07:36:32 -06:00 committed by GitHub
parent 3bb0dac235
commit c1eaf6ff72
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 93 additions and 21 deletions

View file

@ -0,0 +1,57 @@
---
source: crates/ruff_python_parser/tests/fixtures.rs
input_file: crates/ruff_python_parser/resources/inline/err/raise_stmt_from_without_exc.py
---
## AST
```
Module(
ModModule {
range: 0..31,
body: [
Raise(
StmtRaise {
range: 0..14,
exc: None,
cause: Some(
Name(
ExprName {
range: 11..14,
id: Name("exc"),
ctx: Load,
},
),
),
},
),
Raise(
StmtRaise {
range: 15..30,
exc: None,
cause: Some(
NoneLiteral(
ExprNoneLiteral {
range: 26..30,
},
),
),
},
),
],
},
)
```
## Errors
|
1 | raise from exc
| ^^^^ Syntax Error: Exception missing in `raise` statement with cause
2 | raise from None
|
|
1 | raise from exc
2 | raise from None
| ^^^^ Syntax Error: Exception missing in `raise` statement with cause
|