Fixes to ContextManager (#1249)

* add typing.ContextManager for 3.6+ only

This fixes the easier part of #655.

Would it make sense to add a generic typing.ContextManager that exists in any Python version?

* update comment

* fix argument types for ContextManager.__exit__

* add AsyncContextManager

* add @asynccontextmanager

* typing.ContextManager now always exists

* back out async-related changes

Will submit those in a separate PR later

* fix import order

* AbstractContextManager only exists in 3.6+

* AbstractContextManager -> ContextManager
This commit is contained in:
Jelle Zijlstra
2017-05-08 16:21:51 -07:00
committed by Matthias Kramm
parent 385b9c8b66
commit 7dd2f80194
5 changed files with 28 additions and 20 deletions

View File

@@ -1,7 +1,7 @@
# Stubs for typing (Python 2.7)
from abc import abstractmethod, ABCMeta
from types import CodeType, FrameType
from types import CodeType, FrameType, TracebackType
# Definitions of special type checking related constructs. Their definitions
# are not used, so their value does not matter.
@@ -199,6 +199,12 @@ class ValuesView(MappingView, Iterable[_VT_co], Generic[_VT_co]):
def __contains__(self, o: object) -> bool: ...
def __iter__(self) -> Iterator[_VT_co]: ...
class ContextManager(Generic[_T_co]):
def __enter__(self) -> _T_co: ...
def __exit__(self, exc_type: Optional[Type[BaseException]],
exc_value: Optional[BaseException],
traceback: Optional[TracebackType]) -> Optional[bool]: ...
class Mapping(Iterable[_KT], Container[_KT], Sized, Generic[_KT, _VT_co]):
# TODO: We wish the key type could also be covariant, but that doesn't work,
# see discussion in https: //github.com/python/typing/pull/273.