-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow passing
ParseOptions
to inline tests (#16357)
## Summary This PR adds support for a pragma-style header for inline parser tests containing JSON-serialized `ParseOptions`. For example, ```python # parse_options: { "target-version": "3.9" } match 2: case 1: pass ``` The line must start with `# parse_options: ` and then the rest of the (trimmed) line is deserialized into `ParseOptions` used for parsing the the test. ## Test Plan Existing inline tests, plus two new inline tests for `match-before-py310`. --------- Co-authored-by: Alex Waygood <alex.waygood@gmail.com>
- Loading branch information
1 parent
568cf88
commit 764aa0e
Showing
8 changed files
with
238 additions
and
7 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
4 changes: 4 additions & 0 deletions
4
crates/ruff_python_parser/resources/inline/err/match_before_py310.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,4 @@ | ||
# parse_options: { "target-version": "3.9" } | ||
match 2: | ||
case 1: | ||
pass |
4 changes: 4 additions & 0 deletions
4
crates/ruff_python_parser/resources/inline/ok/match_after_py310.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,4 @@ | ||
# parse_options: { "target-version": "3.10" } | ||
match 2: | ||
case 1: | ||
pass |
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
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
63 changes: 63 additions & 0 deletions
63
crates/ruff_python_parser/tests/snapshots/invalid_syntax@match_before_py310.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,63 @@ | ||
--- | ||
source: crates/ruff_python_parser/tests/fixtures.rs | ||
input_file: crates/ruff_python_parser/resources/inline/err/match_before_py310.py | ||
--- | ||
## AST | ||
|
||
``` | ||
Module( | ||
ModModule { | ||
range: 0..79, | ||
body: [ | ||
Match( | ||
StmtMatch { | ||
range: 45..78, | ||
subject: NumberLiteral( | ||
ExprNumberLiteral { | ||
range: 51..52, | ||
value: Int( | ||
2, | ||
), | ||
}, | ||
), | ||
cases: [ | ||
MatchCase { | ||
range: 58..78, | ||
pattern: MatchValue( | ||
PatternMatchValue { | ||
range: 63..64, | ||
value: NumberLiteral( | ||
ExprNumberLiteral { | ||
range: 63..64, | ||
value: Int( | ||
1, | ||
), | ||
}, | ||
), | ||
}, | ||
), | ||
guard: None, | ||
body: [ | ||
Pass( | ||
StmtPass { | ||
range: 74..78, | ||
}, | ||
), | ||
], | ||
}, | ||
], | ||
}, | ||
), | ||
], | ||
}, | ||
) | ||
``` | ||
## Unsupported Syntax Errors | ||
|
||
| | ||
1 | # parse_options: { "target-version": "3.9" } | ||
2 | match 2: | ||
| ^^^^^ Syntax Error: Cannot use `match` statement on Python 3.9 (syntax was added in Python 3.10) | ||
3 | case 1: | ||
4 | pass | ||
| |
54 changes: 54 additions & 0 deletions
54
crates/ruff_python_parser/tests/snapshots/valid_syntax@match_after_py310.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,54 @@ | ||
--- | ||
source: crates/ruff_python_parser/tests/fixtures.rs | ||
input_file: crates/ruff_python_parser/resources/inline/ok/match_after_py310.py | ||
--- | ||
## AST | ||
|
||
``` | ||
Module( | ||
ModModule { | ||
range: 0..80, | ||
body: [ | ||
Match( | ||
StmtMatch { | ||
range: 46..79, | ||
subject: NumberLiteral( | ||
ExprNumberLiteral { | ||
range: 52..53, | ||
value: Int( | ||
2, | ||
), | ||
}, | ||
), | ||
cases: [ | ||
MatchCase { | ||
range: 59..79, | ||
pattern: MatchValue( | ||
PatternMatchValue { | ||
range: 64..65, | ||
value: NumberLiteral( | ||
ExprNumberLiteral { | ||
range: 64..65, | ||
value: Int( | ||
1, | ||
), | ||
}, | ||
), | ||
}, | ||
), | ||
guard: None, | ||
body: [ | ||
Pass( | ||
StmtPass { | ||
range: 75..79, | ||
}, | ||
), | ||
], | ||
}, | ||
], | ||
}, | ||
), | ||
], | ||
}, | ||
) | ||
``` |