-
Notifications
You must be signed in to change notification settings - Fork 1.6k
apply range suppressions to filter diagnostics #21623
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
Open
amyreese
wants to merge
11
commits into
main
Choose a base branch
from
amy/suppress-diagnostics
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+724
−4
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ee58f87
Prototype filtering diagnostics by range suppressions
amyreese dd5d45c
Refactor suppressions checking into check_noqa
amyreese df36084
Short circuit if no suppressions loaded
amyreese 67be3d9
Add test covering expected diagnostics from range suppressions
amyreese 36fbd22
Gate suppression parsing/checking on `--preview`
amyreese 7723b63
english
amyreese 2f57e20
More integration test cases
amyreese 5ebadd4
Added cli tests (currently failing)
amyreese fe83037
Load and pass suppressions through when adding noqa comments
amyreese 2100298
Update comment
amyreese 8f104fa
Add is_empty to Suppressions, don't abort early in check_noqa
amyreese File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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
56 changes: 56 additions & 0 deletions
56
crates/ruff_linter/resources/test/fixtures/ruff/suppressions.py
This file contains hidden or 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,56 @@ | ||
| def f(): | ||
| # These should both be ignored by the range suppression. | ||
| # ruff: disable[E741, F841] | ||
| I = 1 | ||
| # ruff: enable[E741, F841] | ||
|
|
||
|
|
||
| def f(): | ||
| # These should both be ignored by the implicit range suppression. | ||
| # Should also generate an "unmatched suppression" warning. | ||
| # ruff:disable[E741,F841] | ||
| I = 1 | ||
|
|
||
|
|
||
| def f(): | ||
| # Neither warning is ignored, and an "unmatched suppression" | ||
| # should be generated. | ||
| I = 1 | ||
| # ruff: enable[E741, F841] | ||
|
|
||
|
|
||
| def f(): | ||
| # One should be ignored by the range suppression, and | ||
| # the other logged to the user. | ||
| # ruff: disable[E741] | ||
| I = 1 | ||
| # ruff: enable[E741] | ||
|
|
||
|
|
||
| def f(): | ||
| # Test interleaved range suppressions. The first and last | ||
| # lines should each log a different warning, while the | ||
| # middle line should be completely silenced. | ||
| # ruff: disable[E741] | ||
| l = 0 | ||
| # ruff: disable[F841] | ||
| O = 1 | ||
| # ruff: enable[E741] | ||
| I = 2 | ||
| # ruff: enable[F841] | ||
|
|
||
|
|
||
| def f(): | ||
| # Neither of these are ignored and warnings are | ||
| # logged to user | ||
| # ruff: disable[E501] | ||
| I = 1 | ||
| # ruff: enable[E501] | ||
|
|
||
|
|
||
| def f(): | ||
| # These should both be ignored by the range suppression, | ||
| # and an unusued noqa diagnostic should be logged. | ||
| # ruff:disable[E741,F841] | ||
| I = 1 # noqa: E741,F841 | ||
| # ruff:enable[E741,F841] |
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -26,12 +26,14 @@ use crate::doc_lines::{doc_lines_from_ast, doc_lines_from_tokens}; | |
| use crate::fix::{FixResult, fix_file}; | ||
| use crate::noqa::add_noqa; | ||
| use crate::package::PackageRoot; | ||
| use crate::preview::is_range_suppressions_enabled; | ||
| use crate::registry::Rule; | ||
| #[cfg(any(feature = "test-rules", test))] | ||
| use crate::rules::ruff::rules::test_rules::{self, TEST_RULES, TestRule}; | ||
| use crate::settings::types::UnsafeFixes; | ||
| use crate::settings::{LinterSettings, TargetVersion, flags}; | ||
| use crate::source_kind::SourceKind; | ||
| use crate::suppression::Suppressions; | ||
| use crate::{Locator, directives, fs}; | ||
|
|
||
| pub(crate) mod float; | ||
|
|
@@ -331,6 +333,11 @@ pub fn check_path( | |
| .iter_enabled_rules() | ||
| .any(|rule_code| rule_code.lint_source().is_noqa()) | ||
| { | ||
| let suppressions = if is_range_suppressions_enabled(settings) { | ||
| Suppressions::from_tokens(locator.contents(), tokens) | ||
| } else { | ||
| Suppressions::default() | ||
| }; | ||
| let ignored = check_noqa( | ||
| &mut context, | ||
| path, | ||
|
|
@@ -339,6 +346,7 @@ pub fn check_path( | |
| &directives.noqa_line_for, | ||
| parsed.has_valid_syntax(), | ||
| settings, | ||
| &suppressions, | ||
| ); | ||
| if noqa.is_enabled() { | ||
| for index in ignored.iter().rev() { | ||
|
|
@@ -416,6 +424,12 @@ pub fn add_noqa_to_path( | |
| target_version, | ||
| ); | ||
|
|
||
| let suppressions = if is_range_suppressions_enabled(settings) { | ||
| Suppressions::from_tokens(locator.contents(), parsed.tokens()) | ||
| } else { | ||
| Suppressions::default() | ||
| }; | ||
|
|
||
|
Comment on lines
+427
to
+432
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We now end up extracting the suppressions twice: Once in |
||
| // Add any missing `# noqa` pragmas. | ||
| // TODO(dhruvmanila): Add support for Jupyter Notebooks | ||
| add_noqa( | ||
|
|
@@ -427,6 +441,7 @@ pub fn add_noqa_to_path( | |
| &directives.noqa_line_for, | ||
| stylist.line_ending(), | ||
| reason, | ||
| &suppressions, | ||
| ) | ||
| } | ||
|
|
||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We try to avoid reading fixture files in CLI tests as it makes the tests depend on each other and it can also become harder to reason about what we're testing here. Would it be possible to extract the specific case you want to test from
noqa.py?The tests here also don't need to be exhaustive. We can add more exhaustive tests to
add_noqa(I think we have some integration tests, right?)