Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[flake8-async] Do not lint yield in context manager cancel-scope-no-checkpoint (ASYNC100) #12896

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,16 @@ async def func():
async def func():
async with asyncio.timeout(delay=0.2), asyncio.timeout(delay=0.2):
...

# Don't trigger for blocks with a yield statement
async def foo():
with trio.fail_after(1):
yield

# https://github.com/astral-sh/ruff/issues/12873
@asynccontextmanager
async def good_code():
with anyio.fail_after(10):
# There's no await keyword here, but we presume that there
# will be in the caller we yield to, so this is safe.
yield
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ use crate::rules::flake8_async::helpers::MethodName;
/// ## What it does
/// Checks for timeout context managers which do not contain a checkpoint.
///
/// For the purposes of this check, `yield` is considered a checkpoint,
/// since checkpoints may occur in the caller to which we yield.
///
/// ## Why is this bad?
/// Some asynchronous context managers, such as `asyncio.timeout` and
/// `trio.move_on_after`, have no effect unless they contain a checkpoint.
Expand Down Expand Up @@ -80,6 +83,17 @@ pub(crate) fn cancel_scope_no_checkpoint(
return;
}

// Treat yields as checkpoints, since checkpoints can happen
// in the caller yielded to.
// https://flake8-async.readthedocs.io/en/latest/rules.html#async100
// https://github.com/astral-sh/ruff/issues/12873
if with_stmt.body.iter().any(|stmt| {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic here only traverses the direct children of a with statement but, for example, not the statements in an if body. Is this intentional? If not, consider using the StatementVisitor

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not intentional, thank you! I tried using any_over_body instead. Unless I'm misunderstanding, that's slightly faster since it gets to stop traversing the tree as soon as a yield is found, right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not familiar with that method. I'll take a look tomorrow

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, correct usage of any_over_body!

stmt.as_expr_stmt()
.is_some_and(|expr| expr.value.is_yield_expr())
}) {
return;
}

// If the body contains an `await` statement, the context manager is used correctly.
let mut visitor = AwaitVisitor::default();
visitor.visit_body(&with_stmt.body);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,6 @@ ASYNC100.py:90:5: ASYNC100 A `with asyncio.timeout(...):` context does not conta
| _____^
91 | | ...
| |___________^ ASYNC100
92 |
93 | # Don't trigger for blocks with a yield statement
|
Loading