# Async with statements ## Basic `async with` statement The type of the target variable in a `with` statement should be the return type from the context manager's `__aenter__` method. However, `async with` statements aren't supported yet. This test asserts that it doesn't emit any context manager-related errors. ```py class Target: ... class Manager: async def __aenter__(self) -> Target: return Target() async def __aexit__(self, exc_type, exc_value, traceback): ... async def test(): async with Manager() as f: reveal_type(f) # revealed: Target ``` ## Multiple targets ```py class Manager: async def __aenter__(self) -> tuple[int, str]: return 42, "hello" async def __aexit__(self, exc_type, exc_value, traceback): ... async def test(): async with Manager() as (x, y): reveal_type(x) # revealed: int reveal_type(y) # revealed: str ``` ## `@asynccontextmanager` ```py from contextlib import asynccontextmanager from typing import AsyncGenerator class Session: ... @asynccontextmanager async def connect() -> AsyncGenerator[Session]: yield Session() # TODO: this should be `() -> _AsyncGeneratorContextManager[Session, None]` reveal_type(connect) # revealed: (...) -> _AsyncGeneratorContextManager[Unknown, None] async def main(): async with connect() as session: # TODO: should be `Session` reveal_type(session) # revealed: Unknown ``` ## `asyncio.timeout` ```toml [environment] python-version = "3.11" ``` ```py import asyncio async def long_running_task(): await asyncio.sleep(5) async def main(): async with asyncio.timeout(1): await long_running_task() ``` ## `asyncio.TaskGroup` ```toml [environment] python-version = "3.11" ``` ```py import asyncio async def long_running_task(): await asyncio.sleep(5) async def main(): async with asyncio.TaskGroup() as tg: # TODO: should be `TaskGroup` reveal_type(tg) # revealed: Unknown tg.create_task(long_running_task()) ```