forked from orbit-oss/flask
view function is actually type checked
This commit is contained in:
parent
8c6f1d96de
commit
81be290ec8
5 changed files with 64 additions and 46 deletions
|
|
@ -1,6 +1,6 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from http import HTTPStatus
|
||||
from typing import Tuple
|
||||
from typing import Union
|
||||
|
||||
from flask import Flask
|
||||
from flask import jsonify
|
||||
|
|
@ -8,42 +8,51 @@ from flask.templating import render_template
|
|||
from flask.views import View
|
||||
from flask.wrappers import Response
|
||||
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
|
||||
@app.route("/")
|
||||
def hello_world() -> str:
|
||||
@app.route("/str")
|
||||
def hello_str() -> str:
|
||||
return "<p>Hello, World!</p>"
|
||||
|
||||
|
||||
@app.route("/bytes")
|
||||
def hello_bytes() -> bytes:
|
||||
return b"<p>Hello, World!</p>"
|
||||
|
||||
|
||||
@app.route("/json")
|
||||
def hello_world_json() -> Response:
|
||||
def hello_json() -> Response:
|
||||
return jsonify({"response": "Hello, World!"})
|
||||
|
||||
|
||||
@app.route("/status")
|
||||
@app.route("/status/<int:code>")
|
||||
def tuple_status(code: int = 200) -> tuple[str, int]:
|
||||
return "hello", code
|
||||
|
||||
|
||||
@app.route("/status-enum")
|
||||
def tuple_status_enum() -> tuple[str, int]:
|
||||
return "hello", HTTPStatus.OK
|
||||
|
||||
|
||||
@app.route("/headers")
|
||||
def tuple_headers() -> tuple[str, dict[str, str]]:
|
||||
return "Hello, World!", {"Content-Type": "text/plain"}
|
||||
|
||||
|
||||
@app.route("/template")
|
||||
@app.route("/template/<name>")
|
||||
def return_template(name: Union[str, None] = None) -> str:
|
||||
def return_template(name: str | None = None) -> str:
|
||||
return render_template("index.html", name=name)
|
||||
|
||||
|
||||
@app.errorhandler(HTTPStatus.INTERNAL_SERVER_ERROR)
|
||||
def error_500(e) -> Tuple[str, int]:
|
||||
return "<p>Sorry, we are having problems</p>", HTTPStatus.INTERNAL_SERVER_ERROR
|
||||
|
||||
|
||||
@app.before_request
|
||||
def before_request() -> None:
|
||||
app.logger.debug("Executing a sample before_request function")
|
||||
return None
|
||||
|
||||
|
||||
class RenderTemplateView(View):
|
||||
def __init__(self: "RenderTemplateView", template_name: str) -> None:
|
||||
def __init__(self: RenderTemplateView, template_name: str) -> None:
|
||||
self.template_name = template_name
|
||||
|
||||
def dispatch_request(self: "RenderTemplateView") -> str:
|
||||
def dispatch_request(self: RenderTemplateView) -> str:
|
||||
return render_template(self.template_name)
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue