Skip to content

Commit c38a7b7

Browse files
committed
Fix handling of slices in tuples for FURB118, e.g., x[:, 1]
There was already handling for the singleton `x[:]` case but not the tuple case.\nCloses #13508
1 parent 7c83af4 commit c38a7b7

File tree

3 files changed

+64
-5
lines changed

3 files changed

+64
-5
lines changed

crates/ruff_linter/resources/test/fixtures/refurb/FURB118.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,7 @@ class Class:
8383
@staticmethod
8484
def add(x, y):
8585
return x + y
86+
87+
# See https://github.com/astral-sh/ruff/issues/13508
88+
op_itemgetter = lambda x: x[:, 1]
89+
op_itemgetter = lambda x: x[1, :]

crates/ruff_linter/src/rules/refurb/rules/reimplemented_operator.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,20 @@ fn subscript_slice_to_string<'a>(expr: &Expr, locator: &Locator<'a>) -> Cow<'a,
213213
if let Expr::Slice(expr_slice) = expr {
214214
Cow::Owned(slice_expr_to_slice_call(expr_slice, locator))
215215
} else if let Expr::Tuple(tuple) = expr {
216+
let inner = tuple
217+
.elts
218+
.iter()
219+
.map(|expr| match expr {
220+
Expr::Slice(expr) => Cow::Owned(slice_expr_to_slice_call(expr, locator)),
221+
_ => Cow::Borrowed(locator.slice(expr)),
222+
})
223+
.collect::<Vec<_>>()
224+
.join(", ");
225+
216226
if tuple.parenthesized {
217-
Cow::Borrowed(locator.slice(expr))
227+
Cow::Owned(inner)
218228
} else {
219-
Cow::Owned(format!("({})", locator.slice(tuple)))
229+
Cow::Owned(format!("({})", inner))
220230
}
221231
} else {
222232
Cow::Borrowed(locator.slice(expr))

crates/ruff_linter/src/rules/refurb/snapshots/ruff_linter__rules__refurb__tests__FURB118_FURB118.py.snap

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -807,14 +807,14 @@ FURB118.py:34:17: FURB118 [*] Use `operator.itemgetter((0, 1))` instead of defin
807807
36 37 |
808808
37 38 |
809809

810-
FURB118.py:35:17: FURB118 [*] Use `operator.itemgetter((0, 1))` instead of defining a lambda
810+
FURB118.py:35:17: FURB118 [*] Use `operator.itemgetter(0, 1)` instead of defining a lambda
811811
|
812812
33 | op_itemgetter = lambda x: x[:]
813813
34 | op_itemgetter = lambda x: x[0, 1]
814814
35 | op_itemgetter = lambda x: x[(0, 1)]
815815
| ^^^^^^^^^^^^^^^^^^^ FURB118
816816
|
817-
= help: Replace with `operator.itemgetter((0, 1))`
817+
= help: Replace with `operator.itemgetter(0, 1)`
818818

819819
Safe fix
820820
1 1 | # Errors.
@@ -827,7 +827,7 @@ FURB118.py:35:17: FURB118 [*] Use `operator.itemgetter((0, 1))` instead of defin
827827
33 34 | op_itemgetter = lambda x: x[:]
828828
34 35 | op_itemgetter = lambda x: x[0, 1]
829829
35 |-op_itemgetter = lambda x: x[(0, 1)]
830-
36 |+op_itemgetter = operator.itemgetter((0, 1))
830+
36 |+op_itemgetter = operator.itemgetter(0, 1)
831831
36 37 |
832832
37 38 |
833833
38 39 | def op_not2(x):
@@ -847,3 +847,48 @@ FURB118.py:42:5: FURB118 Use `operator.add` instead of defining a function
847847
43 | return x + y
848848
|
849849
= help: Replace with `operator.add`
850+
851+
FURB118.py:88:17: FURB118 [*] Use `operator.itemgetter((slice(None), 1))` instead of defining a lambda
852+
|
853+
87 | # See https://github.com/astral-sh/ruff/issues/13508
854+
88 | op_itemgetter = lambda x: x[:, 1]
855+
| ^^^^^^^^^^^^^^^^^ FURB118
856+
89 | op_itemgetter = lambda x: x[1, :]
857+
|
858+
= help: Replace with `operator.itemgetter((slice(None), 1))`
859+
860+
ℹ Safe fix
861+
1 1 | # Errors.
862+
2 |+import operator
863+
2 3 | op_bitnot = lambda x: ~x
864+
3 4 | op_not = lambda x: not x
865+
4 5 | op_pos = lambda x: +x
866+
--------------------------------------------------------------------------------
867+
85 86 | return x + y
868+
86 87 |
869+
87 88 | # See https://github.com/astral-sh/ruff/issues/13508
870+
88 |-op_itemgetter = lambda x: x[:, 1]
871+
89 |+op_itemgetter = operator.itemgetter((slice(None), 1))
872+
89 90 | op_itemgetter = lambda x: x[1, :]
873+
874+
FURB118.py:89:17: FURB118 [*] Use `operator.itemgetter((1, slice(None)))` instead of defining a lambda
875+
|
876+
87 | # See https://github.com/astral-sh/ruff/issues/13508
877+
88 | op_itemgetter = lambda x: x[:, 1]
878+
89 | op_itemgetter = lambda x: x[1, :]
879+
| ^^^^^^^^^^^^^^^^^ FURB118
880+
|
881+
= help: Replace with `operator.itemgetter((1, slice(None)))`
882+
883+
ℹ Safe fix
884+
1 1 | # Errors.
885+
2 |+import operator
886+
2 3 | op_bitnot = lambda x: ~x
887+
3 4 | op_not = lambda x: not x
888+
4 5 | op_pos = lambda x: +x
889+
--------------------------------------------------------------------------------
890+
86 87 |
891+
87 88 | # See https://github.com/astral-sh/ruff/issues/13508
892+
88 89 | op_itemgetter = lambda x: x[:, 1]
893+
89 |-op_itemgetter = lambda x: x[1, :]
894+
90 |+op_itemgetter = operator.itemgetter((1, slice(None)))

0 commit comments

Comments
 (0)