-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathstreams.py
44 lines (27 loc) · 1.02 KB
/
streams.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import sys
if sys.version_info >= (3, 9):
from collections.abc import AsyncIterator
else:
from typing import AsyncIterator
from typing import Protocol, TypeVar
StreamType = TypeVar("StreamType", covariant=True)
class Stream(Protocol[StreamType]):
async def next(self) -> StreamType: ...
def __aiter__(self) -> AsyncIterator:
return self
async def __anext__(self) -> StreamType:
return await self.next()
class StreamReader(Protocol[StreamType]):
async def read(self) -> StreamType: ...
class StreamSource(Protocol[StreamType]):
async def stream(self) -> Stream[StreamType]: ...
class StreamWithIterator(Stream[StreamType]):
_stream: AsyncIterator[StreamType]
def __init__(self, stream: AsyncIterator[StreamType]):
self._stream = stream
async def next(self) -> StreamType:
return await self._stream.__anext__()
def __aiter__(self):
return self._stream
async def __anext__(self) -> StreamType:
return await self._stream.__anext__()