don't use AnyStr for ResponseValue type

This commit is contained in:
David Lord 2022-03-15 07:36:21 -07:00
parent 8886328822
commit 190dd4df86
No known key found for this signature in database
GPG key ID: 7A1C87E3F5BC42A8
3 changed files with 15 additions and 9 deletions

View file

@ -1672,13 +1672,13 @@ class Flask(Scaffold):
# a 3-tuple is unpacked directly
if len_rv == 3:
rv, status, headers = rv
rv, status, headers = rv # type: ignore[misc]
# decide if a 2-tuple has status or headers
elif len_rv == 2:
if isinstance(rv[1], (Headers, dict, tuple, list)):
rv, headers = rv
else:
rv, status = rv
rv, status = rv # type: ignore[misc]
# other sized tuples are not allowed
else:
raise TypeError(
@ -1701,7 +1701,11 @@ class Flask(Scaffold):
# 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
rv = self.response_class(rv, status=status, headers=headers)
rv = self.response_class(
rv,
status=status,
headers=headers, # type: ignore[arg-type]
)
status = headers = None
elif isinstance(rv, dict):
rv = jsonify(rv)
@ -1729,13 +1733,13 @@ class Flask(Scaffold):
# prefer the status if it was provided
if status is not None:
if isinstance(status, (str, bytes, bytearray)):
rv.status = status # type: ignore
rv.status = status
else:
rv.status_code = status
# extend existing headers with provided headers
if headers:
rv.headers.update(headers)
rv.headers.update(headers) # type: ignore[arg-type]
return rv

View file

@ -186,7 +186,7 @@ def make_response(*args: t.Any) -> "Response":
return current_app.response_class()
if len(args) == 1:
args = args[0]
return current_app.make_response(args)
return current_app.make_response(args) # type: ignore
def url_for(endpoint: str, **values: t.Any) -> str:

View file

@ -4,14 +4,16 @@ import typing as t
if t.TYPE_CHECKING:
from _typeshed.wsgi import WSGIApplication # noqa: F401
from werkzeug.datastructures import Headers # noqa: F401
from .wrappers import Response # noqa: F401
from werkzeug.wrappers.response import Response # noqa: F401
# The possible types that are directly convertible or are a Response object.
ResponseValue = t.Union[
"Response",
t.AnyStr,
str,
bytes,
t.Dict[str, t.Any], # any jsonify-able dict
t.Generator[t.AnyStr, None, None],
t.Iterator[str],
t.Iterator[bytes],
]
StatusCode = int