1 # Stubs for contextlib
\r
4 Any, Callable, Generator, IO, Iterable, Iterator, Optional, Type,
\r
7 from types import TracebackType
\r
9 # Aliased here for backwards compatibility; TODO eventually remove this
\r
10 from typing import ContextManager as ContextManager
\r
12 if sys.version_info >= (3, 5):
\r
13 from typing import AsyncContextManager, AsyncIterator
\r
15 if sys.version_info >= (3, 6):
\r
16 from typing import ContextManager as AbstractContextManager
\r
17 if sys.version_info >= (3, 7):
\r
18 from typing import AsyncContextManager as AbstractAsyncContextManager
\r
22 _ExitFunc = Callable[[Optional[Type[BaseException]],
\r
23 Optional[BaseException],
\r
24 Optional[TracebackType]], bool]
\r
25 _CM_EF = TypeVar('_CM_EF', ContextManager, _ExitFunc)
\r
27 if sys.version_info >= (3, 2):
\r
28 class GeneratorContextManager(ContextManager[_T], Generic[_T]):
\r
29 def __call__(self, func: Callable[..., _T]) -> Callable[..., _T]: ...
\r
30 def contextmanager(func: Callable[..., Iterator[_T]]) -> Callable[..., GeneratorContextManager[_T]]: ...
\r
32 def contextmanager(func: Callable[..., Iterator[_T]]) -> Callable[..., ContextManager[_T]]: ...
\r
34 if sys.version_info >= (3, 7):
\r
35 def asynccontextmanager(func: Callable[..., AsyncIterator[_T]]) -> Callable[..., AsyncContextManager[_T]]: ...
\r
37 if sys.version_info < (3,):
\r
38 def nested(*mgr: ContextManager[Any]) -> ContextManager[Iterable[Any]]: ...
\r
40 class closing(ContextManager[_T], Generic[_T]):
\r
41 def __init__(self, thing: _T) -> None: ...
\r
43 if sys.version_info >= (3, 4):
\r
44 class suppress(ContextManager[None]):
\r
45 def __init__(self, *exceptions: Type[BaseException]) -> None: ...
\r
47 class redirect_stdout(ContextManager[None]):
\r
48 def __init__(self, new_target: IO[str]) -> None: ...
\r
50 if sys.version_info >= (3, 5):
\r
51 class redirect_stderr(ContextManager[None]):
\r
52 def __init__(self, new_target: IO[str]) -> None: ...
\r
54 if sys.version_info >= (3,):
\r
55 class ContextDecorator:
\r
56 def __call__(self, func: Callable[..., None]) -> Callable[..., ContextManager[None]]: ...
\r
58 _U = TypeVar('_U', bound='ExitStack')
\r
60 class ExitStack(ContextManager[ExitStack]):
\r
61 def __init__(self) -> None: ...
\r
62 def enter_context(self, cm: ContextManager[_T]) -> _T: ...
\r
63 def push(self, exit: _CM_EF) -> _CM_EF: ...
\r
64 def callback(self, callback: Callable[..., Any],
\r
65 *args: Any, **kwds: Any) -> Callable[..., Any]: ...
\r
66 def pop_all(self: _U) -> _U: ...
\r
67 def close(self) -> None: ...
\r
68 def __enter__(self: _U) -> _U: ...
\r
70 if sys.version_info >= (3, 7):
\r
71 from typing import Awaitable
\r
73 _S = TypeVar('_S', bound='AsyncExitStack')
\r
75 _ExitCoroFunc = Callable[[Optional[Type[BaseException]],
\r
76 Optional[BaseException],
\r
77 Optional[TracebackType]], Awaitable[bool]]
\r
78 _CallbackCoroFunc = Callable[..., Awaitable[Any]]
\r
79 _ACM_EF = TypeVar('_ACM_EF', AsyncContextManager, _ExitCoroFunc)
\r
81 class AsyncExitStack(AsyncContextManager[AsyncExitStack]):
\r
82 def __init__(self) -> None: ...
\r
83 def enter_context(self, cm: ContextManager[_T]) -> _T: ...
\r
84 def enter_async_context(self, cm: AsyncContextManager[_T]) -> Awaitable[_T]: ...
\r
85 def push(self, exit: _CM_EF) -> _CM_EF: ...
\r
86 def push_async_exit(self, exit: _ACM_EF) -> _ACM_EF: ...
\r
87 def callback(self, callback: Callable[..., Any],
\r
88 *args: Any, **kwds: Any) -> Callable[..., Any]: ...
\r
89 def push_async_callback(self, callback: _CallbackCoroFunc,
\r
90 *args: Any, **kwds: Any) -> _CallbackCoroFunc: ...
\r
91 def pop_all(self: _S) -> _S: ...
\r
92 def aclose(self) -> Awaitable[None]: ...
\r
93 def __aenter__(self: _S) -> Awaitable[_S]: ...
\r