[flake8-async] Implement yield-in-context-manager-in-async-generator (ASYNC119)#24644
Merged
Merged
Conversation
…` (ASYNC119) ## Summary Implement ASYNC119 from flake8-async, which detects `yield` inside a context manager (`with` or `async with`) in an async generator function. This pattern is unsafe because the cleanup of the context manager may be delayed until the generator is closed, at which point `await` is no longer allowed. This can lead to resource leaks, RuntimeErrors from structured concurrency violations, or other bugs. See PEP 533 for details. The rule suppresses warnings for functions decorated with: - `@contextlib.asynccontextmanager` - `@pytest.fixture` - `@pytest_asyncio.fixture` These decorators are known to handle async generator cleanup safely. Part of astral-sh#8451 ## Test Plan Added test fixture covering: - Yields inside `with` and `async with` blocks (errors) - Multiple yields per context manager (errors) - Nested `with` blocks (errors) - Yields outside context managers (ok) - Yields in nested sync/async functions (ok, scope boundary) - `@asynccontextmanager`-decorated functions (ok) - `@pytest.fixture`-decorated functions (ok) - `@pytest_asyncio.fixture`-decorated functions (ok) - Sync generators (ok) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
ntBre
reviewed
Apr 14, 2026
ntBre
left a comment
Contributor
There was a problem hiding this comment.
Thanks, this looks reasonable to me from a really quick skim. I still need to do a full review, but I found a couple of nits while reading through to approve the workflows.
|
- Rename yield_in_cm_in_async_gen -> yield_in_context_manager_in_async_generator - Use NEXT_RUFF_VERSION instead of hardcoded 0.15.10 - Fix clippy unnested-or-patterns by nesting pytest/pytest_asyncio Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- Rewrite doc string to distinguish single-yield (context manager) vs multi-yield (true async generator) cases with clear caveats - Update diagnostic message to match flake8-async style - Add trio.as_safe_channel to references Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
ntBre
reviewed
May 18, 2026
ntBre
left a comment
Contributor
There was a problem hiding this comment.
Thanks, this looks good to me! I just had a few minor suggestions about the docs and diagnostic.
yield-in-context-manager-in-async-generator (ASYNC119)flake8-async] Implement yield-in-context-manager-in-async-generator (ASYNC119)
- Split violation message into message() + fix_title() - Add 'Known problems' section documenting aclosing() and pytest limitations - Fix reference link format for aclosing() - Simplify lifetime on enclosing_async_function helper Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
ntBre
approved these changes
Jun 4, 2026
ntBre
left a comment
Contributor
There was a problem hiding this comment.
Thanks! I pushed a couple of small docs tweaks, but this looks good to me.
ntBre
added a commit
that referenced
this pull request
Jun 8, 2026
Summary -- I noticed while extending the regex to match human-readable names that it also didn't cover the ASYNC rules, which have five letters in the rule code. For example, #24644 revealed no new diagnostics in the ecosystem, but a local run on my cached ecosystem repos revealed many diagnostics. This won't show up as a diff in CI on this PR but should help with ecosystem checks on the ASYNC rules in the future.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Implement ASYNC119 from flake8-async, which detects
yieldinside a context manager (withorasync with) in an async generator function.This pattern is unsafe because the cleanup of the context manager may be delayed until the generator is closed, at which point
awaitis no longer allowed. This can lead to resource leaks,RuntimeErrors from structured concurrency violations, or other bugs. See PEP 533 for details.The rule suppresses warnings for functions decorated with:
@contextlib.asynccontextmanager@pytest.fixture@pytest_asyncio.fixtureThese decorators are known to handle async generator cleanup safely.
Part of #8451
Test plan
cargo dev generate-allrunuvx prek run -apasses