Skip to content

Commit f2eb7bc

Browse files
authored
Update ERA100 to apply to commented dictionary items with trailing comments (#6822)
Closes #6821 ERA100 was not raising on commented parts of dictionaries if it included another comment (such as a noqa clause). In cases where this comment was a noqa clause, RUF100 to be emitted since the noqa would have no effect. Here, we update ERA100 to raise even when there are trailing comments. This resolves the linked issue _and_ increases the scope of ERA100. We could narrow the regular expression to only apply to noqa comments if we do not want to expand ERA100 however I think this change is within the spirit of the rule.
1 parent 29a0c10 commit f2eb7bc

File tree

6 files changed

+128
-1
lines changed

6 files changed

+128
-1
lines changed

crates/ruff/resources/test/fixtures/eradicate/ERA001.py

+9
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,12 @@ def foo(x, y, z):
1919
class A():
2020
pass
2121
# b = c
22+
23+
24+
dictionary = {
25+
# "key1": 123, # noqa: ERA001
26+
# "key2": 456,
27+
# "key3": 789, # test
28+
}
29+
30+
#import os # noqa
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#import os # noqa
2+
#import os # noqa: ERA001
3+
4+
dictionary = {
5+
# "key1": 123, # noqa: ERA001
6+
# "key2": 456, # noqa
7+
# "key3": 789,
8+
}
9+
10+
11+
#import os # noqa: E501

crates/ruff/src/rules/eradicate/detection.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ static HASH_NUMBER: Lazy<Regex> = Lazy::new(|| Regex::new(r"#\d").unwrap());
2828
static MULTILINE_ASSIGNMENT_REGEX: Lazy<Regex> =
2929
Lazy::new(|| Regex::new(r"^\s*([(\[]\s*)?(\w+\s*,\s*)*\w+\s*([)\]]\s*)?=.*[(\[{]$").unwrap());
3030
static PARTIAL_DICTIONARY_REGEX: Lazy<Regex> =
31-
Lazy::new(|| Regex::new(r#"^\s*['"]\w+['"]\s*:.+[,{]\s*$"#).unwrap());
31+
Lazy::new(|| Regex::new(r#"^\s*['"]\w+['"]\s*:.+[,{]\s*(#.*)?$"#).unwrap());
3232
static PRINT_RETURN_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"^(print|return)\b\s*").unwrap());
3333

3434
/// Returns `true` if a comment contains Python code.

crates/ruff/src/rules/eradicate/snapshots/ruff__rules__eradicate__tests__ERA001_ERA001.py.snap

+42
Original file line numberDiff line numberDiff line change
@@ -105,5 +105,47 @@ ERA001.py:21:5: ERA001 [*] Found commented-out code
105105
19 19 | class A():
106106
20 20 | pass
107107
21 |- # b = c
108+
22 21 |
109+
23 22 |
110+
24 23 | dictionary = {
111+
112+
ERA001.py:26:5: ERA001 [*] Found commented-out code
113+
|
114+
24 | dictionary = {
115+
25 | # "key1": 123, # noqa: ERA001
116+
26 | # "key2": 456,
117+
| ^^^^^^^^^^^^^^ ERA001
118+
27 | # "key3": 789, # test
119+
28 | }
120+
|
121+
= help: Remove commented-out code
122+
123+
Possible fix
124+
23 23 |
125+
24 24 | dictionary = {
126+
25 25 | # "key1": 123, # noqa: ERA001
127+
26 |- # "key2": 456,
128+
27 26 | # "key3": 789, # test
129+
28 27 | }
130+
29 28 |
131+
132+
ERA001.py:27:5: ERA001 [*] Found commented-out code
133+
|
134+
25 | # "key1": 123, # noqa: ERA001
135+
26 | # "key2": 456,
136+
27 | # "key3": 789, # test
137+
| ^^^^^^^^^^^^^^^^^^^^^^ ERA001
138+
28 | }
139+
|
140+
= help: Remove commented-out code
141+
142+
Possible fix
143+
24 24 | dictionary = {
144+
25 25 | # "key1": 123, # noqa: ERA001
145+
26 26 | # "key2": 456,
146+
27 |- # "key3": 789, # test
147+
28 27 | }
148+
29 28 |
149+
30 29 | #import os # noqa
108150

109151

crates/ruff/src/rules/ruff/mod.rs

+15
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,21 @@ mod tests {
165165
Ok(())
166166
}
167167

168+
#[test]
169+
fn ruf100_5() -> Result<()> {
170+
let diagnostics = test_path(
171+
Path::new("ruff/RUF100_5.py"),
172+
&settings::Settings {
173+
..settings::Settings::for_rules(vec![
174+
Rule::UnusedNOQA,
175+
Rule::LineTooLong,
176+
Rule::CommentedOutCode,
177+
])
178+
},
179+
)?;
180+
assert_messages!(diagnostics);
181+
Ok(())
182+
}
168183
#[test]
169184
fn flake8_noqa() -> Result<()> {
170185
let diagnostics = test_path(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
source: crates/ruff/src/rules/ruff/mod.rs
3+
---
4+
RUF100_5.py:7:5: ERA001 [*] Found commented-out code
5+
|
6+
5 | # "key1": 123, # noqa: ERA001
7+
6 | # "key2": 456, # noqa
8+
7 | # "key3": 789,
9+
| ^^^^^^^^^^^^^^ ERA001
10+
8 | }
11+
|
12+
= help: Remove commented-out code
13+
14+
Possible fix
15+
4 4 | dictionary = {
16+
5 5 | # "key1": 123, # noqa: ERA001
17+
6 6 | # "key2": 456, # noqa
18+
7 |- # "key3": 789,
19+
8 7 | }
20+
9 8 |
21+
10 9 |
22+
23+
RUF100_5.py:11:1: ERA001 [*] Found commented-out code
24+
|
25+
11 | #import os # noqa: E501
26+
| ^^^^^^^^^^^^^^^^^^^^^^^^ ERA001
27+
|
28+
= help: Remove commented-out code
29+
30+
Possible fix
31+
8 8 | }
32+
9 9 |
33+
10 10 |
34+
11 |-#import os # noqa: E501
35+
36+
RUF100_5.py:11:13: RUF100 [*] Unused `noqa` directive (unused: `E501`)
37+
|
38+
11 | #import os # noqa: E501
39+
| ^^^^^^^^^^^^ RUF100
40+
|
41+
= help: Remove unused `noqa` directive
42+
43+
Suggested fix
44+
8 8 | }
45+
9 9 |
46+
10 10 |
47+
11 |-#import os # noqa: E501
48+
11 |+#import os
49+
50+

0 commit comments

Comments
 (0)