]> git.ipfire.org Git - thirdparty/starlette.git/commitdiff
add type annotations to StreamingResponse (#1708)
authorAdrian Garcia Badaracco <1755071+adriangb@users.noreply.github.com>
Sun, 26 Jun 2022 21:00:08 +0000 (14:00 -0700)
committerGitHub <noreply@github.com>
Sun, 26 Jun 2022 21:00:08 +0000 (14:00 -0700)
starlette/responses.py

index 98c8caf1b51927a4e5c88f74467f18c3fd89476d..d6a9462b8cff4ee1219a04fbff111c7cd77f978a 100644 (file)
@@ -215,10 +215,18 @@ class RedirectResponse(Response):
         self.headers["location"] = quote(str(url), safe=":/%#?=@[]!$&'()*+,;")
 
 
+Content = typing.Union[str, bytes]
+SyncContentStream = typing.Iterator[Content]
+AsyncContentStream = typing.AsyncIterable[Content]
+ContentStream = typing.Union[AsyncContentStream, SyncContentStream]
+
+
 class StreamingResponse(Response):
+    body_iterator: AsyncContentStream
+
     def __init__(
         self,
-        content: typing.Any,
+        content: ContentStream,
         status_code: int = 200,
         headers: typing.Optional[typing.Mapping[str, str]] = None,
         media_type: typing.Optional[str] = None,
@@ -257,7 +265,7 @@ class StreamingResponse(Response):
     async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
         async with anyio.create_task_group() as task_group:
 
-            async def wrap(func: typing.Callable[[], typing.Coroutine]) -> None:
+            async def wrap(func: "typing.Callable[[], typing.Awaitable[None]]") -> None:
                 await func()
                 task_group.cancel_scope.cancel()