|
| 1 | +# Stubs for contextlib |
| 2 | + |
| 3 | +from typing import ( |
| 4 | + Any, Callable, Generator, IO, Optional, Type, |
| 5 | + Generic, TypeVar, |
| 6 | +) |
| 7 | +from types import TracebackType |
| 8 | +import sys |
| 9 | + |
| 10 | +_T = TypeVar('_T') |
| 11 | +_ExitFunc = Callable[[Optional[Type[BaseException]], |
| 12 | + Optional[Exception], |
| 13 | + Optional[TracebackType]], bool] |
| 14 | +_CM_EF = TypeVar('_CM_EF', ContextManager, _ExitFunc) |
| 15 | + |
| 16 | +# TODO already in PEP, have to get added to mypy |
| 17 | +class ContextManager(Generic[_T]): |
| 18 | + def __enter__(self) -> _T: ... |
| 19 | + def __exit__(self, exc_type: Optional[Type[BaseException]], |
| 20 | + exc_val: Optional[Exception], |
| 21 | + exc_tb: Optional[TracebackType]) -> bool: ... |
| 22 | + |
| 23 | +def contextmanager(func: Callable[..., Generator[None, None, None]]) -> Callable[..., ContextManager[None]]: ... |
| 24 | + |
| 25 | +if sys.version_info < (3,): |
| 26 | + def nested(*mgr: ContextManager[Any]) -> ContextManager[None]: ... |
| 27 | + |
| 28 | +class closing(Generic[_T], ContextManager[_T]): |
| 29 | + def __init__(self, thing: _T) -> None: ... |
| 30 | + |
| 31 | +if sys.version_info >= (3, 4): |
| 32 | + class suppress(ContextManager[None]): |
| 33 | + def __init__(self, *exceptions: Type[BaseException]) -> None: ... |
| 34 | + |
| 35 | + class redirect_stdout(ContextManager[None]): |
| 36 | + def __init__(self, new_target: IO[str]) -> None: ... |
| 37 | + |
| 38 | +if sys.version_info >= (3, 5): |
| 39 | + class redirect_stderr(ContextManager[None]): |
| 40 | + def __init__(self, new_target: IO[str]) -> None: ... |
| 41 | + |
| 42 | +if sys.version_info >= (3,): |
| 43 | + class ContextDecorator: |
| 44 | + def __call__(self, func: Callable[..., None]) -> Callable[..., ContextManager[None]]: ... |
| 45 | + |
| 46 | + class ExitStack(ContextManager[ExitStack]): |
| 47 | + def __init__(self) -> None: ... |
| 48 | + def enter_context(self, cm: ContextManager[_T]) -> _T: ... |
| 49 | + def push(self, exit: _CM_EF) -> _CM_EF: ... |
| 50 | + def callback(self, callback: Callable[..., None], |
| 51 | + *args: Any, **kwds: Any) -> Callable[..., None]: ... |
| 52 | + def pop_all(self) -> ExitStack: ... |
| 53 | + def close(self) -> None: ... |
0 commit comments