Skip to content

Commit c35ec8b

Browse files
contextlib: Remove explicit base class from ExitStack (#7963)
Fixes #7961
1 parent 2d2b34c commit c35ec8b

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

stdlib/contextlib.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ class _RedirectStream(AbstractContextManager[_T_io]):
163163
class redirect_stdout(_RedirectStream[_T_io]): ...
164164
class redirect_stderr(_RedirectStream[_T_io]): ...
165165

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

182-
class AsyncExitStack(AbstractAsyncContextManager[AsyncExitStack]):
182+
class AsyncExitStack:
183183
def __init__(self) -> None: ...
184184
def enter_context(self, cm: AbstractContextManager[_T]) -> _T: ...
185185
async def enter_async_context(self, cm: AbstractAsyncContextManager[_T]) -> _T: ...

test_cases/stdlib/test_contextlib.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from contextlib import ExitStack
2+
from typing_extensions import assert_type
3+
4+
5+
# See issue #7961
6+
class Thing(ExitStack):
7+
pass
8+
9+
10+
stack = ExitStack()
11+
thing = Thing()
12+
assert_type(stack.enter_context(Thing()), Thing)
13+
assert_type(thing.enter_context(ExitStack()), ExitStack)
14+
15+
with stack as cm:
16+
assert_type(cm, ExitStack)
17+
with thing as cm2:
18+
assert_type(cm2, Thing)

0 commit comments

Comments
 (0)