Python 3.5+astterm
ast.AsyncWith
async
The node of an async with statement: a list of context managers in items and the body. It is allowed only inside async def.
Syntax
ast.AsyncWith(items, body, type_comment=None)
Each manager in async with a as x, b is a separate withitem with context_expr and optional_vars fields, not a tuple; optional_vars is None when there is no as. The class is separate from With, so visit_With skips it. As with async for, ast.parse accepts it outside a coroutine and compile() reports the error.
Examples
import ast
fn = ast.parse('async def f():\n async with lock, open_db() as db:\n pass').body[0]
node = fn.body[0]
print(type(node).__name__, len(node.items)) # → AsyncWith 2
print(node.items[0].optional_vars, node.items[1].optional_vars.id) # → None db
See also
Open in the full glossarydocs.python.orgReport an inaccuracy