Skip to content

Support mixed sync and async context managers in async with statements #134270

Open
@Zac-HD

Description

@Zac-HD

Over the last few years, I've found myself writing a lot of code which leans heavily on context managers, and parenthesized context managers make it quite conventient to do so - in all but one case. If you have mixed synchronous and async context managers, each switch from sync to async costs another level of indentation, making the code noticably harder to read.

I therefore propose that the async with statement should also accept mixed sync and async context managers, so long as there is at least one async context manager.


I've been using a helper function which gets most of the way towards this. I nonetheless think that the feature would be valuable, because:

  • It guides people away from a "convert to async cm" pattern (e.g. as_acm(sync_cm()) as v,), which changes code semantics by adding checkpoints where the async framework can switch tasks - and entering or exiting context managers is an unusally bad time to do that, since they are often responsible for timeouts or error handling.
  • Keeping the as clauses local helps to avoid naming confusion: when adding or deleting code, it's easy to mismatch async with multicontext(some(), contexts()) as (a, b): relative to async with some() as b, contexts as a:. As seen here, it's also more concise.
from contextlib import AsyncExitStack, asynccontextmanager

@asynccontextmanager
async def multicontext(*cms):
    assert any(hasattr(cm, "__aenter__") for cm in cms)
    async with AsyncExitStack() as stack:
        enter, aenter = stack.enter_context, stack.enter_async_context
        yield tuple(
            (enter(cm) if hasattr(cm, "__enter__") else await aenter(cm))
            for cm in cms
        )

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere.

Update: now on discuss.python.org to collect feedback.

Activity

biniona

biniona commented on May 19, 2025

@biniona
Contributor

At sprints - I can take a crack at this!

Discussed with experienced Python maintainers at sprint, and they suggested I put this one down as a first issue. I am relinquishing any and all claim to this!

JelleZijlstra

JelleZijlstra commented on May 20, 2025

@JelleZijlstra
Member

I'm sympathetic to this but I don't think I agree with "This is a minor feature, which does not need previous discussion elsewhere"; it's a change to core language semantics. I feel this would need at least a discuss.python.org discussion and possibly a PEP.

Zac-HD

Zac-HD commented on May 20, 2025

@Zac-HD
ContributorAuthor

Fair enough! I mostly wanted to get something up to discuss at the sprints, if people are generally on board I can open a discuss thread and if needed write a PEP soonish.

JelleZijlstra

JelleZijlstra commented on May 20, 2025

@JelleZijlstra
Member

Sounds good! If you need a sponsor I'm happy to do it.

changed the title [-]Feature Request: support mixed sync and async context managers in `async with` statements[/-] [+]Support mixed sync and async context managers in `async with` statements[/+] on May 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Support mixed sync and async context managers in `async with` statements · Issue #134270 · python/cpython

      Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

      Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant