Skip to content

Commit 848753a

Browse files
authored
Use contextlib classes inside contextlib (#6353)
1 parent a2f0dbf commit 848753a

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

stdlib/contextlib.pyi

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ from types import TracebackType
44
from typing import (
55
IO,
66
Any,
7-
AsyncContextManager,
87
AsyncIterator,
98
Awaitable,
109
Callable,
@@ -20,6 +19,8 @@ from typing_extensions import ParamSpec, Protocol
2019

2120
AbstractContextManager = ContextManager
2221
if sys.version_info >= (3, 7):
22+
from typing import AsyncContextManager
23+
2324
AbstractAsyncContextManager = AsyncContextManager
2425

2526
_T = TypeVar("_T")
@@ -29,53 +30,53 @@ _F = TypeVar("_F", bound=Callable[..., Any])
2930
_P = ParamSpec("_P")
3031

3132
_ExitFunc = Callable[[Optional[Type[BaseException]], Optional[BaseException], Optional[TracebackType]], bool]
32-
_CM_EF = TypeVar("_CM_EF", ContextManager[Any], _ExitFunc)
33+
_CM_EF = TypeVar("_CM_EF", AbstractContextManager[Any], _ExitFunc)
3334

34-
class _GeneratorContextManager(ContextManager[_T_co]):
35+
class _GeneratorContextManager(AbstractContextManager[_T_co]):
3536
def __call__(self, func: _F) -> _F: ...
3637

3738
# type ignore to deal with incomplete ParamSpec support in mypy
3839
def contextmanager(func: Callable[_P, Iterator[_T]]) -> Callable[_P, _GeneratorContextManager[_T]]: ... # type: ignore
3940

4041
if sys.version_info >= (3, 7):
41-
def asynccontextmanager(func: Callable[_P, AsyncIterator[_T]]) -> Callable[_P, AsyncContextManager[_T]]: ... # type: ignore
42+
def asynccontextmanager(func: Callable[_P, AsyncIterator[_T]]) -> Callable[_P, AbstractAsyncContextManager[_T]]: ... # type: ignore
4243

4344
class _SupportsClose(Protocol):
4445
def close(self) -> object: ...
4546

4647
_SupportsCloseT = TypeVar("_SupportsCloseT", bound=_SupportsClose)
4748

48-
class closing(ContextManager[_SupportsCloseT]):
49+
class closing(AbstractContextManager[_SupportsCloseT]):
4950
def __init__(self, thing: _SupportsCloseT) -> None: ...
5051

5152
if sys.version_info >= (3, 10):
5253
class _SupportsAclose(Protocol):
5354
def aclose(self) -> Awaitable[object]: ...
5455
_SupportsAcloseT = TypeVar("_SupportsAcloseT", bound=_SupportsAclose)
55-
class aclosing(AsyncContextManager[_SupportsAcloseT]):
56+
class aclosing(AbstractAsyncContextManager[_SupportsAcloseT]):
5657
def __init__(self, thing: _SupportsAcloseT) -> None: ...
5758
_AF = TypeVar("_AF", bound=Callable[..., Awaitable[Any]])
5859
class AsyncContextDecorator:
5960
def __call__(self, func: _AF) -> _AF: ...
6061

61-
class suppress(ContextManager[None]):
62+
class suppress(AbstractContextManager[None]):
6263
def __init__(self, *exceptions: Type[BaseException]) -> None: ...
6364
def __exit__(
6465
self, exctype: Type[BaseException] | None, excinst: BaseException | None, exctb: TracebackType | None
6566
) -> bool: ...
6667

67-
class redirect_stdout(ContextManager[_T_io]):
68+
class redirect_stdout(AbstractContextManager[_T_io]):
6869
def __init__(self, new_target: _T_io) -> None: ...
6970

70-
class redirect_stderr(ContextManager[_T_io]):
71+
class redirect_stderr(AbstractContextManager[_T_io]):
7172
def __init__(self, new_target: _T_io) -> None: ...
7273

7374
class ContextDecorator:
7475
def __call__(self, func: _F) -> _F: ...
7576

76-
class ExitStack(ContextManager[ExitStack]):
77+
class ExitStack(AbstractContextManager[ExitStack]):
7778
def __init__(self) -> None: ...
78-
def enter_context(self, cm: ContextManager[_T]) -> _T: ...
79+
def enter_context(self, cm: AbstractContextManager[_T]) -> _T: ...
7980
def push(self, exit: _CM_EF) -> _CM_EF: ...
8081
def callback(self, __callback: Callable[..., Any], *args: Any, **kwds: Any) -> Callable[..., Any]: ...
8182
def pop_all(self: Self) -> Self: ...
@@ -88,11 +89,11 @@ class ExitStack(ContextManager[ExitStack]):
8889
if sys.version_info >= (3, 7):
8990
_ExitCoroFunc = Callable[[Optional[Type[BaseException]], Optional[BaseException], Optional[TracebackType]], Awaitable[bool]]
9091
_CallbackCoroFunc = Callable[..., Awaitable[Any]]
91-
_ACM_EF = TypeVar("_ACM_EF", AsyncContextManager[Any], _ExitCoroFunc)
92-
class AsyncExitStack(AsyncContextManager[AsyncExitStack]):
92+
_ACM_EF = TypeVar("_ACM_EF", AbstractAsyncContextManager[Any], _ExitCoroFunc)
93+
class AsyncExitStack(AbstractAsyncContextManager[AsyncExitStack]):
9394
def __init__(self) -> None: ...
94-
def enter_context(self, cm: ContextManager[_T]) -> _T: ...
95-
def enter_async_context(self, cm: AsyncContextManager[_T]) -> Awaitable[_T]: ...
95+
def enter_context(self, cm: AbstractContextManager[_T]) -> _T: ...
96+
def enter_async_context(self, cm: AbstractAsyncContextManager[_T]) -> Awaitable[_T]: ...
9697
def push(self, exit: _CM_EF) -> _CM_EF: ...
9798
def push_async_exit(self, exit: _ACM_EF) -> _ACM_EF: ...
9899
def callback(self, __callback: Callable[..., Any], *args: Any, **kwds: Any) -> Callable[..., Any]: ...

0 commit comments

Comments
 (0)