Skip to content

Commit

Permalink
Avoid triggering unnecessary-comprehension-any-all for async generators
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Mar 30, 2023
1 parent 54ad939 commit 8fb9b8c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 26 deletions.
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
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: ~

0 comments on commit 8fb9b8c

Please sign in to comment.