Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

207
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda