Could we perhaps move the async
keyword inside the parens?
with (
async acquire_lock() as lock,
open('config.json') as f,
async connect_to_db() as db,
temp_directory() as tmpdir,
):
Now you can mix sync and async context managers in a single with
block, and, unlike with the OP’s solution as-is, it’s abundantly clear to anyone reading the code which context managers are async and which aren’t, and the interpreter doesn’t have to waste any time checking, either - it can raise RuntimeError
if any of the context managers are of the wrong kind.
For completeness, I suppose we should probably also allow with async foo as bar
as an equivalent to async with foo as bar
(for the case where you’re only entering one context manager and not a bunch of them).