mirror of
https://github.com/davidhalter/typeshed.git
synced 2025-12-07 20:54:28 +08:00
Improve contextlib (#406)
* remove old, new stubgen * comment everything * contextlib done * use TypeVar instead of overload * py2 done
This commit is contained in:
committed by
Matthias Kramm
parent
24086b2195
commit
1bd78d4aab
@@ -1,17 +0,0 @@
|
||||
# Stubs for contextlib
|
||||
|
||||
# NOTE: These are incomplete!
|
||||
|
||||
from typing import Callable, Generic, Iterator, TypeVar
|
||||
|
||||
_T = TypeVar('_T')
|
||||
|
||||
class ContextManager(Generic[_T]):
|
||||
def __enter__(self) -> _T: ...
|
||||
def __exit__(self, *exc_info) -> None: ...
|
||||
|
||||
# TODO this doesn't capture the relationship that the returned function's args are the same as func's.
|
||||
def contextmanager(func: Callable[..., Iterator[_T]]) -> Callable[..., ContextManager[_T]]: ...
|
||||
|
||||
class closing(ContextManager[_T], Generic[_T]):
|
||||
def __init__(self, thing: _T) -> None: ...
|
||||
53
stdlib/2and3/contextlib.pyi
Normal file
53
stdlib/2and3/contextlib.pyi
Normal file
@@ -0,0 +1,53 @@
|
||||
# Stubs for contextlib
|
||||
|
||||
from typing import (
|
||||
Any, Callable, Generator, IO, Optional, Type,
|
||||
Generic, TypeVar,
|
||||
)
|
||||
from types import TracebackType
|
||||
import sys
|
||||
|
||||
_T = TypeVar('_T')
|
||||
_ExitFunc = Callable[[Optional[Type[BaseException]],
|
||||
Optional[Exception],
|
||||
Optional[TracebackType]], bool]
|
||||
_CM_EF = TypeVar('_CM_EF', ContextManager, _ExitFunc)
|
||||
|
||||
# TODO already in PEP, have to get added to mypy
|
||||
class ContextManager(Generic[_T]):
|
||||
def __enter__(self) -> _T: ...
|
||||
def __exit__(self, exc_type: Optional[Type[BaseException]],
|
||||
exc_val: Optional[Exception],
|
||||
exc_tb: Optional[TracebackType]) -> bool: ...
|
||||
|
||||
def contextmanager(func: Callable[..., Generator[None, None, None]]) -> Callable[..., ContextManager[None]]: ...
|
||||
|
||||
if sys.version_info < (3,):
|
||||
def nested(*mgr: ContextManager[Any]) -> ContextManager[None]: ...
|
||||
|
||||
class closing(Generic[_T], ContextManager[_T]):
|
||||
def __init__(self, thing: _T) -> None: ...
|
||||
|
||||
if sys.version_info >= (3, 4):
|
||||
class suppress(ContextManager[None]):
|
||||
def __init__(self, *exceptions: Type[BaseException]) -> None: ...
|
||||
|
||||
class redirect_stdout(ContextManager[None]):
|
||||
def __init__(self, new_target: IO[str]) -> None: ...
|
||||
|
||||
if sys.version_info >= (3, 5):
|
||||
class redirect_stderr(ContextManager[None]):
|
||||
def __init__(self, new_target: IO[str]) -> None: ...
|
||||
|
||||
if sys.version_info >= (3,):
|
||||
class ContextDecorator:
|
||||
def __call__(self, func: Callable[..., None]) -> Callable[..., ContextManager[None]]: ...
|
||||
|
||||
class ExitStack(ContextManager[ExitStack]):
|
||||
def __init__(self) -> None: ...
|
||||
def enter_context(self, cm: ContextManager[_T]) -> _T: ...
|
||||
def push(self, exit: _CM_EF) -> _CM_EF: ...
|
||||
def callback(self, callback: Callable[..., None],
|
||||
*args: Any, **kwds: Any) -> Callable[..., None]: ...
|
||||
def pop_all(self) -> ExitStack: ...
|
||||
def close(self) -> None: ...
|
||||
@@ -1,17 +0,0 @@
|
||||
# Stubs for contextlib
|
||||
|
||||
# NOTE: These are incomplete!
|
||||
|
||||
from typing import Callable, Generic, Iterator, TypeVar
|
||||
|
||||
_T = TypeVar('_T')
|
||||
|
||||
class ContextManager(Generic[_T]):
|
||||
def __enter__(self) -> _T: ...
|
||||
def __exit__(self, *exc_info) -> None: ...
|
||||
|
||||
# TODO this doesn't capture the relationship that the returned function's args are the same as func's.
|
||||
def contextmanager(func: Callable[..., Iterator[_T]]) -> Callable[..., ContextManager[_T]]: ...
|
||||
|
||||
class closing(ContextManager[_T], Generic[_T]):
|
||||
def __init__(self, thing: _T) -> None: ...
|
||||
Reference in New Issue
Block a user