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

Allow additional arguments for sum and max comprehensions #12364

Merged
merged 1 commit into from
Jul 18, 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
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
sum([x.val for x in bar])
min([x.val for x in bar])
max([x.val for x in bar])
sum([x.val for x in bar], 0)

# Ok
sum(x.val for x in bar)
min(x.val for x in bar)
max(x.val for x in bar)
sum(x.val for x in bar, 0)
15 changes: 9 additions & 6 deletions crates/ruff_linter/src/rules/flake8_comprehensions/fixes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ use std::iter;
use anyhow::{bail, Result};
use itertools::Itertools;
use libcst_native::{
Arg, AssignEqual, AssignTargetExpression, Call, Comment, CompFor, Dict, DictComp, DictElement,
Element, EmptyLine, Expression, GeneratorExp, LeftCurlyBrace, LeftParen, LeftSquareBracket,
ListComp, Name, ParenthesizableWhitespace, ParenthesizedNode, ParenthesizedWhitespace,
RightCurlyBrace, RightParen, RightSquareBracket, SetComp, SimpleString, SimpleWhitespace,
TrailingWhitespace, Tuple,
Arg, AssignEqual, AssignTargetExpression, Call, Comma, Comment, CompFor, Dict, DictComp,
DictElement, Element, EmptyLine, Expression, GeneratorExp, LeftCurlyBrace, LeftParen,
LeftSquareBracket, ListComp, Name, ParenthesizableWhitespace, ParenthesizedNode,
ParenthesizedWhitespace, RightCurlyBrace, RightParen, RightSquareBracket, SetComp,
SimpleString, SimpleWhitespace, TrailingWhitespace, Tuple,
};

use ruff_diagnostics::{Edit, Fix};
Expand Down Expand Up @@ -937,7 +937,10 @@ pub(crate) fn fix_unnecessary_comprehension_in_call(
let whitespace_after_arg = match &call.args[0].comma {
Some(comma) => {
let whitespace_after_comma = comma.whitespace_after.clone();
call.args[0].comma = None;
call.args[0].comma = Some(Comma {
whitespace_after: ParenthesizableWhitespace::default(),
..comma.clone()
});
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without this, we were (1) dropping commas in some tests (see fixture changes), and (2) adding an extra space after the comma when fixing sum([x.val for x in bar], 0) (not sure why?).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I don't remember why this was None but this seems correct to me. It might've been some weird comment placement, not sure though.

whitespace_after_comma
}
_ => call.args[0].whitespace_after_arg.clone(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ pub(crate) fn unnecessary_comprehension_in_call(
if !keywords.is_empty() {
return;
}
let [arg] = args else {
let Some(arg) = args.first() else {
return;
};
let (Expr::ListComp(ast::ExprListComp { elt, .. })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ C419.py:4:5: C419 [*] Unnecessary list comprehension
2 2 | all([x.id for x in bar])
3 3 | any( # first comment
4 |- [x.id for x in bar], # second comment
4 |+ x.id for x in bar # second comment
4 |+ x.id for x in bar, # second comment
5 5 | ) # third comment
6 6 | all( # first comment
7 7 | [x.id for x in bar], # second comment
Expand All @@ -72,7 +72,7 @@ C419.py:7:5: C419 [*] Unnecessary list comprehension
5 5 | ) # third comment
6 6 | all( # first comment
7 |- [x.id for x in bar], # second comment
7 |+ x.id for x in bar # second comment
7 |+ x.id for x in bar, # second comment
8 8 | ) # third comment
9 9 | any({x.id for x in bar})
10 10 |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ C419_1.py:1:5: C419 [*] Unnecessary list comprehension
1 |+sum(x.val for x in bar)
2 2 | min([x.val for x in bar])
3 3 | max([x.val for x in bar])
4 4 |
4 4 | sum([x.val for x in bar], 0)

C419_1.py:2:5: C419 [*] Unnecessary list comprehension
|
1 | sum([x.val for x in bar])
2 | min([x.val for x in bar])
| ^^^^^^^^^^^^^^^^^^^^ C419
3 | max([x.val for x in bar])
4 | sum([x.val for x in bar], 0)
|
= help: Remove unnecessary list comprehension

Expand All @@ -31,17 +32,16 @@ C419_1.py:2:5: C419 [*] Unnecessary list comprehension
2 |-min([x.val for x in bar])
2 |+min(x.val for x in bar)
3 3 | max([x.val for x in bar])
4 4 |
5 5 | # Ok
4 4 | sum([x.val for x in bar], 0)
5 5 |

C419_1.py:3:5: C419 [*] Unnecessary list comprehension
|
1 | sum([x.val for x in bar])
2 | min([x.val for x in bar])
3 | max([x.val for x in bar])
| ^^^^^^^^^^^^^^^^^^^^ C419
4 |
5 | # Ok
4 | sum([x.val for x in bar], 0)
|
= help: Remove unnecessary list comprehension

Expand All @@ -50,6 +50,27 @@ C419_1.py:3:5: C419 [*] Unnecessary list comprehension
2 2 | min([x.val for x in bar])
3 |-max([x.val for x in bar])
3 |+max(x.val for x in bar)
4 4 |
5 5 | # Ok
6 6 | sum(x.val for x in bar)
4 4 | sum([x.val for x in bar], 0)
5 5 |
6 6 | # Ok

C419_1.py:4:5: C419 [*] Unnecessary list comprehension
|
2 | min([x.val for x in bar])
3 | max([x.val for x in bar])
4 | sum([x.val for x in bar], 0)
| ^^^^^^^^^^^^^^^^^^^^ C419
5 |
6 | # Ok
|
= help: Remove unnecessary list comprehension

ℹ Unsafe fix
1 1 | sum([x.val for x in bar])
2 2 | min([x.val for x in bar])
3 3 | max([x.val for x in bar])
4 |-sum([x.val for x in bar], 0)
4 |+sum(x.val for x in bar, 0)
5 5 |
6 6 | # Ok
7 7 | sum(x.val for x in bar)
Loading