diff --git a/packages/pyright-internal/src/tests/samples/with7.py b/packages/pyright-internal/src/tests/samples/with7.py index 5cfce84b8aac..112217c83e93 100644 --- a/packages/pyright-internal/src/tests/samples/with7.py +++ b/packages/pyright-internal/src/tests/samples/with7.py @@ -36,3 +36,25 @@ def test_multi_typevar_context_manager() -> None: reveal_type(ctx, expected_text="int") bar = 1 reveal_type(bar, expected_text="Literal['str', 1]") + + +class AsyncContextManager(Generic[T]): + async def __aenter__(self) -> Self: ... + + async def __aexit__(self, exc_type, exc_val, exc_tb) -> T: ... + + +async def test_generic_async_context_manager() -> None: + # __aexit__ specializes to bool, so the exception may have been suppressed + # and the body may not have run. + baz = "str" + async with AsyncContextManager[bool]() as ctx: + baz = 1 + reveal_type(baz, expected_text="Literal['str', 1]") + + # __aexit__ specializes to None, so the exception is never suppressed and + # the entire body must have run. + qux = "str" + async with AsyncContextManager[None]() as ctx: + qux = 1 + reveal_type(qux, expected_text="Literal[1]")