Skip to content

Commit

Permalink
Update ERA100 to apply to commented dictionary items with trailing co…
Browse files Browse the repository at this point in the history
…mments (#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.
  • Loading branch information
zanieb authored Aug 25, 2023
1 parent 29a0c10 commit f2eb7bc
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 1 deletion.
9 changes: 9 additions & 0 deletions crates/ruff/resources/test/fixtures/eradicate/ERA001.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,12 @@ def foo(x, y, z):
class A():
pass
# b = c


dictionary = {
# "key1": 123, # noqa: ERA001
# "key2": 456,
# "key3": 789, # test
}

#import os # noqa
11 changes: 11 additions & 0 deletions crates/ruff/resources/test/fixtures/ruff/RUF100_5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#import os # noqa
#import os # noqa: ERA001

dictionary = {
# "key1": 123, # noqa: ERA001
# "key2": 456, # noqa
# "key3": 789,
}


#import os # noqa: E501
2 changes: 1 addition & 1 deletion crates/ruff/src/rules/eradicate/detection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ static HASH_NUMBER: Lazy<Regex> = Lazy::new(|| Regex::new(r"#\d").unwrap());
static MULTILINE_ASSIGNMENT_REGEX: Lazy<Regex> =
Lazy::new(|| Regex::new(r"^\s*([(\[]\s*)?(\w+\s*,\s*)*\w+\s*([)\]]\s*)?=.*[(\[{]$").unwrap());
static PARTIAL_DICTIONARY_REGEX: Lazy<Regex> =
Lazy::new(|| Regex::new(r#"^\s*['"]\w+['"]\s*:.+[,{]\s*$"#).unwrap());
Lazy::new(|| Regex::new(r#"^\s*['"]\w+['"]\s*:.+[,{]\s*(#.*)?$"#).unwrap());
static PRINT_RETURN_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"^(print|return)\b\s*").unwrap());

/// Returns `true` if a comment contains Python code.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,47 @@ ERA001.py:21:5: ERA001 [*] Found commented-out code
19 19 | class A():
20 20 | pass
21 |- # b = c
22 21 |
23 22 |
24 23 | dictionary = {

ERA001.py:26:5: ERA001 [*] Found commented-out code
|
24 | dictionary = {
25 | # "key1": 123, # noqa: ERA001
26 | # "key2": 456,
| ^^^^^^^^^^^^^^ ERA001
27 | # "key3": 789, # test
28 | }
|
= help: Remove commented-out code

Possible fix
23 23 |
24 24 | dictionary = {
25 25 | # "key1": 123, # noqa: ERA001
26 |- # "key2": 456,
27 26 | # "key3": 789, # test
28 27 | }
29 28 |

ERA001.py:27:5: ERA001 [*] Found commented-out code
|
25 | # "key1": 123, # noqa: ERA001
26 | # "key2": 456,
27 | # "key3": 789, # test
| ^^^^^^^^^^^^^^^^^^^^^^ ERA001
28 | }
|
= help: Remove commented-out code

Possible fix
24 24 | dictionary = {
25 25 | # "key1": 123, # noqa: ERA001
26 26 | # "key2": 456,
27 |- # "key3": 789, # test
28 27 | }
29 28 |
30 29 | #import os # noqa


15 changes: 15 additions & 0 deletions crates/ruff/src/rules/ruff/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,21 @@ mod tests {
Ok(())
}

#[test]
fn ruf100_5() -> Result<()> {
let diagnostics = test_path(
Path::new("ruff/RUF100_5.py"),
&settings::Settings {
..settings::Settings::for_rules(vec![
Rule::UnusedNOQA,
Rule::LineTooLong,
Rule::CommentedOutCode,
])
},
)?;
assert_messages!(diagnostics);
Ok(())
}
#[test]
fn flake8_noqa() -> Result<()> {
let diagnostics = test_path(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
---
source: crates/ruff/src/rules/ruff/mod.rs
---
RUF100_5.py:7:5: ERA001 [*] Found commented-out code
|
5 | # "key1": 123, # noqa: ERA001
6 | # "key2": 456, # noqa
7 | # "key3": 789,
| ^^^^^^^^^^^^^^ ERA001
8 | }
|
= help: Remove commented-out code

Possible fix
4 4 | dictionary = {
5 5 | # "key1": 123, # noqa: ERA001
6 6 | # "key2": 456, # noqa
7 |- # "key3": 789,
8 7 | }
9 8 |
10 9 |

RUF100_5.py:11:1: ERA001 [*] Found commented-out code
|
11 | #import os # noqa: E501
| ^^^^^^^^^^^^^^^^^^^^^^^^ ERA001
|
= help: Remove commented-out code

Possible fix
8 8 | }
9 9 |
10 10 |
11 |-#import os # noqa: E501

RUF100_5.py:11:13: RUF100 [*] Unused `noqa` directive (unused: `E501`)
|
11 | #import os # noqa: E501
| ^^^^^^^^^^^^ RUF100
|
= help: Remove unused `noqa` directive

Suggested fix
8 8 | }
9 9 |
10 10 |
11 |-#import os # noqa: E501
11 |+#import os


0 comments on commit f2eb7bc

Please sign in to comment.