-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
3 changed files
with
93 additions
and
21 deletions.
There are no files selected for viewing
2 changes: 2 additions & 0 deletions
2
crates/ruff_python_parser/resources/inline/err/raise_stmt_from_without_exc.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
raise from exc | ||
raise from None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
crates/ruff_python_parser/tests/snapshots/invalid_syntax@raise_stmt_from_without_exc.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
| |