Skip to content

Commit b577749

Browse files
committed
make ExitStack/AsyncExitStack not appear as abstract
1 parent b0401bc commit b577749

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

stdlib/contextlib.pyi

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,15 @@ class _BaseExitStack(Generic[_ExitT_co]):
178178
def callback(self, callback: Callable[_P, _T], /, *args: _P.args, **kwds: _P.kwargs) -> Callable[_P, _T]: ...
179179
def pop_all(self) -> Self: ...
180180

181-
# In reality this is a subclass of `AbstractContextManager`;
182-
# see #7961 for why we don't do that in the stub
183-
class ExitStack(_BaseExitStack[_ExitT_co], metaclass=abc.ABCMeta):
181+
# this class is to avoid putting `metaclass=abc.ABCMeta` on the implementations directly, as this would make them
182+
# appear explicitly abstract to some tools. this is due to the implementations not subclassing `AbstractContextManager`
183+
# see note on the subclasses
184+
@type_check_only
185+
class _BaseExitStackAbstract(_BaseExitStack[_ExitT_co], metaclass=abc.ABCMeta): ...
186+
187+
# In reality this is a subclass of `AbstractContextManager`, but we can't provide `Self` as the argument for `__enter__`
188+
# https://discuss.python.org/t/self-as-typevar-default/90939
189+
class ExitStack(_BaseExitStackAbstract[_ExitT_co]):
184190
def close(self) -> None: ...
185191
def __enter__(self) -> Self: ...
186192
def __exit__(
@@ -192,9 +198,9 @@ _ExitCoroFunc: TypeAlias = Callable[
192198
]
193199
_ACM_EF = TypeVar("_ACM_EF", bound=AbstractAsyncContextManager[Any, Any] | _ExitCoroFunc)
194200

195-
# In reality this is a subclass of `AbstractAsyncContextManager`;
196-
# see #7961 for why we don't do that in the stub
197-
class AsyncExitStack(_BaseExitStack[_ExitT_co], metaclass=abc.ABCMeta):
201+
# In reality this is a subclass of `AbstractContextManager`, but we can't provide `Self` as the argument for `__enter__`
202+
# https://discuss.python.org/t/self-as-typevar-default/90939
203+
class AsyncExitStack(_BaseExitStackAbstract[_ExitT_co]):
198204
async def enter_async_context(self, cm: AbstractAsyncContextManager[_T, _ExitT_co]) -> _T: ...
199205
def push_async_exit(self, exit: _ACM_EF) -> _ACM_EF: ...
200206
def push_async_callback(

0 commit comments

Comments
 (0)