8000 Fixes to ContextManager (#1249) · python/typeshed@7dd2f80 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7dd2f80

Browse files
JelleZijlstramatthiaskramm
authored andcommitted
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
1 parent 385b9c8 commit 7dd2f80

File tree

5 files changed

+28
-20
lines changed

5 files changed

+28
-20
lines changed

stdlib/2/typing.pyi

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Stubs for typing (Python 2.7)
22

33
from abc import abstractmethod, ABCMeta
4-
from types import CodeType, FrameType
4+
from types import CodeType, FrameType, TracebackType
55

66
# Definitions of special type checking related constructs. Their definitions
77
# are not used, so their value does not matter.
@@ -199,6 +199,12 @@ class ValuesView(MappingView, Iterable[_VT_co], Generic[_VT_co]):
199199
def __contains__(self, o: object) -> bool: ...
200200
def __iter__(self) -> Iterator[_VT_co]: ...
201201

202+
class ContextManager(Generic[_T_co]):
203+
def __enter__(self) -> _T_co: ...
204+
def __exit__(self, exc_type: Optional[Type[BaseException]],
205+
exc_value: Optional[BaseException],
206+
traceback: Optional[TracebackType]) -> Optional[bool]: ...
207+
202208
class Mapping(Iterable[_KT], Container[_KT], Sized, Generic[_KT, _VT_co]):
203209
# TODO: We wish the key type could also be covariant, but that doesn't work,
204210
# see discussion in https: //github.com/python/typing/pull/273.

stdlib/2and3/contextlib.pyi

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,23 @@
22

33
from typing import (
44
Any, Callable, Generator, IO, Iterable, Iterator, Optional, Type,
5-
Generic, TypeVar,
5+
Generic, TypeVar
66
)
77
from types import TracebackType
88
import sys
9+
# Aliased here for backwards compatibility; TODO eventually remove this
10+
from typing import ContextManager as ContextManager
11+
12+
if sys.version_info >= (3, 6):
13+
from typing import ContextManager as AbstractContextManager
914

1015
_T = TypeVar('_T')
16+
1117
_ExitFunc = Callable[[Optional[Type[BaseException]],
12-
Optional[Exception],
18+
Optional[BaseException],
1319
O 8000 ptional[TracebackType]], bool]
1420
_CM_EF = TypeVar('_CM_EF', ContextManager, _ExitFunc)
1521

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-
2322
if sys.version_info >= (3, 2):
2423
class GeneratorContextManager(Generic[_T], ContextManager[_T]):
2524
def __call__(self, func: Callable[..., _T]) -> Callable[..., _T]: ...

stdlib/3/typing.pyi

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import sys
44
from abc import abstractmethod, ABCMeta
5-
from types import CodeType, FrameType
5+
from types import CodeType, FrameType, TracebackType
66

77
# Definitions of special type checking related constructs. Their definition
88
# are not used, so their value does not matter.
@@ -127,7 +127,7 @@ class Generator(Iterator[_T_co], Generic[_T_co, _T_contra, _V_co]):
127127
gi_yieldfrom = ... # type: Optional[Generator]
128128

129129
# TODO: Several types should only be defined if sys.python_version >= (3, 5):
130-
# Awaitable, AsyncIterator, AsyncIterable, Coroutine, Collection, ContextManager.
130+
# Awaitable, AsyncIterator, AsyncIterable, Coroutine, Collection.
131131
# See https: //github.com/python/typeshed/issues/655 for why this is not easy.
132132

133133
class Awaitable(Generic[_T_co]):
@@ -281,7 +281,11 @@ class ValuesView(MappingView, Iterable[_VT_co], Generic[_VT_co]):
281281
def __contains__(self, o: object) -> bool: ...
282282
def __iter__(self) -> Iterator[_VT_co]: ...
283283

284-
# TODO: ContextManager (only if contextlib.AbstractContextManager exists)
284+
class ContextManager(Generic[_T_co]):
285+
def __enter__(self) -> _T_co: ...
286+
def __exit__(self, exc_type: Optional[Type[BaseException]],
287+
exc_value: Optional[BaseException],
288+
traceback: Optional[TracebackType]) -> Optional[bool]: ...
285289

286290
class Mapping(_Collection[_KT], Generic[_KT, _VT_co]):
287291
# TODO: We wish the key type could also be covariant, but that doesn't work,

stdlib/3/unittest/__init__.pyi

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33
from typing import (
44
Any, Callable, Dict, Iterable, Iterator, List, Optional, Pattern, Sequence,
55
Set, FrozenSet, TextIO, Tuple, Type, TypeVar, Union, Generic,
6-
overload,
6+
overload, ContextManager
77
)
88
import logging
99
import sys
1010
from types import ModuleType, TracebackType
11-
from contextlib import ContextManager
1211

1312

1413
_T = TypeVar('_T')
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
import contextlib
21
import os
32
import sys
43
import tempfile
5-
from typing import Any, AnyStr, Callable, IO, Iterator, Text
4+
from typing import Any, AnyStr, Callable, ContextManager, IO, Iterator, Text
5+
66
def replace_atomic(src: AnyStr, dst: AnyStr) -> None: ...
77
def move_atomic(src: AnyStr, dst: AnyStr) -> None: ...
88
class AtomicWriter(object):
99
def __init__(self, path: AnyStr, mode: Text='w', overwrite: bool=False) -> None: ...
10-
def open(self) -> contextlib.ContextManager[IO]: ...
11-
def _open(self, get_fileobject: Callable) -> contextlib.ContextManager[IO]: ...
10+
def open(self) -> ContextManager[IO]: ...
11+
def _open(self, get_fileobject: Callable) -> ContextManager[IO]: ...
1212
def get_fileobject(self, dir: AnyStr=None, **kwargs) -> IO: ...
1313
def sync(self, f: IO) -> None: ...
1414
def commit(self, f: IO) -> None: ...
1515
def rollback(self, f: IO) -> None: ...
16-
def atomic_write(path: AnyStr, writer_cls: type=AtomicWriter, **cls_kwargs) -> contextlib.ContextManager[IO]: ...
16+
def atomic_write(path: AnyStr, writer_cls: type=AtomicWriter, **cls_kwargs) -> ContextManager[IO]: ...

0 commit comments

Comments
 (0)
0