summaryrefslogtreecommitdiffstats
path: root/Lib/_collections_abc.py
diff options
context:
space:
mode:
authorYury Selivanov <yselivanov@sprymix.com>2015-05-14 16:19:16 (GMT)
committerYury Selivanov <yselivanov@sprymix.com>2015-05-14 16:19:16 (GMT)
commite0104ae1034eff71539e487caaab35365f08f181 (patch)
tree3cd672d0ae8a9eb77f980cbf1079704a467ea5df /Lib/_collections_abc.py
parent7d0d6ee525a6c75fc3a48105dcd448673bcf4a44 (diff)
downloadcpython-e0104ae1034eff71539e487caaab35365f08f181.zip
cpython-e0104ae1034eff71539e487caaab35365f08f181.tar.gz
cpython-e0104ae1034eff71539e487caaab35365f08f181.tar.bz2
Issue 24184: Add AsyncIterator and AsyncIterable to collections.abc.
Diffstat (limited to 'Lib/_collections_abc.py')
-rw-r--r--Lib/_collections_abc.py39
1 files changed, 38 insertions, 1 deletions
diff --git a/Lib/_collections_abc.py b/Lib/_collections_abc.py
index ea80403..2f83543 100644
--- a/Lib/_collections_abc.py
+++ b/Lib/_collections_abc.py
@@ -9,7 +9,7 @@ Unit tests are in test_collections.
from abc import ABCMeta, abstractmethod
import sys
-__all__ = ["Awaitable", "Coroutine",
+__all__ = ["Awaitable", "Coroutine", "AsyncIterable", "AsyncIterator",
"Hashable", "Iterable", "Iterator", "Generator",
"Sized", "Container", "Callable",
"Set", "MutableSet",
@@ -148,6 +148,43 @@ class Awaitable(metaclass=_CoroutineMeta):
Awaitable.register(Coroutine)
+class AsyncIterable(metaclass=ABCMeta):
+
+ __slots__ = ()
+
+ @abstractmethod
+ async def __aiter__(self):
+ return AsyncIterator()
+
+ @classmethod
+ def __subclasshook__(cls, C):
+ if cls is AsyncIterable:
+ if any("__aiter__" in B.__dict__ for B in C.__mro__):
+ return True
+ return NotImplemented
+
+
+class AsyncIterator(AsyncIterable):
+
+ __slots__ = ()
+
+ @abstractmethod
+ async def __anext__(self):
+ """Return the next item or raise StopAsyncIteration when exhausted."""
+ raise StopAsyncIteration
+
+ async def __aiter__(self):
+ return self
+
+ @classmethod
+ def __subclasshook__(cls, C):
+ if cls is AsyncIterator:
+ if (any("__anext__" in B.__dict__ for B in C.__mro__) and
+ any("__aiter__" in B.__dict__ for B in C.__mro__)):
+ return True
+ return NotImplemented
+
+
class Iterable(metaclass=ABCMeta):
__slots__ = ()