Skip to content

Commit

Permalink
Respect async with in timeout-without-await (#9859)
Browse files Browse the repository at this point in the history
Closes #9855.
  • Loading branch information
charliermarsh committed Feb 6, 2024
1 parent 75553ab commit daae28e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
17 changes: 13 additions & 4 deletions crates/ruff_linter/resources/test/fixtures/flake8_trio/TRIO100.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
import trio


async def foo():
async def func():
with trio.fail_after():
...

async def foo():

async def func():
with trio.fail_at():
await ...

async def foo():

async def func():
with trio.move_on_after():
...

async def foo():

async def func():
with trio.move_at():
await ...


async def func():
with trio.move_at():
async with trio.open_nursery() as nursery:
...
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,20 @@ source: crates/ruff_linter/src/rules/flake8_trio/mod.rs
---
TRIO100.py:5:5: TRIO100 A `with trio.fail_after(...):` context does not contain any `await` statements. This makes it pointless, as the timeout can only be triggered by a checkpoint.
|
4 | async def foo():
4 | async def func():
5 | with trio.fail_after():
| _____^
6 | | ...
| |___________^ TRIO100
7 |
8 | async def foo():
|

TRIO100.py:13:5: TRIO100 A `with trio.move_on_after(...):` context does not contain any `await` statements. This makes it pointless, as the timeout can only be triggered by a checkpoint.
TRIO100.py:15:5: TRIO100 A `with trio.move_on_after(...):` context does not contain any `await` statements. This makes it pointless, as the timeout can only be triggered by a checkpoint.
|
12 | async def foo():
13 | with trio.move_on_after():
14 | async def func():
15 | with trio.move_on_after():
| _____^
14 | | ...
16 | | ...
| |___________^ TRIO100
15 |
16 | async def foo():
|


6 changes: 6 additions & 0 deletions crates/ruff_python_ast/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,12 @@ impl Visitor<'_> for AwaitVisitor {
fn visit_stmt(&mut self, stmt: &Stmt) {
match stmt {
Stmt::FunctionDef(_) | Stmt::ClassDef(_) => (),
Stmt::With(ast::StmtWith { is_async: true, .. }) => {
self.seen_await = true;
}
Stmt::For(ast::StmtFor { is_async: true, .. }) => {
self.seen_await = true;
}
_ => crate::visitor::walk_stmt(self, stmt),
}
}
Expand Down

0 comments on commit daae28e

Please sign in to comment.