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

Avoid unnecessary-comprehension-any-all for async generators #3823

Merged
merged 1 commit into from
Mar 30, 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
20 changes: 12 additions & 8 deletions crates/ruff/resources/test/fixtures/flake8_pie/PIE802.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
# no error
all((x.id for x in bar))
all(x.id for x in bar)
all(x.id for x in bar)
any(x.id for x in bar)
any({x.id for x in bar})

# PIE 802
# PIE802
any([x.id for x in bar])
all([x.id for x in bar])
any( # first comment
Expand All @@ -14,3 +7,14 @@
all( # first comment
[x.id for x in bar], # second comment
) # third comment

# OK
Copy link
Member Author

Choose a reason for hiding this comment

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

(Inverted the order of this fixture: we typically put errors up-top, and non-errors on bottom.)

all((x.id for x in bar))
all(x.id for x in bar)
all(x.id for x in bar)
any(x.id for x in bar)
any({x.id for x in bar})


async def f() -> bool:
return all([await use_greeting(greeting) for greeting in await greetings()])
12 changes: 10 additions & 2 deletions crates/ruff/src/rules/flake8_pie/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use ruff_diagnostics::{AlwaysAutofixableViolation, Violation};
use ruff_diagnostics::{Diagnostic, Edit};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::comparable::ComparableExpr;
use ruff_python_ast::helpers::{create_expr, match_trailing_comment, unparse_expr};
use ruff_python_ast::helpers::{any_over_expr, create_expr, match_trailing_comment, unparse_expr};
use ruff_python_ast::types::{Range, RefEquality};
use ruff_python_stdlib::identifiers::is_identifier;
use ruff_python_stdlib::keyword::KWLIST;
Expand Down Expand Up @@ -332,6 +332,11 @@ pub fn unnecessary_spread(checker: &mut Checker, keys: &[Option<Expr>], values:
}
}

/// Return `true` if the `Expr` contains an `await` expression.
fn is_async_generator(expr: &Expr) -> bool {
any_over_expr(expr, &|expr| matches!(expr.node, ExprKind::Await { .. }))
}

/// PIE802
pub fn unnecessary_comprehension_any_all(
checker: &mut Checker,
Expand All @@ -344,7 +349,10 @@ pub fn unnecessary_comprehension_any_all(
if !checker.ctx.is_builtin(id) {
return;
}
if let ExprKind::ListComp { .. } = args[0].node {
if let ExprKind::ListComp { elt, .. } = &args[0].node {
if is_async_generator(elt) {
return;
}
let mut diagnostic =
Diagnostic::new(UnnecessaryComprehensionAnyAll, Range::from(&args[0]));
if checker.patch(diagnostic.kind.rule()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ expression: diagnostics
suggestion: Remove unnecessary list comprehension
fixable: true
location:
row: 9
row: 2
column: 4
end_location:
row: 9
row: 2
column: 23
fix:
edits:
- content: any(x.id for x in bar)
location:
row: 9
row: 2
column: 0
end_location:
row: 9
row: 2
column: 24
parent: ~
- kind:
Expand All @@ -29,19 +29,19 @@ expression: diagnostics
suggestion: Remove unnecessary list comprehension
fixable: true
location:
row: 10
row: 3
column: 4
end_location:
row: 10
row: 3
column: 23
fix:
edits:
- content: all(x.id for x in bar)
location:
row: 10
row: 3
column: 0
end_location:
row: 10
row: 3
column: 24
parent: ~
- kind:
Expand All @@ -50,19 +50,19 @@ expression: diagnostics
suggestion: Remove unnecessary list comprehension
fixable: true
location:
row: 12
row: 5
column: 4
end_location:
row: 12
row: 5
column: 23
fix:
edits:
- content: "any( # first comment\n x.id for x in bar # second comment\n)"
location:
row: 11
row: 4
column: 0
end_location:
row: 13
row: 6
column: 1
parent: ~
- kind:
Expand All @@ -71,19 +71,19 @@ expression: diagnostics
suggestion: Remove unnecessary list comprehension
fixable: true
location:
row: 15
row: 8
column: 4
end_location:
row: 15
row: 8
column: 23
fix:
edits:
- content: "all( # first comment\n x.id for x in bar # second comment\n)"
location:
row: 14
row: 7
column: 0
end_location:
row: 16
row: 9
column: 1
parent: ~