Apply RUF017 when start is passed via position (#6664)

As discussed in
https://github.com/astral-sh/ruff/pull/6489#discussion_r1297858919.
Linking https://github.com/astral-sh/ruff/issues/5073
This commit is contained in:
Shantanu 2023-08-17 17:10:07 -07:00 committed by GitHub
parent 5892c691ea
commit a128fe5148
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 83 additions and 2 deletions

View file

@ -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),

View file

@ -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=[])
@ -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])