Skip to content

Commit 72ffa12

Browse files
authored
[pygrep-hooks]: Detect file-level suppressions comments without rule codes (PGH004) (#16699)
## Summary This PR stabilizes the preview behavior of `PGH004` to also detect blanked file-level noqa comments (and not just line level comments). This preview behavior was first released with Ruff 0.4.8 (June 2025). There are no open issues or PRs open related to this behavior.
1 parent 15c0535 commit 72ffa12

File tree

5 files changed

+24
-65
lines changed

5 files changed

+24
-65
lines changed

crates/ruff_linter/src/checkers/noqa.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,6 @@ pub(crate) fn check_noqa(
224224
&noqa_directives,
225225
locator,
226226
&file_noqa_directives,
227-
settings.preview,
228227
);
229228
}
230229

crates/ruff_linter/src/rules/pygrep_hooks/mod.rs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ mod tests {
1010

1111
use crate::registry::Rule;
1212

13-
use crate::settings::types::PreviewMode;
1413
use crate::test::test_path;
1514
use crate::{assert_messages, settings};
1615

@@ -30,22 +29,4 @@ mod tests {
3029
assert_messages!(snapshot, diagnostics);
3130
Ok(())
3231
}
33-
34-
#[test_case(Rule::BlanketNOQA, Path::new("PGH004_2.py"))]
35-
fn preview_rules(rule_code: Rule, path: &Path) -> Result<()> {
36-
let snapshot = format!(
37-
"preview__{}_{}",
38-
rule_code.noqa_code(),
39-
path.to_string_lossy()
40-
);
41-
let diagnostics = test_path(
42-
Path::new("pygrep_hooks").join(path).as_path(),
43-
&settings::LinterSettings {
44-
preview: PreviewMode::Enabled,
45-
..settings::LinterSettings::for_rule(rule_code)
46-
},
47-
)?;
48-
assert_messages!(snapshot, diagnostics);
49-
Ok(())
50-
}
5132
}

crates/ruff_linter/src/rules/pygrep_hooks/rules/blanket_noqa.rs

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use ruff_python_trivia::Cursor;
44
use ruff_text_size::{Ranged, TextRange};
55

66
use crate::noqa::{self, Directive, FileNoqaDirectives, NoqaDirectives};
7-
use crate::settings::types::PreviewMode;
87
use crate::Locator;
98

109
/// ## What it does
@@ -18,9 +17,6 @@ use crate::Locator;
1817
/// maintain, as the annotation does not clarify which diagnostics are intended
1918
/// to be suppressed.
2019
///
21-
/// In [preview], this rule also checks for blanket file-level annotations (e.g.,
22-
/// `# ruff: noqa`, as opposed to `# ruff: noqa: F401`).
23-
///
2420
/// ## Example
2521
/// ```python
2622
/// from .base import * # noqa
@@ -41,8 +37,6 @@ use crate::Locator;
4137
///
4238
/// ## References
4339
/// - [Ruff documentation](https://docs.astral.sh/ruff/configuration/#error-suppression)
44-
///
45-
/// [preview]: https://docs.astral.sh/ruff/preview/
4640
#[derive(ViolationMetadata)]
4741
pub(crate) struct BlanketNOQA {
4842
missing_colon: bool,
@@ -84,19 +78,16 @@ pub(crate) fn blanket_noqa(
8478
noqa_directives: &NoqaDirectives,
8579
locator: &Locator,
8680
file_noqa_directives: &FileNoqaDirectives,
87-
preview: PreviewMode,
8881
) {
89-
if preview.is_enabled() {
90-
for line in file_noqa_directives.lines() {
91-
if let Directive::All(_) = line.parsed_file_exemption {
92-
diagnostics.push(Diagnostic::new(
93-
BlanketNOQA {
94-
missing_colon: false,
95-
file_exemption: true,
96-
},
97-
line.range(),
98-
));
99-
}
82+
for line in file_noqa_directives.lines() {
83+
if let Directive::All(_) = line.parsed_file_exemption {
84+
diagnostics.push(Diagnostic::new(
85+
BlanketNOQA {
86+
missing_colon: false,
87+
file_exemption: true,
88+
},
89+
line.range(),
90+
));
10091
}
10192
}
10293

Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
---
22
source: crates/ruff_linter/src/rules/pygrep_hooks/mod.rs
3-
snapshot_kind: text
43
---
54
PGH004_2.py:1:1: PGH004 Use specific rule codes when using `noqa`
65
|
@@ -9,3 +8,18 @@ PGH004_2.py:1:1: PGH004 Use specific rule codes when using `noqa`
98
2 | # ruff : noqa
109
3 | # ruff: noqa: F401
1110
|
11+
12+
PGH004_2.py:2:1: PGH004 Use specific rule codes when using `ruff: noqa`
13+
|
14+
1 | # noqa
15+
2 | # ruff : noqa
16+
| ^^^^^^^^^^^^^ PGH004
17+
3 | # ruff: noqa: F401
18+
|
19+
20+
PGH004_2.py:6:1: PGH004 Use specific rule codes when using `ruff: noqa`
21+
|
22+
6 | # flake8: noqa
23+
| ^^^^^^^^^^^^^^ PGH004
24+
7 | import math as m
25+
|

crates/ruff_linter/src/rules/pygrep_hooks/snapshots/ruff_linter__rules__pygrep_hooks__tests__preview__PGH004_PGH004_2.py.snap

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)