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

Allow passing ParseOptions to inline tests #16357

Open
wants to merge 70 commits into
base: main
Choose a base branch
from
Open

Conversation

ntBre
Copy link
Contributor

@ntBre ntBre commented Feb 24, 2025

Summary

The changes here are based on the similar behavior in biome's collect_tests function, which allows a syntax for inline test headers like

label language name [options]

Before this PR, we only allowed headers of the form label name, where label is either test_err or test_ok and name is the name of the test. This PR adds support for an optional, trailing options field, corresponding to JSON-serialized ParseOptions. These get written to a *.options.json file alongside the inline test script and read when that test is run.

This is currently stacked on #16090 so that I had something to test.

I wanted to add serde as a dev dependency only, but since these are run as integration tests, I had to add it as a full dependency. For this exact use case, we could just parse a trailing version string like "3.10" but I thought we might want the full ParseOptions for the future.

Test Plan

Existing inline tests, plus two new inline tests for match-before-py310.

ntBre and others added 30 commits February 24, 2025 08:48
I initially tried passing the target version to all of the parser functions, but
this required a huge number of changes. The downside of the current approach is
that we're likely to accumulate quite a large Vec<SyntaxError> in the parser,
only to filter it down later. The upside is that we don't have to fix every
single call site of every parser function right now
could be Copy as well, currently because Mode and PythonVersion are
This reverts commit 3f43c82.

This didn't work because the fuzzing only runs with cfg(test) not cfg(fuzzing)
@ntBre ntBre force-pushed the brent/parser-tests branch from d2950d6 to ef8c37e Compare February 25, 2025 18:03
Copy link

codspeed-hq bot commented Feb 25, 2025

CodSpeed Performance Report

Merging #16357 will not alter performance

Comparing brent/parser-tests (62b43a7) with main (dd6f623)

Summary

✅ 32 untouched benchmarks

@ntBre
Copy link
Contributor Author

ntBre commented Feb 25, 2025

I rebased on #16090 to get the new SyntaxError name and switched to the pragma implementation. It seems quite nice and ended up being less code too. The only piece I might be missing is including the code file contents in the snapshot, but I wasn't quite sure if that was still needed. It sounds like that could be a follow-up either way.

I made serde an optional dependency gated behind the serde feature in the parser. You have to pass --features serde to run the parser tests with something like cargo test -p ruff_python_parser --features serde, but I think CI runs everything with --all-features. This feels a bit hacky but maybe it's okay? I left that commit last so it would be easy to revert.

@ntBre
Copy link
Contributor Author

ntBre commented Feb 25, 2025

My other branch must be missing Doug's alist changes, at least I assume that's where the codspeed failure is coming from. I expect that to go away when I change the base branch to main.

ntBre added a commit that referenced this pull request Feb 25, 2025
This PR is the first in a series derived from #16308, each of which add support
for detecting one version-related syntax error from #6591. This one should be
the largest because it also includes a couple of additional changes:
1. the `syntax_errors!` macro, which makes adding more variants a bit easier
2. the `Parser::add_unsupported_syntax_error` method

Otherwise I think the general structure will be the same for each syntax error:
* Detecting the error in the parser
* Inline parser tests for the new error
* New ruff CLI tests for the new error

Because of the second point here, this PR is currently stacked on #16357.

As noted above, there are new inline parser tests, as well as new ruff CLI
tests. Once #16379 is resolved, there should also be new mdtests for red-knot,
but this PR does not currently include those.
@ntBre
Copy link
Contributor Author

ntBre commented Feb 26, 2025

While trying to use this branch as a base for detecting some syntax errors, I realized that I forgot to actually check for the new errors in the valid syntax case. It's difficult to test this case because it requires a version-related syntax error in a test_ok inline test. I guess a #[should_panic] test calling test_valid_syntax directly could work, but I can also confirm that these changes now detect the false-positive I was debugging on the other branch.

ntBre added a commit that referenced this pull request Feb 26, 2025
This PR is the first in a series derived from #16308, each of which add support
for detecting one version-related syntax error from #6591. This one should be
the largest because it also includes a couple of additional changes:
1. the `syntax_errors!` macro, which makes adding more variants a bit easier
2. the `Parser::add_unsupported_syntax_error` method

Otherwise I think the general structure will be the same for each syntax error:
* Detecting the error in the parser
* Inline parser tests for the new error
* New ruff CLI tests for the new error

Because of the second point here, this PR is currently stacked on #16357.

As noted above, there are new inline parser tests, as well as new ruff CLI
tests. Once #16379 is resolved, there should also be new mdtests for red-knot,
but this PR does not currently include those.
ntBre added a commit that referenced this pull request Feb 26, 2025
## Summary

This PR builds on the changes in #16220 to pass a target Python version
to the parser. It also adds the `Parser::unsupported_syntax_errors` field, which
collects version-related syntax errors while parsing. These syntax
errors are then turned into `Message`s in ruff (in preview mode).

This PR only detects one syntax error (`match` statement before Python
3.10), but it has been pretty quick to extend to several other simple
errors (see #16308 for example).

## Test Plan

The current tests are CLI tests in the linter crate, but these could be
supplemented with inline parser tests after #16357.

I also tested the display of these syntax errors in VS Code:


![image](https://github.com/user-attachments/assets/062b4441-740e-46c3-887c-a954049ef26e)

![image](https://github.com/user-attachments/assets/101f55b8-146c-4d59-b6b0-922f19bcd0fa)

---------

Co-authored-by: Alex Waygood <[email protected]>
Base automatically changed from brent/syntax-errors-parser to main February 26, 2025 04:03
@ntBre ntBre requested a review from AlexWaygood as a code owner February 26, 2025 04:03
@ntBre
Copy link
Contributor Author

ntBre commented Feb 26, 2025

Based on a discussion with @MichaReiser on Discord, I backed out the serde feature changes and added JsonParseOptions and JsonMode types in fixtures.rs to keep serde as a dev-dependency.

This avoids needing to run the parser integration tests with the serde feature enabled or requiring serde as a full dependency.

ntBre added a commit that referenced this pull request Feb 26, 2025
This PR is the first in a series derived from #16308, each of which add support
for detecting one version-related syntax error from #6591. This one should be
the largest because it also includes a couple of additional changes:
1. the `syntax_errors!` macro, which makes adding more variants a bit easier
2. the `Parser::add_unsupported_syntax_error` method

Otherwise I think the general structure will be the same for each syntax error:
* Detecting the error in the parser
* Inline parser tests for the new error
* New ruff CLI tests for the new error

Because of the second point here, this PR is currently stacked on #16357.

As noted above, there are new inline parser tests, as well as new ruff CLI
tests. Once #16379 is resolved, there should also be new mdtests for red-knot,
but this PR does not currently include those.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
internal An internal refactor or improvement parser Related to the parser
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants