Add type hints to tutorial and Javascript examples

- Add return type annotations and parameter
  types
 - Import necessary typing modules (Optional,
 Union, Flask, Response)
 - Improve code readability and IDE support for
   examples
 - All existing tests continue to pass
This commit is contained in:
sebacry3 2025-09-26 11:08:04 -05:00
parent adf363679d
commit 57df4824ea
4 changed files with 26 additions and 17 deletions

View file

@ -1,4 +1,6 @@
import functools
from typing import Callable, Union
from werkzeug.wrappers import Response
from flask import Blueprint
from flask import flash
@ -16,7 +18,7 @@ from .db import get_db
bp = Blueprint("auth", __name__, url_prefix="/auth")
def login_required(view):
def login_required(view: Callable) -> Callable:
"""View decorator that redirects anonymous users to the login page."""
@functools.wraps(view)
@ -30,7 +32,7 @@ def login_required(view):
@bp.before_app_request
def load_logged_in_user():
def load_logged_in_user() -> None:
"""If a user id is stored in the session, load the user object from
the database into ``g.user``."""
user_id = session.get("user_id")
@ -44,7 +46,7 @@ def load_logged_in_user():
@bp.route("/register", methods=("GET", "POST"))
def register():
def register() -> Union[str, Response]:
"""Register a new user.
Validates that the username is not already taken. Hashes the
@ -82,7 +84,7 @@ def register():
@bp.route("/login", methods=("GET", "POST"))
def login():
def login() -> Union[str, Response]:
"""Log in a registered user by adding the user id to the session."""
if request.method == "POST":
username = request.form["username"]
@ -110,7 +112,7 @@ def login():
@bp.route("/logout")
def logout():
def logout() -> Union[str, Response]:
"""Clear the current session, including the stored user id."""
session.clear()
return redirect(url_for("index"))