8000 add typing.ContextManager for 3.6+ only by JelleZijlstra · Pull Request #1200 · python/typeshed · GitHub
[go: up one dir, main page]

Skip to content

add typing.ContextManager for 3.6+ only #1200

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 24 additions & 19 deletions stdlib/2and3/contextlib.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -7,50 +7,55 @@ from typing import (
from types import TracebackType
import sys


_T = TypeVar('_T')

if sys.version_info >= (3, 6):
from typing import ContextManager as AbstractContextManager
_ContextManager = AbstractContextManager
else:
class _ContextManager(Generic[_T]):
def __enter__(self) -> _T: ...
def __exit__(self, exc_type: Optional[Type[BaseException]],
exc_val: Optional[BaseException],
exc_tb: Optional[TracebackType]) -> bool: ...

_ExitFunc = Callable[[Optional[Type[BaseException]],
Optional[Exception],
Optional[BaseException],
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: ...
_CM_EF = TypeVar('_CM_EF', _ContextManager, _ExitFunc)

if sys.version_info >= (3, 2):
class GeneratorContextManager(Generic[_T], ContextManager[_T]):
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]]: ...
def contextmanager(func: Callable[..., Iterator[_T]]) -> Callable[..., _ContextManager[_T]]: ...

if sys.version_info < (3,):
def nested(*mgr: ContextManager[Any]) -> ContextManager[Iterable[Any]]: ...
def nested(*mgr: _ContextManager[Any]) -> _ContextManager[Iterable[Any]]: ...

class closing(Generic[_T], ContextManager[_T]):
class closing(Generic[_T], _ContextManager[_T]):
def __init__(self, thing: _T) -> None: ...

if sys.version_info >= (3, 4):
class suppress(ContextManager[None]):
class suppress(_ContextManager[None]):
def __init__(self, *exceptions: Type[BaseException]) -> None: ...

class redirect_stdout(ContextManager[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]):
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]]: ...
def __call__(self, func: Callable[..., None]) -> Callable[..., _ContextManager[None]]: ...

class ExitStack(ContextManager[ExitStack]):
class ExitStack(_ContextManager[ExitStack]):
def __init__(self) -> None: ...
def enter_context(self, cm: ContextManager[_T]) -> _T: ...
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]: ...
Expand Down
9 changes: 7 additions & 2 deletions stdlib/3/typing.pyi
class Mapping(_Collection[_KT], Generic[_KT, _VT_co]):
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import sys
from abc import abstractmethod, ABCMeta
from types import CodeType, FrameType
from types import CodeType, FrameType, TracebackType

# Definitions of special type checking related constructs. Their definition
# are not used, so their value does not matter.
Expand Down Expand Up @@ -281,7 +281,12 @@ class ValuesView(MappingView, Iterable[_VT_co], Generic[_VT_co]):
def __contains__(self, o: object) -> bool: ...
def __iter__(self) -> Iterator[_VT_co]: ...

# TODO: ContextManager (only if contextlib.AbstractContextManager exists)
if sys.version_info >= (3, 6):
class ContextManager(Generic[_T]):
def __enter__(self) -> _T: ...
def __exit__(self, exc_type: Optional[Type[BaseException]],
exc_val: Optional[BaseException],
exc_tb: Optional[TracebackType]) -> bool: ...

# TODO: We wish the key type could also be covariant, but that doesn't work,
Expand Down
4 changes: 2 additions & 2 deletions stdlib/3/unittest/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ from typing import (
import logging
import sys
from types import ModuleType, TracebackType
from contextlib import ContextManager
from contextlib import _ContextManager


_T = TypeVar('_T')
Expand Down Expand Up @@ -40,7 +40,7 @@ class TestCase:
def run(self, result: Optional[TestResult] = ...) -> TestCase: ...
def skipTest(self, reason: Any) -> None: ...
if sys.version_info >= (3, 4):
def subTest(self, msg: Any = ..., **params: Any) -> ContextManager[None]: ...
def subTest(self, msg: Any = ..., **params: Any) -> _ContextManager[None]: ...
def debug(self) -> None: ...
def assertEqual(self, first: Any, second: Any, msg: Any = ...) -> None: ...
def assertNotEqual(self, first: Any, second: Any,
Expand Down
7 changes: 4 additions & 3 deletions third_party/2and3/atomicwrites/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ import os
import sys
import tempfile
from typing import Any, AnyStr, Callable, IO, Iterator, Text

def replace_atomic(src: AnyStr, dst: AnyStr) -> None: ...
def move_atomic(src: AnyStr, dst: AnyStr) -> None: ...
class AtomicWriter(object):
def __init__(self, path: AnyStr, mode: Text='w', overwrite: bool=False) -> None: ...
def open(self) -> contextlib.ContextManager[IO]: ...
def _open(self, get_fileobject: Callable) -> contextlib.ContextManager[IO]: ...
def open(self) -> contextlib._ContextManager[IO]: ...
def _open(self, get_fileobject: Callable) -> contextlib._ContextManager[IO]: ...
def get_fileobject(self, dir: AnyStr=None, **kwargs) -> IO: ...
def sync(self, f: IO) -> None: ...
def commit(self, f: IO) -> None: ...
def rollback(self, f: IO) -> None: ...
def atomic_write(path: AnyStr, writer_cls: type=AtomicWriter, **cls_kwargs) -> contextlib.ContextManager[IO]: ...
def atomic_write(path: AnyStr, writer_cls: type=AtomicWriter, **cls_kwargs) -> contextlib._ContextManager[IO]: ...
0