Skip to content

Commit

Permalink
Accept AsyncIterables being passed to Response
Browse files Browse the repository at this point in the history
  • Loading branch information
mjsir911 committed May 20, 2024
1 parent 2fc6d4f commit 60ff030
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/quart/wrappers/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,19 +102,24 @@ async def __anext__(self) -> bytes:


class IterableBody(ResponseBody):
def __init__(self, iterable: AsyncGenerator[bytes, None] | Iterable) -> None:
def __init__(self, iterable: AsyncIterable[bytes] | Iterable) -> None:
self.iter: AsyncGenerator[bytes, None]
if isasyncgen(iterable):
self.iter = iterable
elif isgenerator(iterable):
self.iter = run_sync_iterable(iterable)
else:

elif isinstance(iterable, AsyncIterable):
async def _aiter() -> AsyncGenerator[bytes, None]:
for data in iterable: # type: ignore
async for data in iterable:
yield data

self.iter = _aiter()
elif isinstance(iterable, Iterable):
async def _aiter() -> AsyncGenerator[bytes, None]:
for data in iterable:
yield data
self.iter = _aiter()
else:
raise ValueError("unreachable?")

async def __aenter__(self) -> IterableBody:
return self
Expand Down Expand Up @@ -262,7 +267,7 @@ class Response(SansIOResponse):

def __init__(
self,
response: ResponseBody | AnyStr | Iterable | None = None,
response: ResponseBody | AnyStr | Iterable | AsyncIterable | None = None,
status: int | None = None,
headers: dict | Headers | None = None,
mimetype: str | None = None,
Expand Down

0 comments on commit 60ff030

Please sign in to comment.