Skip to content
Closed
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
14 changes: 9 additions & 5 deletions crates/pyrefly_python/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,12 +330,16 @@ impl Ast {
}

pub fn contains_await(expr: &Expr) -> bool {
let mut found = false;
expr.visit(&mut |node: &Expr| {
// Iterative DFS to safely visit all nodes, including the root and nested children.
let mut stack = vec![expr];

while let Some(node) = stack.pop() {
if matches!(node, Expr::Await(_)) {
found = true;
return true;
}
});
found
node.recurse(&mut |child| stack.push(child));
}

false
}
}
16 changes: 16 additions & 0 deletions pyrefly/lib/test/yields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,3 +483,19 @@ def f(start, iterable: Iterable[_T], step) -> Iterator[_T]:

"#,
);

testcase!(
test_async_generator_nested_await_in_condition,
r#"
from typing import AsyncGenerator, assert_type

async def some_async_func(x: int) -> bool:
return True

async def main() -> None:
# Regression test for Issue #1611
# Previously inferred as Generator (sync) because await was nested in comparison
generator = (x for x in [1, 2, 3] if await some_async_func(x) == True)
assert_type(generator, AsyncGenerator[int, None])
"#,
);
Loading