Skip to content

contextlib: Remove explicit base class from ExitStack #7963

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

Merged
merged 2 commits into from
May 27, 2022
Merged
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
4 changes: 2 additions & 2 deletions stdlib/contextlib.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ class _RedirectStream(AbstractContextManager[_T_io]):
class redirect_stdout(_RedirectStream[_T_io]): ...
class redirect_stderr(_RedirectStream[_T_io]): ...

class ExitStack(AbstractContextManager[ExitStack]):
class ExitStack:
def __init__(self) -> None: ...
def enter_context(self, cm: AbstractContextManager[_T]) -> _T: ...
def push(self, exit: _CM_EF) -> _CM_EF: ...
Expand All @@ -179,7 +179,7 @@ if sys.version_info >= (3, 7):
_ExitCoroFunc: TypeAlias = Callable[[type[BaseException] | None, BaseException | None, TracebackType | None], Awaitable[bool]]
_ACM_EF = TypeVar("_ACM_EF", bound=AbstractAsyncContextManager[Any] | _ExitCoroFunc)

class AsyncExitStack(AbstractAsyncContextManager[AsyncExitStack]):
class AsyncExitStack:
def __init__(self) -> None: ...
def enter_context(self, cm: AbstractContextManager[_T]) -> _T: ...
async def enter_async_context(self, cm: AbstractAsyncContextManager[_T]) -> _T: ...
Expand Down
18 changes: 18 additions & 0 deletions test_cases/stdlib/test_contextlib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from contextlib import ExitStack
from typing_extensions import assert_type


# See issue #7961
class Thing(ExitStack):
pass


stack = ExitStack()
thing = Thing()
assert_type(stack.enter_context(Thing()), Thing)
assert_type(thing.enter_context(ExitStack()), ExitStack)

with stack as cm:
assert_type(cm, ExitStack)
with thing as cm2:
assert_type(cm2, Thing)