[pygrep-hooks]: Detect file-level suppressions comments without rul… (#16720)

## Summary

I accidentially dropped this commit from the Ruff 0.10 release. See
https://github.com/astral-sh/ruff/pull/16699
This commit is contained in:
Micha Reiser 2025-03-14 09:37:16 +01:00 committed by GitHub
parent 595565015b
commit 14c5ed5d7d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 24 additions and 65 deletions

View file

@ -224,7 +224,6 @@ pub(crate) fn check_noqa(
&noqa_directives, &noqa_directives,
locator, locator,
&file_noqa_directives, &file_noqa_directives,
settings.preview,
); );
} }

View file

@ -10,7 +10,6 @@ mod tests {
use crate::registry::Rule; use crate::registry::Rule;
use crate::settings::types::PreviewMode;
use crate::test::test_path; use crate::test::test_path;
use crate::{assert_messages, settings}; use crate::{assert_messages, settings};
@ -30,22 +29,4 @@ mod tests {
assert_messages!(snapshot, diagnostics); assert_messages!(snapshot, diagnostics);
Ok(()) Ok(())
} }
#[test_case(Rule::BlanketNOQA, Path::new("PGH004_2.py"))]
fn preview_rules(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!(
"preview__{}_{}",
rule_code.noqa_code(),
path.to_string_lossy()
);
let diagnostics = test_path(
Path::new("pygrep_hooks").join(path).as_path(),
&settings::LinterSettings {
preview: PreviewMode::Enabled,
..settings::LinterSettings::for_rule(rule_code)
},
)?;
assert_messages!(snapshot, diagnostics);
Ok(())
}
} }

View file

@ -4,7 +4,6 @@ use ruff_python_trivia::Cursor;
use ruff_text_size::{Ranged, TextRange}; use ruff_text_size::{Ranged, TextRange};
use crate::noqa::{self, Directive, FileNoqaDirectives, NoqaDirectives}; use crate::noqa::{self, Directive, FileNoqaDirectives, NoqaDirectives};
use crate::settings::types::PreviewMode;
use crate::Locator; use crate::Locator;
/// ## What it does /// ## What it does
@ -18,9 +17,6 @@ use crate::Locator;
/// maintain, as the annotation does not clarify which diagnostics are intended /// maintain, as the annotation does not clarify which diagnostics are intended
/// to be suppressed. /// to be suppressed.
/// ///
/// In [preview], this rule also checks for blanket file-level annotations (e.g.,
/// `# ruff: noqa`, as opposed to `# ruff: noqa: F401`).
///
/// ## Example /// ## Example
/// ```python /// ```python
/// from .base import * # noqa /// from .base import * # noqa
@ -41,8 +37,6 @@ use crate::Locator;
/// ///
/// ## References /// ## References
/// - [Ruff documentation](https://docs.astral.sh/ruff/configuration/#error-suppression) /// - [Ruff documentation](https://docs.astral.sh/ruff/configuration/#error-suppression)
///
/// [preview]: https://docs.astral.sh/ruff/preview/
#[derive(ViolationMetadata)] #[derive(ViolationMetadata)]
pub(crate) struct BlanketNOQA { pub(crate) struct BlanketNOQA {
missing_colon: bool, missing_colon: bool,
@ -84,19 +78,16 @@ pub(crate) fn blanket_noqa(
noqa_directives: &NoqaDirectives, noqa_directives: &NoqaDirectives,
locator: &Locator, locator: &Locator,
file_noqa_directives: &FileNoqaDirectives, file_noqa_directives: &FileNoqaDirectives,
preview: PreviewMode,
) { ) {
if preview.is_enabled() { for line in file_noqa_directives.lines() {
for line in file_noqa_directives.lines() { if let Directive::All(_) = line.parsed_file_exemption {
if let Directive::All(_) = line.parsed_file_exemption { diagnostics.push(Diagnostic::new(
diagnostics.push(Diagnostic::new( BlanketNOQA {
BlanketNOQA { missing_colon: false,
missing_colon: false, file_exemption: true,
file_exemption: true, },
}, line.range(),
line.range(), ));
));
}
} }
} }

View file

@ -1,6 +1,5 @@
--- ---
source: crates/ruff_linter/src/rules/pygrep_hooks/mod.rs source: crates/ruff_linter/src/rules/pygrep_hooks/mod.rs
snapshot_kind: text
--- ---
PGH004_2.py:1:1: PGH004 Use specific rule codes when using `noqa` PGH004_2.py:1:1: PGH004 Use specific rule codes when using `noqa`
| |
@ -9,3 +8,18 @@ PGH004_2.py:1:1: PGH004 Use specific rule codes when using `noqa`
2 | # ruff : noqa 2 | # ruff : noqa
3 | # ruff: noqa: F401 3 | # ruff: noqa: F401
| |
PGH004_2.py:2:1: PGH004 Use specific rule codes when using `ruff: noqa`
|
1 | # noqa
2 | # ruff : noqa
| ^^^^^^^^^^^^^ PGH004
3 | # ruff: noqa: F401
|
PGH004_2.py:6:1: PGH004 Use specific rule codes when using `ruff: noqa`
|
6 | # flake8: noqa
| ^^^^^^^^^^^^^^ PGH004
7 | import math as m
|

View file

@ -1,26 +0,0 @@
---
source: crates/ruff_linter/src/rules/pygrep_hooks/mod.rs
snapshot_kind: text
---
PGH004_2.py:1:1: PGH004 Use specific rule codes when using `noqa`
|
1 | # noqa
| ^^^^^^ PGH004
2 | # ruff : noqa
3 | # ruff: noqa: F401
|
PGH004_2.py:2:1: PGH004 Use specific rule codes when using `ruff: noqa`
|
1 | # noqa
2 | # ruff : noqa
| ^^^^^^^^^^^^^ PGH004
3 | # ruff: noqa: F401
|
PGH004_2.py:6:1: PGH004 Use specific rule codes when using `ruff: noqa`
|
6 | # flake8: noqa
| ^^^^^^^^^^^^^^ PGH004
7 | import math as m
|