Files
typeshed/stdlib/2and3/contextlib.pyi
nimin98 20d9fcf858 Support callable contextmanagers in contextlib (#1152)
mypy could not recognize the case when we use contextmanager as a
decorator, which has been supported since Python 3.2.

In the following code snippet,

from contextlib import contextmanager

@contextmanager
def foo(arg1):
    try:
        print(arg1)
        print('1')
        yield
    finally:
        print('2')

@foo('0')
def foo2():
    print('3')

foo2()

we get mypy error as follows,
error: ContextManager[Any] not callable

The suggested changes can fix this error and properly reflect the
updated contextmanager usage pattern.
2017-04-11 14:51:03 -07:00

59 lines
2.2 KiB
Python

# Stubs for contextlib
from typing import (
Any, Callable, Generator, IO, Iterable, Iterator, 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: ...
if sys.version_info >= (3, 2):
class GeneratorContextManager(Generic[_T], ContextManager[_T]):
def __call__(self, func: Callable[..., _T]) -> Callable[..., _T]: ...
def contextmanager(func: Callable[..., Iterator[_T]]) -> Callable[..., GeneratorContextManager[_T]]: ...
else:
def contextmanager(func: Callable[..., Iterator[_T]]) -> Callable[..., ContextManager[_T]]: ...
if sys.version_info < (3,):
def nested(*mgr: ContextManager[Any]) -> ContextManager[Iterable[Any]]: ...
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: ...