Skip to content

Commit 1bd78d4

Browse files
tharvikmatthiaskramm
authored andcommitted
Improve contextlib (#406)
* remove old, new stubgen * comment everything * contextlib done * use TypeVar instead of overload * py2 done
1 parent 24086b2 commit 1bd78d4

File tree

3 files changed

+53
-34
lines changed

3 files changed

+53
-34
lines changed

stdlib/2.7/contextlib.pyi

Lines changed: 0 additions & 17 deletions
This file was deleted.

stdlib/2and3/contextlib.pyi

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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: ...

stdlib/3/contextlib.pyi

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)