Skip to content

Commit

Permalink
[flake8-comprehensions] Do not lint async for comprehensions in `…
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
dylwil3 committed Aug 15, 2024
1 parent 73160dc commit 6dcd743
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,14 @@ async def f() -> bool:

# should not be linted...
sum({x.id for x in bar})


# https://github.com/astral-sh/ruff/issues/12891
from collections.abc import AsyncGenerator


async def test() -> None:
async def async_gen() -> AsyncGenerator[bool, None]:
yield True

assert all([v async for v in async_gen()]) # OK
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,21 @@ pub(crate) fn unnecessary_comprehension_in_call(
let Some(arg) = args.first() else {
return;
};
let (Expr::ListComp(ast::ExprListComp { elt, .. })
| Expr::SetComp(ast::ExprSetComp { elt, .. })) = arg
let (Expr::ListComp(ast::ExprListComp {
elt, generators, ..
})
| Expr::SetComp(ast::ExprSetComp {
elt, generators, ..
})) = arg
else {
return;
};
if contains_await(elt) {
return;
}
if generators.iter().any(|generator| generator.is_async) {
return;
}
let Some(Ok(builtin_function)) = checker
.semantic()
.resolve_builtin_symbol(func)
Expand Down

0 comments on commit 6dcd743

Please sign in to comment.