Skip to content
Merged
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
18 changes: 18 additions & 0 deletions src/test_typing.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import contextlib
import pickle
import re
import sys
Expand Down Expand Up @@ -1309,6 +1310,21 @@ def __len__(self):
assert len(MMB[KT, VT]()) == 0


class OtherABCTests(TestCase):

@skipUnless(hasattr(typing, 'ContextManager'),
'requires typing.ContextManager')
def test_contextmanager(self):
@contextlib.contextmanager
def manager():
yield 42

cm = manager()
assert isinstance(cm, typing.ContextManager)
assert isinstance(cm, typing.ContextManager[int])
assert not isinstance(42, typing.ContextManager)


class NamedTupleTests(TestCase):

def test_basics(self):
Expand Down Expand Up @@ -1447,6 +1463,8 @@ def test_all(self):
assert 'ValuesView' in a
assert 'cast' in a
assert 'overload' in a
if hasattr(contextlib, 'AbstractContextManager'):
assert 'ContextManager' in a
# Check that io and re are not exported.
assert 'io' not in a
assert 're' not in a
Expand Down
7 changes: 7 additions & 0 deletions src/typing.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import abc
from abc import abstractmethod, abstractproperty
import collections
import contextlib
import functools
import re as stdlib_re # Avoid confusion with the re we export.
import sys
Expand Down Expand Up @@ -1530,6 +1531,12 @@ class ValuesView(MappingView[VT_co], extra=collections_abc.ValuesView):
pass


if hasattr(contextlib, 'AbstractContextManager'):
class ContextManager(Generic[T_co], extra=contextlib.AbstractContextManager):
__slots__ = ()
__all__.append('ContextManager')


class Dict(dict, MutableMapping[KT, VT]):

def __new__(cls, *args, **kwds):
Expand Down