From 74171741d10b7bf0d0101f75e05e3b409c20eb62 Mon Sep 17 00:00:00 2001 From: Sietse Snel Date: Fri, 17 Mar 2023 14:34:29 +0100 Subject: [PATCH] Enable MyPy Need to add ignores for the Flask-SQLAlchemy model declarations as a workaround for limitations regarding inheritance from objects, see https://github.com/python/mypy/issues/8603 --- .github/workflows/main.yml | 2 +- yoda_eus/app.py | 24 ++++++++++++------------ yoda_eus/password_complexity.py | 3 ++- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index d7a59cd..200b57f 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -34,7 +34,7 @@ jobs: - name: Check static typing run: | - mypy . --explicit-package-bases || true + mypy yoda_eus --explicit-package-bases - name: Check code for common misspellings run: | diff --git a/yoda_eus/app.py b/yoda_eus/app.py index 9991922..370fbc7 100644 --- a/yoda_eus/app.py +++ b/yoda_eus/app.py @@ -7,7 +7,7 @@ import urllib.parse from datetime import datetime from os import path -from typing import Dict +from typing import Any, Dict import bcrypt from flask import abort, Flask, jsonify, make_response, render_template, request, Response, send_from_directory @@ -22,7 +22,7 @@ db = SQLAlchemy() -class User(db.Model): +class User(db.Model): # type: ignore __tablename__ = "users" id = db.Column(db.Integer, db.Sequence("users_id_seq"), primary_key=True) username = db.Column(db.String(64), nullable=False, unique=True, index=True) @@ -35,7 +35,7 @@ class User(db.Model): user_zones = db.relationship("UserZone", back_populates="user") -class UserZone(db.Model): +class UserZone(db.Model): # type: ignore __tablename__ = "user_zones" user_id = db.Column(db.Integer, db.ForeignKey('users.id'), primary_key=True) inviter_user = db.Column(db.String(255), nullable=False) @@ -44,7 +44,7 @@ class UserZone(db.Model): user = db.relationship("User", back_populates="user_zones") -def create_app(config_filename="flask.cfg", enable_api=True) -> Flask: +def create_app(config_filename: str = "flask.cfg", enable_api: bool = True) -> Flask: # create a minimal app app = Flask(__name__, static_folder="/var/www/yoda/static/", @@ -322,10 +322,10 @@ def process_activate_account_form(hash: str) -> Response: # Sanity checks secret hash if hash is None or hash == "": # Failsafe - params = {"activation_error_message": "Activation link is invalid"} - return render_template('activation-error.html', **params), 403 - else: - params = {"secret_hash": hash} + failed_params = {"activation_error_message": "Activation link is invalid"} + return render_template('activation-error.html', **failed_params), 403 + + params: Dict[str, Any] = {"secret_hash": hash} # Validate secret hash and handle errors users = User.query.filter_by(hash=hash).all() @@ -402,10 +402,10 @@ def process_reset_password_form(hash: str) -> Response: # Sanity checks secret hash if hash is None or hash == "": # Failsafe - params = {"reset_error_message": "Password reset link is invalid"} - return render_template('reset-password-error.html', **params), 403 - else: - params = {"secret_hash": hash} + failed_params = {"reset_error_message": "Password reset link is invalid"} + return render_template('reset-password-error.html', **failed_params), 403 + + params: Dict[str, Any] = {"secret_hash": hash} # Validate secret hash and handle errors users = User.query.filter_by(hash=hash).all() diff --git a/yoda_eus/password_complexity.py b/yoda_eus/password_complexity.py index e5b3a45..db15905 100644 --- a/yoda_eus/password_complexity.py +++ b/yoda_eus/password_complexity.py @@ -2,9 +2,10 @@ __license__ = 'GPLv3, see LICENSE' import string +from typing import List -def check_password_complexity(password: str) -> list: +def check_password_complexity(password: str) -> List[str]: """Checks whether a password meets EUS password complexity requirements. :param password: Password to check