Skip to content

Commit

Permalink
Match rule prefixes from external codes setting in unused-noqa (#…
Browse files Browse the repository at this point in the history
…8177)

Supersedes #8176
Closes #8174

## Test plan

Old snapshot contains the new / unmatched `V` code
New snapshot contains no `V` prefixed codes
  • Loading branch information
zanieb authored Oct 24, 2023
1 parent a6cc56f commit 6f31e9c
Show file tree
Hide file tree
Showing 9 changed files with 426 additions and 115 deletions.
3 changes: 3 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/ruff/RUF100_0.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ def f() -> None:
# Invalid (but external)
d = 1 # noqa: F841, V101

# Invalid (but external)
d = 1 # noqa: V500

# fmt: off
# Invalid - no space before #
d = 1# noqa: E501
Expand Down
5 changes: 4 additions & 1 deletion crates/ruff_linter/src/checkers/noqa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,10 @@ pub(crate) fn check_noqa(
}

if line.matches.iter().any(|match_| *match_ == code)
|| settings.external.contains(code)
|| settings
.external
.iter()
.any(|external| code.starts_with(external))
{
valid_codes.push(code);
} else {
Expand Down
21 changes: 20 additions & 1 deletion crates/ruff_linter/src/rules/ruff/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,26 @@ mod tests {
let diagnostics = test_path(
Path::new("ruff/RUF100_0.py"),
&settings::LinterSettings {
external: FxHashSet::from_iter(vec!["V101".to_string()]),
external: vec!["V101".to_string()],
..settings::LinterSettings::for_rules(vec![
Rule::UnusedNOQA,
Rule::LineTooLong,
Rule::UnusedImport,
Rule::UnusedVariable,
Rule::TabIndentation,
])
},
)?;
assert_messages!(diagnostics);
Ok(())
}

#[test]
fn ruf100_0_prefix() -> Result<()> {
let diagnostics = test_path(
Path::new("ruff/RUF100_0.py"),
&settings::LinterSettings {
external: vec!["V".to_string()],
..settings::LinterSettings::for_rules(vec![
Rule::UnusedNOQA,
Rule::LineTooLong,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ RUF100_0.py:22:12: RUF100 [*] Unused `noqa` directive (unused: `F841`)
22 | d = 1 # noqa: F841, V101
| ^^^^^^^^^^^^^^^^^^ RUF100
23 |
24 | # fmt: off
24 | # Invalid (but external)
|
= help: Remove unused `noqa` directive

Expand All @@ -97,171 +97,191 @@ RUF100_0.py:22:12: RUF100 [*] Unused `noqa` directive (unused: `F841`)
22 |- d = 1 # noqa: F841, V101
22 |+ d = 1 # noqa: V101
23 23 |
24 24 | # fmt: off
25 25 | # Invalid - no space before #
24 24 | # Invalid (but external)
25 25 | d = 1 # noqa: V500

RUF100_0.py:26:10: RUF100 [*] Unused `noqa` directive (unused: `E501`)
RUF100_0.py:25:12: RUF100 [*] Unused `noqa` directive (unknown: `V500`)
|
24 | # fmt: off
25 | # Invalid - no space before #
26 | d = 1# noqa: E501
| ^^^^^^^^^^^^ RUF100
27 |
28 | # Invalid - many spaces before #
24 | # Invalid (but external)
25 | d = 1 # noqa: V500
| ^^^^^^^^^^^^ RUF100
26 |
27 | # fmt: off
|
= help: Remove unused `noqa` directive

Fix
22 22 | d = 1 # noqa: F841, V101
23 23 |
24 24 | # fmt: off
25 25 | # Invalid - no space before #
26 |- d = 1# noqa: E501
26 |+ d = 1
27 27 |
28 28 | # Invalid - many spaces before #
29 29 | d = 1 # noqa: E501
24 24 | # Invalid (but external)
25 |- d = 1 # noqa: V500
25 |+ d = 1
26 26 |
27 27 | # fmt: off
28 28 | # Invalid - no space before #

RUF100_0.py:29:10: RUF100 [*] Unused `noqa` directive (unused: `E501`)
|
27 | # fmt: off
28 | # Invalid - no space before #
29 | d = 1# noqa: E501
| ^^^^^^^^^^^^ RUF100
30 |
31 | # Invalid - many spaces before #
|
= help: Remove unused `noqa` directive

Fix
26 26 |
27 27 | # fmt: off
28 28 | # Invalid - no space before #
29 |- d = 1# noqa: E501
29 |+ d = 1
30 30 |
31 31 | # Invalid - many spaces before #
32 32 | d = 1 # noqa: E501

RUF100_0.py:29:5: F841 [*] Local variable `d` is assigned to but never used
RUF100_0.py:32:5: F841 [*] Local variable `d` is assigned to but never used
|
28 | # Invalid - many spaces before #
29 | d = 1 # noqa: E501
31 | # Invalid - many spaces before #
32 | d = 1 # noqa: E501
| ^ F841
30 | # fmt: on
33 | # fmt: on
|
= help: Remove assignment to unused variable `d`

Suggested fix
26 26 | d = 1# noqa: E501
27 27 |
28 28 | # Invalid - many spaces before #
29 |- d = 1 # noqa: E501
30 29 | # fmt: on
31 30 |
32 31 |
29 29 | d = 1# noqa: E501
30 30 |
31 31 | # Invalid - many spaces before #
32 |- d = 1 # noqa: E501
33 32 | # fmt: on
34 33 |
35 34 |

RUF100_0.py:29:33: RUF100 [*] Unused `noqa` directive (unused: `E501`)
RUF100_0.py:32:33: RUF100 [*] Unused `noqa` directive (unused: `E501`)
|
28 | # Invalid - many spaces before #
29 | d = 1 # noqa: E501
31 | # Invalid - many spaces before #
32 | d = 1 # noqa: E501
| ^^^^^^^^^^^^ RUF100
30 | # fmt: on
33 | # fmt: on
|
= help: Remove unused `noqa` directive

Fix
26 26 | d = 1# noqa: E501
27 27 |
28 28 | # Invalid - many spaces before #
29 |- d = 1 # noqa: E501
29 |+ d = 1
30 30 | # fmt: on
31 31 |
32 32 |
29 29 | d = 1# noqa: E501
30 30 |
31 31 | # Invalid - many spaces before #
32 |- d = 1 # noqa: E501
32 |+ d = 1
33 33 | # fmt: on
34 34 |
35 35 |

RUF100_0.py:55:6: RUF100 [*] Unused `noqa` directive (unused: `F841`)
RUF100_0.py:58:6: RUF100 [*] Unused `noqa` directive (unused: `F841`)
|
54 | Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
55 | """ # noqa: E501, F841
57 | Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
58 | """ # noqa: E501, F841
| ^^^^^^^^^^^^^^^^^^ RUF100
56 |
57 | # Invalid
59 |
60 | # Invalid
|
= help: Remove unused `noqa` directive

Fix
52 52 | https://github.com/PyCQA/pycodestyle/pull/258/files#diff-841c622497a8033d10152bfdfb15b20b92437ecdea21a260944ea86b77b51533
53 53 |
54 54 | Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
55 |-""" # noqa: E501, F841
55 |+""" # noqa: E501
55 55 | https://github.com/PyCQA/pycodestyle/pull/258/files#diff-841c622497a8033d10152bfdfb15b20b92437ecdea21a260944ea86b77b51533
56 56 |
57 57 | # Invalid
58 58 | _ = """Lorem ipsum dolor sit amet.
57 57 | Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
58 |-""" # noqa: E501, F841
58 |+""" # noqa: E501
59 59 |
60 60 | # Invalid
61 61 | _ = """Lorem ipsum dolor sit amet.

RUF100_0.py:63:6: RUF100 [*] Unused `noqa` directive (unused: `E501`)
RUF100_0.py:66:6: RUF100 [*] Unused `noqa` directive (unused: `E501`)
|
62 | Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor.
63 | """ # noqa: E501
65 | Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor.
66 | """ # noqa: E501
| ^^^^^^^^^^^^ RUF100
64 |
65 | # Invalid
67 |
68 | # Invalid
|
= help: Remove unused `noqa` directive

Fix
60 60 | https://github.com/PyCQA/pycodestyle/pull/258/files#diff-841c622497a8033d10152bfdfb15b20b92437ecdea21a260944ea86b77b51533
61 61 |
62 62 | Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor.
63 |-""" # noqa: E501
63 |+"""
63 63 | https://github.com/PyCQA/pycodestyle/pull/258/files#diff-841c622497a8033d10152bfdfb15b20b92437ecdea21a260944ea86b77b51533
64 64 |
65 65 | # Invalid
66 66 | _ = """Lorem ipsum dolor sit amet.
65 65 | Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor.
66 |-""" # noqa: E501
66 |+"""
67 67 |
68 68 | # Invalid
69 69 | _ = """Lorem ipsum dolor sit amet.

RUF100_0.py:71:6: RUF100 [*] Unused blanket `noqa` directive
RUF100_0.py:74:6: RUF100 [*] Unused blanket `noqa` directive
|
70 | Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor.
71 | """ # noqa
73 | Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor.
74 | """ # noqa
| ^^^^^^ RUF100
72 |
73 | # Valid
75 |
76 | # Valid
|
= help: Remove unused `noqa` directive

Fix
68 68 | https://github.com/PyCQA/pycodestyle/pull/258/files#diff-841c622497a8033d10152bfdfb15b20b92437ecdea21a260944ea86b77b51533
69 69 |
70 70 | Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor.
71 |-""" # noqa
71 |+"""
71 71 | https://github.com/PyCQA/pycodestyle/pull/258/files#diff-841c622497a8033d10152bfdfb15b20b92437ecdea21a260944ea86b77b51533
72 72 |
73 73 | # Valid
74 74 | # this is a veryyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy long comment # noqa: E501
73 73 | Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor.
74 |-""" # noqa
74 |+"""
75 75 |
76 76 | # Valid
77 77 | # this is a veryyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy long comment # noqa: E501

RUF100_0.py:85:8: F401 [*] `shelve` imported but unused
RUF100_0.py:88:8: F401 [*] `shelve` imported but unused
|
83 | import collections # noqa
84 | import os # noqa: F401, RUF100
85 | import shelve # noqa: RUF100
86 | import collections # noqa
87 | import os # noqa: F401, RUF100
88 | import shelve # noqa: RUF100
| ^^^^^^ F401
86 | import sys # noqa: F401, RUF100
89 | import sys # noqa: F401, RUF100
|
= help: Remove unused import: `shelve`

Fix
82 82 |
83 83 | import collections # noqa
84 84 | import os # noqa: F401, RUF100
85 |-import shelve # noqa: RUF100
86 85 | import sys # noqa: F401, RUF100
87 86 |
88 87 | print(sys.path)
85 85 |
86 86 | import collections # noqa
87 87 | import os # noqa: F401, RUF100
88 |-import shelve # noqa: RUF100
89 88 | import sys # noqa: F401, RUF100
90 89 |
91 90 | print(sys.path)

RUF100_0.py:90:89: E501 Line too long (89 > 88)
RUF100_0.py:93:89: E501 Line too long (89 > 88)
|
88 | print(sys.path)
89 |
90 | "shape: (6,)\nSeries: '' [duration[μs]]\n[\n\t0µs\n\t1µs\n\t2µs\n\t3µs\n\t4µs\n\t5µs\n]" # noqa: F401
91 | print(sys.path)
92 |
93 | "shape: (6,)\nSeries: '' [duration[μs]]\n[\n\t0µs\n\t1µs\n\t2µs\n\t3µs\n\t4µs\n\t5µs\n]" # noqa: F401
| ^ E501
|

RUF100_0.py:90:92: RUF100 [*] Unused `noqa` directive (unused: `F401`)
RUF100_0.py:93:92: RUF100 [*] Unused `noqa` directive (unused: `F401`)
|
88 | print(sys.path)
89 |
90 | "shape: (6,)\nSeries: '' [duration[μs]]\n[\n\t0µs\n\t1µs\n\t2µs\n\t3µs\n\t4µs\n\t5µs\n]" # noqa: F401
91 | print(sys.path)
92 |
93 | "shape: (6,)\nSeries: '' [duration[μs]]\n[\n\t0µs\n\t1µs\n\t2µs\n\t3µs\n\t4µs\n\t5µs\n]" # noqa: F401
| ^^^^^^^^^^^^ RUF100
|
= help: Remove unused `noqa` directive

Fix
87 87 |
88 88 | print(sys.path)
89 89 |
90 |-"shape: (6,)\nSeries: '' [duration[μs]]\n[\n\t0µs\n\t1µs\n\t2µs\n\t3µs\n\t4µs\n\t5µs\n]" # noqa: F401
90 |+"shape: (6,)\nSeries: '' [duration[μs]]\n[\n\t0µs\n\t1µs\n\t2µs\n\t3µs\n\t4µs\n\t5µs\n]"
91 91 |
90 90 |
91 91 | print(sys.path)
92 92 |
93 93 | def f():
93 |-"shape: (6,)\nSeries: '' [duration[μs]]\n[\n\t0µs\n\t1µs\n\t2µs\n\t3µs\n\t4µs\n\t5µs\n]" # noqa: F401
93 |+"shape: (6,)\nSeries: '' [duration[μs]]\n[\n\t0µs\n\t1µs\n\t2µs\n\t3µs\n\t4µs\n\t5µs\n]"
94 94 |
95 95 |
96 96 | def f():


Loading

0 comments on commit 6f31e9c

Please sign in to comment.