Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

205
Views
What is the type annotation for a Flask view?

I want to add type annotations to a view function that returns a call to redirect. What does redirect return, and how do I add an annotation for that to my view function?

I thought it might be str, or the redirect function, but I'm not sure.

def setalarm() -> redirect:
    # Retrieves the information to create new alarms.
    return redirect("/")
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

The straightforward answer is to annotate your view with whatever you're writing it to return. In your specific example, redirect returns an instance of werkzeug.wrappers.Response.

from werkzeug.wrappers import Response

def set_alarm() -> Response:
    return redirect()

Rather than figuring out what any given function returns in order to annotate your view, it might seem easier to come up with a Union annotation that represents anything a Flask view is allowed to return. However, Flask doesn't provide typing information, and its dynamic nature makes representing the possibilities difficult.

By default, a Flask view can return:

  • A str or bytes.
  • A subclass of werkzeug.wrappers.BaseResponse.
  • A tuple in one of these forms, where data is any of the other types a Flask view can return:
    • (data,)
    • (data, status), where status can be either an int or a str or bytes.
    • (data, headers), where headers is either a dict, iterable of (key, value) tuples, or a werkzeug.datastructures.Headers object.
    • (data, status, headers)
  • A dict to be converted to JSON. The values should be types that app.json_encoder supports.
  • A WSGI callable.

Flask can support more or different return types by overriding the Flask.make_response method. The data it can serialize to JSON can be extended by overriding Flask.json_encoder. If you have customized Flask's behavior you'll need to customize the type information as well.

Here's a view_return_type that represents the possible return types from a Flask view, ignoring JSON typing. Once you define the type, you can annotate any view with it.

import typing as t
from werkzeug.datastructures import Headers
from werkzeug.wrappers import BaseResponse

_str_bytes = t.Union[str, bytes]
_data_type = t.Union[
    _str_bytes,
    BaseResponse,
    t.Dict[str, t.Any],
    t.Callable[
        [t.Dict[str, t.Any], t.Callable[[str, t.List[t.Tuple[str, str]]], None]], t.Iterable[bytes]
    ],
]
_status_type = t.Union[int, _str_bytes]
_headers_type = t.Union[
    Headers, t.Dict[_str_bytes, _str_bytes], t.Iterable[t.Tuple[_str_bytes, _str_bytes]],
]

view_return_type = t.Union[
    _data_type,
    t.Tuple[_data_type],
    t.Tuple[_data_type, _status_type],
    t.Tuple[_data_type, _headers_type],
    t.Tuple[_data_type, _status_type, _headers_type],
]
@app.route("/users/<int:id>/")
def user_detail(id: int) -> view_return_type:
    ...
over 4 years ago · Santiago Trujillo Report

0

In Flask 2 you can use flask.typing.ResponseReturnValue.

from flask.typing import ResponseReturnValue


@app.get("/")
def index() -> ResponseReturnValue:
    return "OK"
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!