2 from _typeshed import Self, StrOrBytesPath
3 from types import TracebackType
18 from typing_extensions import ParamSpec, Protocol
20 AbstractContextManager = ContextManager
21 if sys.version_info >= (3, 7):
22 from typing import AsyncContextManager
24 AbstractAsyncContextManager = AsyncContextManager
27 _T_co = TypeVar("_T_co", covariant=True)
28 _T_io = TypeVar("_T_io", bound=Optional[IO[str]])
29 _F = TypeVar("_F", bound=Callable[..., Any])
32 _ExitFunc = Callable[[Optional[Type[BaseException]], Optional[BaseException], Optional[TracebackType]], bool]
33 _CM_EF = TypeVar("_CM_EF", AbstractContextManager[Any], _ExitFunc)
35 class _GeneratorContextManager(AbstractContextManager[_T_co]):
36 def __call__(self, func: _F) -> _F: ...
38 # type ignore to deal with incomplete ParamSpec support in mypy
39 def contextmanager(func: Callable[_P, Iterator[_T]]) -> Callable[_P, _GeneratorContextManager[_T]]: ... # type: ignore
41 if sys.version_info >= (3, 7):
42 def asynccontextmanager(func: Callable[_P, AsyncIterator[_T]]) -> Callable[_P, AbstractAsyncContextManager[_T]]: ... # type: ignore
44 class _SupportsClose(Protocol):
45 def close(self) -> object: ...
47 _SupportsCloseT = TypeVar("_SupportsCloseT", bound=_SupportsClose)
49 class closing(AbstractContextManager[_SupportsCloseT]):
50 def __init__(self, thing: _SupportsCloseT) -> None: ...
52 if sys.version_info >= (3, 10):
53 class _SupportsAclose(Protocol):
54 def aclose(self) -> Awaitable[object]: ...
55 _SupportsAcloseT = TypeVar("_SupportsAcloseT", bound=_SupportsAclose)
56 class aclosing(AbstractAsyncContextManager[_SupportsAcloseT]):
57 def __init__(self, thing: _SupportsAcloseT) -> None: ...
58 _AF = TypeVar("_AF", bound=Callable[..., Awaitable[Any]])
59 class AsyncContextDecorator:
60 def __call__(self, func: _AF) -> _AF: ...
62 class suppress(AbstractContextManager[None]):
63 def __init__(self, *exceptions: Type[BaseException]) -> None: ...
65 self, exctype: Type[BaseException] | None, excinst: BaseException | None, exctb: TracebackType | None
68 class redirect_stdout(AbstractContextManager[_T_io]):
69 def __init__(self, new_target: _T_io) -> None: ...
71 class redirect_stderr(AbstractContextManager[_T_io]):
72 def __init__(self, new_target: _T_io) -> None: ...
74 class ContextDecorator:
75 def __call__(self, func: _F) -> _F: ...
77 class ExitStack(AbstractContextManager[ExitStack]):
78 def __init__(self) -> None: ...
79 def enter_context(self, cm: AbstractContextManager[_T]) -> _T: ...
80 def push(self, exit: _CM_EF) -> _CM_EF: ...
81 def callback(self, __callback: Callable[..., Any], *args: Any, **kwds: Any) -> Callable[..., Any]: ...
82 def pop_all(self: Self) -> Self: ...
83 def close(self) -> None: ...
84 def __enter__(self: Self) -> Self: ...
86 self, __exc_type: Type[BaseException] | None, __exc_value: BaseException | None, __traceback: TracebackType | None
89 if sys.version_info >= (3, 7):
90 _ExitCoroFunc = Callable[[Optional[Type[BaseException]], Optional[BaseException], Optional[TracebackType]], Awaitable[bool]]
91 _CallbackCoroFunc = Callable[..., Awaitable[Any]]
92 _ACM_EF = TypeVar("_ACM_EF", AbstractAsyncContextManager[Any], _ExitCoroFunc)
93 class AsyncExitStack(AbstractAsyncContextManager[AsyncExitStack]):
94 def __init__(self) -> None: ...
95 def enter_context(self, cm: AbstractContextManager[_T]) -> _T: ...
96 def enter_async_context(self, cm: AbstractAsyncContextManager[_T]) -> Awaitable[_T]: ...
97 def push(self, exit: _CM_EF) -> _CM_EF: ...
98 def push_async_exit(self, exit: _ACM_EF) -> _ACM_EF: ...
99 def callback(self, __callback: Callable[..., Any], *args: Any, **kwds: Any) -> Callable[..., Any]: ...
100 def push_async_callback(self, __callback: _CallbackCoroFunc, *args: Any, **kwds: Any) -> _CallbackCoroFunc: ...
101 def pop_all(self: Self) -> Self: ...
102 def aclose(self) -> Awaitable[None]: ...
103 def __aenter__(self: Self) -> Awaitable[Self]: ...
105 self, __exc_type: Type[BaseException] | None, __exc_value: BaseException | None, __traceback: TracebackType | None
106 ) -> Awaitable[bool]: ...
108 if sys.version_info >= (3, 10):
109 class nullcontext(AbstractContextManager[_T], AbstractAsyncContextManager[_T]):
112 def __init__(self: nullcontext[None], enter_result: None = ...) -> None: ...
114 def __init__(self: nullcontext[_T], enter_result: _T) -> None: ...
115 def __enter__(self) -> _T: ...
116 def __exit__(self, *exctype: Any) -> None: ...
117 async def __aenter__(self) -> _T: ...
118 async def __aexit__(self, *exctype: Any) -> None: ...
120 elif sys.version_info >= (3, 7):
121 class nullcontext(AbstractContextManager[_T]):
124 def __init__(self: nullcontext[None], enter_result: None = ...) -> None: ...
126 def __init__(self: nullcontext[_T], enter_result: _T) -> None: ...
127 def __enter__(self) -> _T: ...
128 def __exit__(self, *exctype: Any) -> None: ...
130 if sys.version_info >= (3, 11):
131 _T_fd_or_any_path = TypeVar("_T_fd_or_any_path", bound=int | StrOrBytesPath)
132 class chdir(AbstractContextManager[None], Generic[_T_fd_or_any_path]):
133 path: _T_fd_or_any_path
134 def __init__(self, path: _T_fd_or_any_path) -> None: ...
135 def __enter__(self) -> None: ...
136 def __exit__(self, *excinfo: object) -> None: ...