Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[refurb] Fix misbehavior of operator.itemgetter when getter param is a tuple #11774

Merged
merged 3 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/refurb/FURB118.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
op_itemgetter = lambda x: (x[0], x[1], x[2])
op_itemgetter = lambda x: (x[1:], x[2])
op_itemgetter = lambda x: x[:]
op_itemgetter = lambda x: x[0, 1]
op_itemgetter = lambda x: x[(0, 1)]


def op_not2(x):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,12 @@ fn slice_expr_to_slice_call(slice: &ExprSlice, locator: &Locator) -> String {
fn subscript_slice_to_string<'a>(expr: &Expr, locator: &Locator<'a>) -> Cow<'a, str> {
if let Expr::Slice(expr_slice) = expr {
Cow::Owned(slice_expr_to_slice_call(expr_slice, locator))
} else if let Expr::Tuple(tuple) = expr {
if tuple.parenthesized {
Cow::Borrowed(locator.slice(expr))
} else {
Cow::Owned(format!("({})", locator.slice(tuple)))
}
} else {
Cow::Borrowed(locator.slice(expr))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,7 @@ FURB118.py:31:17: FURB118 [*] Use `operator.itemgetter(0, 1, 2)` instead of defi
32 |+op_itemgetter = operator.itemgetter(0, 1, 2)
32 33 | op_itemgetter = lambda x: (x[1:], x[2])
33 34 | op_itemgetter = lambda x: x[:]
34 35 |
34 35 | op_itemgetter = lambda x: x[0, 1]

FURB118.py:32:17: FURB118 [*] Use `operator.itemgetter(slice(1, None), 2)` instead of defining a lambda
|
Expand All @@ -734,6 +734,7 @@ FURB118.py:32:17: FURB118 [*] Use `operator.itemgetter(slice(1, None), 2)` inste
32 | op_itemgetter = lambda x: (x[1:], x[2])
| ^^^^^^^^^^^^^^^^^^^^^^^ FURB118
33 | op_itemgetter = lambda x: x[:]
34 | op_itemgetter = lambda x: x[0, 1]
|
= help: Replace with `operator.itemgetter(slice(1, None), 2)`

Expand All @@ -750,15 +751,17 @@ FURB118.py:32:17: FURB118 [*] Use `operator.itemgetter(slice(1, None), 2)` inste
32 |-op_itemgetter = lambda x: (x[1:], x[2])
33 |+op_itemgetter = operator.itemgetter(slice(1, None), 2)
33 34 | op_itemgetter = lambda x: x[:]
34 35 |
35 36 |
34 35 | op_itemgetter = lambda x: x[0, 1]
35 36 | op_itemgetter = lambda x: x[(0, 1)]

FURB118.py:33:17: FURB118 [*] Use `operator.itemgetter(slice(None))` instead of defining a lambda
|
31 | op_itemgetter = lambda x: (x[0], x[1], x[2])
32 | op_itemgetter = lambda x: (x[1:], x[2])
33 | op_itemgetter = lambda x: x[:]
| ^^^^^^^^^^^^^^ FURB118
34 | op_itemgetter = lambda x: x[0, 1]
35 | op_itemgetter = lambda x: x[(0, 1)]
|
= help: Replace with `operator.itemgetter(slice(None))`

Expand All @@ -774,22 +777,73 @@ FURB118.py:33:17: FURB118 [*] Use `operator.itemgetter(slice(None))` instead of
32 33 | op_itemgetter = lambda x: (x[1:], x[2])
33 |-op_itemgetter = lambda x: x[:]
34 |+op_itemgetter = operator.itemgetter(slice(None))
34 35 |
35 36 |
36 37 | def op_not2(x):
34 35 | op_itemgetter = lambda x: x[0, 1]
35 36 | op_itemgetter = lambda x: x[(0, 1)]
36 37 |

FURB118.py:36:5: FURB118 Use `operator.not_` instead of defining a function
FURB118.py:34:17: FURB118 [*] Use `operator.itemgetter((0, 1))` instead of defining a lambda
|
36 | def op_not2(x):
32 | op_itemgetter = lambda x: (x[1:], x[2])
33 | op_itemgetter = lambda x: x[:]
34 | op_itemgetter = lambda x: x[0, 1]
| ^^^^^^^^^^^^^^^^^ FURB118
35 | op_itemgetter = lambda x: x[(0, 1)]
|
= help: Replace with `operator.itemgetter((0, 1))`

ℹ Safe fix
1 1 | # Errors.
2 |+import operator
2 3 | op_bitnot = lambda x: ~x
3 4 | op_not = lambda x: not x
4 5 | op_pos = lambda x: +x
--------------------------------------------------------------------------------
31 32 | op_itemgetter = lambda x: (x[0], x[1], x[2])
32 33 | op_itemgetter = lambda x: (x[1:], x[2])
33 34 | op_itemgetter = lambda x: x[:]
34 |-op_itemgetter = lambda x: x[0, 1]
35 |+op_itemgetter = operator.itemgetter((0, 1))
35 36 | op_itemgetter = lambda x: x[(0, 1)]
36 37 |
37 38 |

FURB118.py:35:17: FURB118 [*] Use `operator.itemgetter((0, 1))` instead of defining a lambda
|
33 | op_itemgetter = lambda x: x[:]
34 | op_itemgetter = lambda x: x[0, 1]
35 | op_itemgetter = lambda x: x[(0, 1)]
| ^^^^^^^^^^^^^^^^^^^ FURB118
|
= help: Replace with `operator.itemgetter((0, 1))`

ℹ Safe fix
1 1 | # Errors.
2 |+import operator
2 3 | op_bitnot = lambda x: ~x
3 4 | op_not = lambda x: not x
4 5 | op_pos = lambda x: +x
--------------------------------------------------------------------------------
32 33 | op_itemgetter = lambda x: (x[1:], x[2])
33 34 | op_itemgetter = lambda x: x[:]
34 35 | op_itemgetter = lambda x: x[0, 1]
35 |-op_itemgetter = lambda x: x[(0, 1)]
36 |+op_itemgetter = operator.itemgetter((0, 1))
36 37 |
37 38 |
38 39 | def op_not2(x):

FURB118.py:38:5: FURB118 Use `operator.not_` instead of defining a function
|
38 | def op_not2(x):
| ^^^^^^^ FURB118
37 | return not x
39 | return not x
|
= help: Replace with `operator.not_`

FURB118.py:40:5: FURB118 Use `operator.add` instead of defining a function
FURB118.py:42:5: FURB118 Use `operator.add` instead of defining a function
|
40 | def op_add2(x, y):
42 | def op_add2(x, y):
| ^^^^^^^ FURB118
41 | return x + y
43 | return x + y
|
= help: Replace with `operator.add`
Loading