view functions can return generators as responses directly
This commit is contained in:
parent
7f2a0f4806
commit
762382e436
5 changed files with 48 additions and 2 deletions
|
|
@ -5,6 +5,7 @@ import os
|
|||
import sys
|
||||
import typing as t
|
||||
import weakref
|
||||
from collections.abc import Iterator as _abc_Iterator
|
||||
from datetime import timedelta
|
||||
from itertools import chain
|
||||
from threading import Lock
|
||||
|
|
@ -1843,6 +1844,10 @@ class Flask(Scaffold):
|
|||
``dict``
|
||||
A dictionary that will be jsonify'd before being returned.
|
||||
|
||||
``generator`` or ``iterator``
|
||||
A generator that returns ``str`` or ``bytes`` to be
|
||||
streamed as the response.
|
||||
|
||||
``tuple``
|
||||
Either ``(body, status, headers)``, ``(body, status)``, or
|
||||
``(body, headers)``, where ``body`` is any of the other types
|
||||
|
|
@ -1862,6 +1867,12 @@ class Flask(Scaffold):
|
|||
The function is called as a WSGI application. The result is
|
||||
used to create a response object.
|
||||
|
||||
.. versionchanged:: 2.2
|
||||
A generator will be converted to a streaming response.
|
||||
|
||||
.. versionchanged:: 1.1
|
||||
A dict will be converted to a JSON response.
|
||||
|
||||
.. versionchanged:: 0.9
|
||||
Previously a tuple was interpreted as the arguments for the
|
||||
response object.
|
||||
|
|
@ -1900,7 +1911,7 @@ class Flask(Scaffold):
|
|||
|
||||
# make sure the body is an instance of the response class
|
||||
if not isinstance(rv, self.response_class):
|
||||
if isinstance(rv, (str, bytes, bytearray)):
|
||||
if isinstance(rv, (str, bytes, bytearray)) or isinstance(rv, _abc_Iterator):
|
||||
# let the response class set the status and headers instead of
|
||||
# waiting to do it manually, so that the class can handle any
|
||||
# special logic
|
||||
|
|
|
|||
|
|
@ -6,7 +6,9 @@ if t.TYPE_CHECKING: # pragma: no cover
|
|||
from werkzeug.wrappers import Response # noqa: F401
|
||||
|
||||
# The possible types that are directly convertible or are a Response object.
|
||||
ResponseValue = t.Union["Response", str, bytes, t.Dict[str, t.Any]]
|
||||
ResponseValue = t.Union[
|
||||
"Response", str, bytes, t.Dict[str, t.Any], t.Iterator[str], t.Iterator[bytes]
|
||||
]
|
||||
|
||||
# the possible types for an individual HTTP header
|
||||
# This should be a Union, but mypy doesn't pass unless it's a TypeVar.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue