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

Apply RUF017 when start is passed via position #6664

Merged
merged 1 commit into from
Aug 18, 2023
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
4 changes: 2 additions & 2 deletions crates/ruff/src/rules/ruff/rules/quadratic_list_summation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,11 @@ fn func_is_builtin(func: &Expr, name: &str, semantic: &SemanticModel) -> bool {

/// Returns `true` if the `start` argument to a `sum()` call is an empty list.
fn start_is_empty_list(arguments: &Arguments, semantic: &SemanticModel) -> bool {
let Some(keyword) = arguments.find_keyword("start") else {
let Some(start_arg) = arguments.find_argument("start", 1) else {
return false;
};

match &keyword.value {
match start_arg {
Expr::Call(ast::ExprCall {
func, arguments, ..
}) => arguments.is_empty() && func_is_builtin(func, "list", semantic),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,31 @@ RUF017.py:5:1: RUF017 [*] Avoid quadratic list summation
7 9 | sum([[1, 2, 3], [4, 5, 6]], start=[])
8 10 | sum([[1, 2, 3], [4, 5, 6]], [])

RUF017.py:6:1: RUF017 [*] Avoid quadratic list summation
|
4 | # RUF017
5 | sum([x, y], start=[])
6 | sum([x, y], [])
| ^^^^^^^^^^^^^^^ RUF017
7 | sum([[1, 2, 3], [4, 5, 6]], start=[])
8 | sum([[1, 2, 3], [4, 5, 6]], [])
|
= help: Replace with `functools.reduce`

ℹ Suggested fix
1 |+import functools
2 |+import operator
1 3 | x = [1, 2, 3]
2 4 | y = [4, 5, 6]
3 5 |
4 6 | # RUF017
5 7 | sum([x, y], start=[])
6 |-sum([x, y], [])
8 |+functools.reduce(operator.iadd, [x, y], [])
7 9 | sum([[1, 2, 3], [4, 5, 6]], start=[])
8 10 | sum([[1, 2, 3], [4, 5, 6]], [])
9 11 | sum([[1, 2, 3], [4, 5, 6]],

RUF017.py:7:1: RUF017 [*] Avoid quadratic list summation
|
5 | sum([x, y], start=[])
Expand All @@ -50,4 +75,60 @@ RUF017.py:7:1: RUF017 [*] Avoid quadratic list summation
9 11 | sum([[1, 2, 3], [4, 5, 6]],
10 12 | [])

RUF017.py:8:1: RUF017 [*] Avoid quadratic list summation
|
6 | sum([x, y], [])
7 | sum([[1, 2, 3], [4, 5, 6]], start=[])
8 | sum([[1, 2, 3], [4, 5, 6]], [])
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ RUF017
9 | sum([[1, 2, 3], [4, 5, 6]],
10 | [])
|
= help: Replace with `functools.reduce`

ℹ Suggested fix
1 |+import functools
2 |+import operator
1 3 | x = [1, 2, 3]
2 4 | y = [4, 5, 6]
3 5 |
--------------------------------------------------------------------------------
5 7 | sum([x, y], start=[])
6 8 | sum([x, y], [])
7 9 | sum([[1, 2, 3], [4, 5, 6]], start=[])
8 |-sum([[1, 2, 3], [4, 5, 6]], [])
10 |+functools.reduce(operator.iadd, [[1, 2, 3], [4, 5, 6]], [])
9 11 | sum([[1, 2, 3], [4, 5, 6]],
10 12 | [])
11 13 |

RUF017.py:9:1: RUF017 [*] Avoid quadratic list summation
|
7 | sum([[1, 2, 3], [4, 5, 6]], start=[])
8 | sum([[1, 2, 3], [4, 5, 6]], [])
9 | / sum([[1, 2, 3], [4, 5, 6]],
10 | | [])
| |_______^ RUF017
11 |
12 | # OK
|
= help: Replace with `functools.reduce`

ℹ Suggested fix
1 |+import functools
2 |+import operator
1 3 | x = [1, 2, 3]
2 4 | y = [4, 5, 6]
3 5 |
--------------------------------------------------------------------------------
6 8 | sum([x, y], [])
7 9 | sum([[1, 2, 3], [4, 5, 6]], start=[])
8 10 | sum([[1, 2, 3], [4, 5, 6]], [])
9 |-sum([[1, 2, 3], [4, 5, 6]],
10 |- [])
11 |+functools.reduce(operator.iadd, [[1, 2, 3], [4, 5, 6]], [])
11 12 |
12 13 | # OK
13 14 | sum([x, y])


Loading