Skip to content

Fix contextlib.asynccontextmanager to work with coroutine functions #10753

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion stdlib/contextlib.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import abc
import sys
from _typeshed import FileDescriptorOrPath, Unused
from abc import abstractmethod
from collections.abc import AsyncGenerator, AsyncIterator, Awaitable, Callable, Generator, Iterator
from collections.abc import AsyncGenerator, AsyncIterator, Awaitable, Callable, Coroutine, Generator, Iterator
from types import TracebackType
from typing import IO, Any, Generic, Protocol, TypeVar, overload, runtime_checkable
from typing_extensions import ParamSpec, Self, TypeAlias
Expand Down Expand Up @@ -104,6 +104,11 @@ else:
self, typ: type[BaseException] | None, value: BaseException | None, traceback: TracebackType | None
) -> bool | None: ...

@overload
def asynccontextmanager(
func: Callable[_P, Coroutine[Any, Any, AsyncIterator[_T_co]]]
) -> Callable[_P, _AsyncGeneratorContextManager[_T_co]]: ...
@overload
def asynccontextmanager(func: Callable[_P, AsyncIterator[_T_co]]) -> Callable[_P, _AsyncGeneratorContextManager[_T_co]]: ...

class _SupportsClose(Protocol):
Expand Down
17 changes: 16 additions & 1 deletion test_cases/stdlib/check_contextlib.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

from contextlib import ExitStack
from contextlib import ExitStack, asynccontextmanager
from typing import AsyncGenerator
from typing_extensions import assert_type


Expand All @@ -18,3 +19,17 @@ class Thing(ExitStack):
assert_type(cm, ExitStack)
with thing as cm2:
assert_type(cm2, Thing)


@asynccontextmanager
async def async_context() -> AsyncGenerator[str, None]:
yield "example"


async def async_gen() -> AsyncGenerator[str, None]:
yield "async gen"


@asynccontextmanager
def async_cm_func() -> AsyncGenerator[str, None]:
return async_gen()
Comment on lines +24 to +35
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All these test cases appear to pass on main

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, mypy already knows how to solve this on main 👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, mypy already knows how to solve this on main 👍

Then I'm confused. What's the problem we're trying to fix, if these tests all already pass?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeap, since it was fixed on main, we can drop this fix from typeshed