Skip to content

Commit 6dcd743

Browse files
authored
[flake8-comprehensions] Do not lint async for comprehensions in unnecessary-comprehension-in-call (C419) (#12895)
List and set comprehensions using `async for` cannot be replaced with underlying generators; this PR modifies C419 to skip such comprehensions. Closes #12891.
1 parent 73160dc commit 6dcd743

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

crates/ruff_linter/resources/test/fixtures/flake8_comprehensions/C419.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,14 @@ async def f() -> bool:
5555

5656
# should not be linted...
5757
sum({x.id for x in bar})
58+
59+
60+
# https://github.com/astral-sh/ruff/issues/12891
61+
from collections.abc import AsyncGenerator
62+
63+
64+
async def test() -> None:
65+
async def async_gen() -> AsyncGenerator[bool, None]:
66+
yield True
67+
68+
assert all([v async for v in async_gen()]) # OK

crates/ruff_linter/src/rules/flake8_comprehensions/rules/unnecessary_comprehension_in_call.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,21 @@ pub(crate) fn unnecessary_comprehension_in_call(
100100
let Some(arg) = args.first() else {
101101
return;
102102
};
103-
let (Expr::ListComp(ast::ExprListComp { elt, .. })
104-
| Expr::SetComp(ast::ExprSetComp { elt, .. })) = arg
103+
let (Expr::ListComp(ast::ExprListComp {
104+
elt, generators, ..
105+
})
106+
| Expr::SetComp(ast::ExprSetComp {
107+
elt, generators, ..
108+
})) = arg
105109
else {
106110
return;
107111
};
108112
if contains_await(elt) {
109113
return;
110114
}
115+
if generators.iter().any(|generator| generator.is_async) {
116+
return;
117+
}
111118
let Some(Ok(builtin_function)) = checker
112119
.semantic()
113120
.resolve_builtin_symbol(func)

0 commit comments

Comments
 (0)