Skip to content

Commit 7a94216

Browse files
authored
[ruff] Stabilize unsafe fix for zip-instead-of-pairwise (RUF007) (#14401)
This PR stabilizes the unsafe fix for [zip-instead-of-pairwise (RUF007)](https://docs.astral.sh/ruff/rules/zip-instead-of-pairwise/#zip-instead-of-pairwise-ruf007), which replaces the use of zip with that of itertools.pairwise and has been available under preview since version 0.5.7. There are no open issues regarding RUF007 at the time of this writing.
1 parent 1384b1a commit 7a94216

File tree

4 files changed

+164
-277
lines changed

4 files changed

+164
-277
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,6 @@ mod tests {
388388
Ok(())
389389
}
390390

391-
#[test_case(Rule::ZipInsteadOfPairwise, Path::new("RUF007.py"))]
392391
#[test_case(Rule::UnsafeMarkupUse, Path::new("RUF035.py"))]
393392
#[test_case(
394393
Rule::FunctionCallInDataclassDefaultArgument,

crates/ruff_linter/src/rules/ruff/rules/zip_instead_of_pairwise.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -154,18 +154,16 @@ pub(crate) fn zip_instead_of_pairwise(checker: &mut Checker, call: &ast::ExprCal
154154

155155
let mut diagnostic = Diagnostic::new(ZipInsteadOfPairwise, func.range());
156156

157-
if checker.settings.preview.is_enabled() {
158-
diagnostic.try_set_fix(|| {
159-
let (import_edit, binding) = checker.importer().get_or_import_symbol(
160-
&ImportRequest::import("itertools", "pairwise"),
161-
func.start(),
162-
checker.semantic(),
163-
)?;
164-
let reference_edit =
165-
Edit::range_replacement(format!("{binding}({})", first_arg_info.id), call.range());
166-
Ok(Fix::unsafe_edits(import_edit, [reference_edit]))
167-
});
168-
}
157+
diagnostic.try_set_fix(|| {
158+
let (import_edit, binding) = checker.importer().get_or_import_symbol(
159+
&ImportRequest::import("itertools", "pairwise"),
160+
func.start(),
161+
checker.semantic(),
162+
)?;
163+
let reference_edit =
164+
Edit::range_replacement(format!("{binding}({})", first_arg_info.id), call.range());
165+
Ok(Fix::unsafe_edits(import_edit, [reference_edit]))
166+
});
169167

170168
checker.diagnostics.push(diagnostic);
171169
}

crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__RUF007_RUF007.py.snap

Lines changed: 154 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
---
22
source: crates/ruff_linter/src/rules/ruff/mod.rs
3-
snapshot_kind: text
43
---
5-
RUF007.py:16:1: RUF007 Prefer `itertools.pairwise()` over `zip()` when iterating over successive pairs
4+
RUF007.py:16:1: RUF007 [*] Prefer `itertools.pairwise()` over `zip()` when iterating over successive pairs
65
|
76
15 | # Errors
87
16 | zip(input, input[1:])
@@ -12,7 +11,22 @@ RUF007.py:16:1: RUF007 Prefer `itertools.pairwise()` over `zip()` when iterating
1211
|
1312
= help: Replace `zip()` with `itertools.pairwise()`
1413

15-
RUF007.py:17:1: RUF007 Prefer `itertools.pairwise()` over `zip()` when iterating over successive pairs
14+
Unsafe fix
15+
1 |+import itertools
16+
1 2 | input = [1, 2, 3]
17+
2 3 | otherInput = [2, 3, 4]
18+
3 4 | foo = [1, 2, 3, 4]
19+
--------------------------------------------------------------------------------
20+
13 14 | zip(foo[:-1], foo[1:], foo, strict=True) # more than 2 inputs
21+
14 15 |
22+
15 16 | # Errors
23+
16 |-zip(input, input[1:])
24+
17 |+itertools.pairwise(input)
25+
17 18 | zip(input, input[1::1])
26+
18 19 | zip(input[:-1], input[1:])
27+
19 20 | zip(input[1:], input[2:])
28+
29+
RUF007.py:17:1: RUF007 [*] Prefer `itertools.pairwise()` over `zip()` when iterating over successive pairs
1630
|
1731
15 | # Errors
1832
16 | zip(input, input[1:])
@@ -23,7 +37,22 @@ RUF007.py:17:1: RUF007 Prefer `itertools.pairwise()` over `zip()` when iterating
2337
|
2438
= help: Replace `zip()` with `itertools.pairwise()`
2539

26-
RUF007.py:18:1: RUF007 Prefer `itertools.pairwise()` over `zip()` when iterating over successive pairs
40+
Unsafe fix
41+
1 |+import itertools
42+
1 2 | input = [1, 2, 3]
43+
2 3 | otherInput = [2, 3, 4]
44+
3 4 | foo = [1, 2, 3, 4]
45+
--------------------------------------------------------------------------------
46+
14 15 |
47+
15 16 | # Errors
48+
16 17 | zip(input, input[1:])
49+
17 |-zip(input, input[1::1])
50+
18 |+itertools.pairwise(input)
51+
18 19 | zip(input[:-1], input[1:])
52+
19 20 | zip(input[1:], input[2:])
53+
20 21 | zip(input[1:-1], input[2:])
54+
55+
RUF007.py:18:1: RUF007 [*] Prefer `itertools.pairwise()` over `zip()` when iterating over successive pairs
2756
|
2857
16 | zip(input, input[1:])
2958
17 | zip(input, input[1::1])
@@ -34,7 +63,22 @@ RUF007.py:18:1: RUF007 Prefer `itertools.pairwise()` over `zip()` when iterating
3463
|
3564
= help: Replace `zip()` with `itertools.pairwise()`
3665

37-
RUF007.py:19:1: RUF007 Prefer `itertools.pairwise()` over `zip()` when iterating over successive pairs
66+
Unsafe fix
67+
1 |+import itertools
68+
1 2 | input = [1, 2, 3]
69+
2 3 | otherInput = [2, 3, 4]
70+
3 4 | foo = [1, 2, 3, 4]
71+
--------------------------------------------------------------------------------
72+
15 16 | # Errors
73+
16 17 | zip(input, input[1:])
74+
17 18 | zip(input, input[1::1])
75+
18 |-zip(input[:-1], input[1:])
76+
19 |+itertools.pairwise(input)
77+
19 20 | zip(input[1:], input[2:])
78+
20 21 | zip(input[1:-1], input[2:])
79+
21 22 | list(zip(input, input[1:]))
80+
81+
RUF007.py:19:1: RUF007 [*] Prefer `itertools.pairwise()` over `zip()` when iterating over successive pairs
3882
|
3983
17 | zip(input, input[1::1])
4084
18 | zip(input[:-1], input[1:])
@@ -45,7 +89,22 @@ RUF007.py:19:1: RUF007 Prefer `itertools.pairwise()` over `zip()` when iterating
4589
|
4690
= help: Replace `zip()` with `itertools.pairwise()`
4791

48-
RUF007.py:20:1: RUF007 Prefer `itertools.pairwise()` over `zip()` when iterating over successive pairs
92+
Unsafe fix
93+
1 |+import itertools
94+
1 2 | input = [1, 2, 3]
95+
2 3 | otherInput = [2, 3, 4]
96+
3 4 | foo = [1, 2, 3, 4]
97+
--------------------------------------------------------------------------------
98+
16 17 | zip(input, input[1:])
99+
17 18 | zip(input, input[1::1])
100+
18 19 | zip(input[:-1], input[1:])
101+
19 |-zip(input[1:], input[2:])
102+
20 |+itertools.pairwise(input)
103+
20 21 | zip(input[1:-1], input[2:])
104+
21 22 | list(zip(input, input[1:]))
105+
22 23 | list(zip(input[:-1], input[1:]))
106+
107+
RUF007.py:20:1: RUF007 [*] Prefer `itertools.pairwise()` over `zip()` when iterating over successive pairs
49108
|
50109
18 | zip(input[:-1], input[1:])
51110
19 | zip(input[1:], input[2:])
@@ -56,7 +115,22 @@ RUF007.py:20:1: RUF007 Prefer `itertools.pairwise()` over `zip()` when iterating
56115
|
57116
= help: Replace `zip()` with `itertools.pairwise()`
58117

59-
RUF007.py:21:6: RUF007 Prefer `itertools.pairwise()` over `zip()` when iterating over successive pairs
118+
Unsafe fix
119+
1 |+import itertools
120+
1 2 | input = [1, 2, 3]
121+
2 3 | otherInput = [2, 3, 4]
122+
3 4 | foo = [1, 2, 3, 4]
123+
--------------------------------------------------------------------------------
124+
17 18 | zip(input, input[1::1])
125+
18 19 | zip(input[:-1], input[1:])
126+
19 20 | zip(input[1:], input[2:])
127+
20 |-zip(input[1:-1], input[2:])
128+
21 |+itertools.pairwise(input)
129+
21 22 | list(zip(input, input[1:]))
130+
22 23 | list(zip(input[:-1], input[1:]))
131+
23 24 | zip(foo[:-1], foo[1:], strict=True)
132+
133+
RUF007.py:21:6: RUF007 [*] Prefer `itertools.pairwise()` over `zip()` when iterating over successive pairs
60134
|
61135
19 | zip(input[1:], input[2:])
62136
20 | zip(input[1:-1], input[2:])
@@ -67,7 +141,22 @@ RUF007.py:21:6: RUF007 Prefer `itertools.pairwise()` over `zip()` when iterating
67141
|
68142
= help: Replace `zip()` with `itertools.pairwise()`
69143

70-
RUF007.py:22:6: RUF007 Prefer `itertools.pairwise()` over `zip()` when iterating over successive pairs
144+
Unsafe fix
145+
1 |+import itertools
146+
1 2 | input = [1, 2, 3]
147+
2 3 | otherInput = [2, 3, 4]
148+
3 4 | foo = [1, 2, 3, 4]
149+
--------------------------------------------------------------------------------
150+
18 19 | zip(input[:-1], input[1:])
151+
19 20 | zip(input[1:], input[2:])
152+
20 21 | zip(input[1:-1], input[2:])
153+
21 |-list(zip(input, input[1:]))
154+
22 |+list(itertools.pairwise(input))
155+
22 23 | list(zip(input[:-1], input[1:]))
156+
23 24 | zip(foo[:-1], foo[1:], strict=True)
157+
24 25 | zip(foo[:-1], foo[1:], strict=False)
158+
159+
RUF007.py:22:6: RUF007 [*] Prefer `itertools.pairwise()` over `zip()` when iterating over successive pairs
71160
|
72161
20 | zip(input[1:-1], input[2:])
73162
21 | list(zip(input, input[1:]))
@@ -78,7 +167,22 @@ RUF007.py:22:6: RUF007 Prefer `itertools.pairwise()` over `zip()` when iterating
78167
|
79168
= help: Replace `zip()` with `itertools.pairwise()`
80169

81-
RUF007.py:23:1: RUF007 Prefer `itertools.pairwise()` over `zip()` when iterating over successive pairs
170+
Unsafe fix
171+
1 |+import itertools
172+
1 2 | input = [1, 2, 3]
173+
2 3 | otherInput = [2, 3, 4]
174+
3 4 | foo = [1, 2, 3, 4]
175+
--------------------------------------------------------------------------------
176+
19 20 | zip(input[1:], input[2:])
177+
20 21 | zip(input[1:-1], input[2:])
178+
21 22 | list(zip(input, input[1:]))
179+
22 |-list(zip(input[:-1], input[1:]))
180+
23 |+list(itertools.pairwise(input))
181+
23 24 | zip(foo[:-1], foo[1:], strict=True)
182+
24 25 | zip(foo[:-1], foo[1:], strict=False)
183+
25 26 | zip(foo[:-1], foo[1:], strict=bool(foo))
184+
185+
RUF007.py:23:1: RUF007 [*] Prefer `itertools.pairwise()` over `zip()` when iterating over successive pairs
82186
|
83187
21 | list(zip(input, input[1:]))
84188
22 | list(zip(input[:-1], input[1:]))
@@ -89,7 +193,21 @@ RUF007.py:23:1: RUF007 Prefer `itertools.pairwise()` over `zip()` when iterating
89193
|
90194
= help: Replace `zip()` with `itertools.pairwise()`
91195

92-
RUF007.py:24:1: RUF007 Prefer `itertools.pairwise()` over `zip()` when iterating over successive pairs
196+
Unsafe fix
197+
1 |+import itertools
198+
1 2 | input = [1, 2, 3]
199+
2 3 | otherInput = [2, 3, 4]
200+
3 4 | foo = [1, 2, 3, 4]
201+
--------------------------------------------------------------------------------
202+
20 21 | zip(input[1:-1], input[2:])
203+
21 22 | list(zip(input, input[1:]))
204+
22 23 | list(zip(input[:-1], input[1:]))
205+
23 |-zip(foo[:-1], foo[1:], strict=True)
206+
24 |+itertools.pairwise(foo)
207+
24 25 | zip(foo[:-1], foo[1:], strict=False)
208+
25 26 | zip(foo[:-1], foo[1:], strict=bool(foo))
209+
210+
RUF007.py:24:1: RUF007 [*] Prefer `itertools.pairwise()` over `zip()` when iterating over successive pairs
93211
|
94212
22 | list(zip(input[:-1], input[1:]))
95213
23 | zip(foo[:-1], foo[1:], strict=True)
@@ -99,11 +217,36 @@ RUF007.py:24:1: RUF007 Prefer `itertools.pairwise()` over `zip()` when iterating
99217
|
100218
= help: Replace `zip()` with `itertools.pairwise()`
101219

102-
RUF007.py:25:1: RUF007 Prefer `itertools.pairwise()` over `zip()` when iterating over successive pairs
220+
Unsafe fix
221+
1 |+import itertools
222+
1 2 | input = [1, 2, 3]
223+
2 3 | otherInput = [2, 3, 4]
224+
3 4 | foo = [1, 2, 3, 4]
225+
--------------------------------------------------------------------------------
226+
21 22 | list(zip(input, input[1:]))
227+
22 23 | list(zip(input[:-1], input[1:]))
228+
23 24 | zip(foo[:-1], foo[1:], strict=True)
229+
24 |-zip(foo[:-1], foo[1:], strict=False)
230+
25 |+itertools.pairwise(foo)
231+
25 26 | zip(foo[:-1], foo[1:], strict=bool(foo))
232+
233+
RUF007.py:25:1: RUF007 [*] Prefer `itertools.pairwise()` over `zip()` when iterating over successive pairs
103234
|
104235
23 | zip(foo[:-1], foo[1:], strict=True)
105236
24 | zip(foo[:-1], foo[1:], strict=False)
106237
25 | zip(foo[:-1], foo[1:], strict=bool(foo))
107238
| ^^^ RUF007
108239
|
109240
= help: Replace `zip()` with `itertools.pairwise()`
241+
242+
Unsafe fix
243+
1 |+import itertools
244+
1 2 | input = [1, 2, 3]
245+
2 3 | otherInput = [2, 3, 4]
246+
3 4 | foo = [1, 2, 3, 4]
247+
--------------------------------------------------------------------------------
248+
22 23 | list(zip(input[:-1], input[1:]))
249+
23 24 | zip(foo[:-1], foo[1:], strict=True)
250+
24 25 | zip(foo[:-1], foo[1:], strict=False)
251+
25 |-zip(foo[:-1], foo[1:], strict=bool(foo))
252+
26 |+itertools.pairwise(foo)

0 commit comments

Comments
 (0)