From e2e59bc37792d3b5bdc590e2ded0689475c8a3c5 Mon Sep 17 00:00:00 2001 From: jnvw Date: Sun, 1 Feb 2026 02:52:00 +0530 Subject: [PATCH] style: format response tuple unpacking (extract helper) --- src/flask/app.py | 52 +++++++++++++++++++++++++++++++----------------- 1 file changed, 34 insertions(+), 18 deletions(-) diff --git a/src/flask/app.py b/src/flask/app.py index c17da4b4..6b0e2c1e 100644 --- a/src/flask/app.py +++ b/src/flask/app.py @@ -1220,6 +1220,38 @@ class Flask(App): return rv + def _unpack_response_tuple( + self, rv: tuple + ) -> tuple[ft.ResponseReturnValue, int | None, HeadersValue | None]: + """Unpack a response tuple into body, status, and headers. + + Valid forms: + - (body, status, headers) + - (body, status) + - (body, headers) + """ + status = None + headers = None + + length = len(rv) + + if length == 3: + body, status, headers = rv + elif length == 2: + body, second = rv + if isinstance(second, (Headers, dict, tuple, list)): + headers = second + else: + status = second + else: + raise TypeError( + "The view function did not return a valid response tuple." + " The tuple must have the form (body, status, headers)," + " (body, status), or (body, headers)." + ) + + return body, status, headers + def make_response(self, rv: ft.ResponseReturnValue) -> Response: """Convert the return value from a view function to an instance of :attr:`response_class`. @@ -1282,24 +1314,8 @@ class Flask(App): # unpack tuple returns if isinstance(rv, tuple): - len_rv = len(rv) - - # a 3-tuple is unpacked directly - if len_rv == 3: - 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 # pyright: ignore - else: - rv, status = rv # type: ignore[assignment,misc] - # other sized tuples are not allowed - else: - raise TypeError( - "The view function did not return a valid response tuple." - " The tuple must have the form (body, status, headers)," - " (body, status), or (body, headers)." - ) + body, status, headers = self._unpack_response_tuple(rv) + rv = body # the body must not be None if rv is None: