Fix various type hints

This commit is contained in:
2026-04-05 08:27:40 +08:00
parent 29dfc6c5a4
commit 674b0de3b2
36 changed files with 157 additions and 121 deletions
+23 -11
View File
@@ -352,7 +352,9 @@ class AccountTestCase(unittest.TestCase):
# Success under the same base, with order in a mess.
with self.__app.app_context():
stock_2: Account = Account.find_by_code(f"{STOCK.base_code}-002")
stock_2: Account | None = \
Account.find_by_code(f"{STOCK.base_code}-002")
self.assertIsNotNone(stock_2)
stock_2.no = 66
db.session.commit()
@@ -370,7 +372,8 @@ class AccountTestCase(unittest.TestCase):
f"{STOCK.base_code}-002",
f"{STOCK.base_code}-003"})
account: Account = Account.find_by_code(STOCK.code)
account: Account | None = Account.find_by_code(STOCK.code)
self.assertIsNotNone(account)
self.assertEqual(account.base_code, STOCK.base_code)
self.assertEqual(account.title_l10n, STOCK.title)
@@ -395,7 +398,8 @@ class AccountTestCase(unittest.TestCase):
self.assertEqual(response.headers["Location"], detail_uri)
with self.__app.app_context():
account: Account = Account.find_by_code(CASH.code)
account: Account | None = Account.find_by_code(CASH.code)
self.assertIsNotNone(account)
self.assertEqual(account.base_code, CASH.base_code)
self.assertEqual(account.title_l10n, f"{CASH.title}-1")
@@ -462,7 +466,7 @@ class AccountTestCase(unittest.TestCase):
from accounting.models import Account
detail_uri: str = f"{PREFIX}/{CASH.code}"
update_uri: str = f"{PREFIX}/{CASH.code}/update"
account: Account
account: Account | None
response: httpx.Response
response = self.__client.post(update_uri,
@@ -504,11 +508,12 @@ class AccountTestCase(unittest.TestCase):
csrf_token: str = get_csrf_token(client)
detail_uri: str = f"{PREFIX}/{CASH.code}"
update_uri: str = f"{PREFIX}/{CASH.code}/update"
account: Account
account: Account | None
response: httpx.Response
with self.__app.app_context():
account = Account.find_by_code(CASH.code)
self.assertIsNotNone(account)
self.assertEqual(account.created_by.username, editor_username)
self.assertEqual(account.updated_by.username, editor_username)
@@ -534,11 +539,12 @@ class AccountTestCase(unittest.TestCase):
from accounting.models import Account
detail_uri: str = f"{PREFIX}/{CASH.code}"
update_uri: str = f"{PREFIX}/{CASH.code}/update"
account: Account
account: Account | None
response: httpx.Response
with self.__app.app_context():
account = Account.find_by_code(CASH.code)
self.assertIsNotNone(account)
self.assertEqual(account.title_l10n, CASH.title)
self.assertEqual(account.l10n, [])
@@ -553,6 +559,7 @@ class AccountTestCase(unittest.TestCase):
with self.__app.app_context():
account = Account.find_by_code(CASH.code)
self.assertIsNotNone(account)
self.assertEqual(account.title_l10n, CASH.title)
self.assertEqual({(x.locale, x.title) for x in account.l10n},
{("zh_Hant", f"{CASH.title}-zh_Hant")})
@@ -665,15 +672,20 @@ class AccountTestCase(unittest.TestCase):
f"{PREFIX}/1111-00{i}")
with self.__app.app_context():
account_1: Account = Account.find_by_code("1111-001")
account_1: Account | None = Account.find_by_code("1111-001")
self.assertIsNotNone(account_1)
id_1: int = account_1.id
account_2: Account = Account.find_by_code("1111-002")
account_2: Account | None = Account.find_by_code("1111-002")
self.assertIsNotNone(account_2)
id_2: int = account_2.id
account_3: Account = Account.find_by_code("1111-003")
account_3: Account | None = Account.find_by_code("1111-003")
self.assertIsNotNone(account_3)
id_3: int = account_3.id
account_4: Account = Account.find_by_code("1111-004")
account_4: Account | None = Account.find_by_code("1111-004")
self.assertIsNotNone(account_4)
id_4: int = account_4.id
account_5: Account = Account.find_by_code("1111-005")
account_5: Account | None = Account.find_by_code("1111-005")
self.assertIsNotNone(account_5)
id_5: int = account_5.id
account_1.no = 3
account_2.no = 5
+1 -1
View File
@@ -230,7 +230,7 @@ class ConsoleCommandTestCase(unittest.TestCase):
new_account: Account = Account(
id=new_id(Account),
base_code="1112",
no="2",
no=2,
title_l10n=custom_title,
is_need_offset=False,
created_by_id=creator_pk,
+3 -3
View File
@@ -1,7 +1,7 @@
# The Mia! Accounting Demonstration Website.
# Author: imacat@mail.imacat.idv.tw (imacat), 2023/1/27
# Copyright (c) 2023 imacat.
# Copyright (c) 2023-2026 imacat.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@@ -58,7 +58,7 @@ def show_login_form() -> str | Response:
@bp.post("login", endpoint="login")
def login() -> redirect:
def login() -> Response:
"""Logs in the user.
:return: The redirection to the home page.
@@ -72,7 +72,7 @@ def login() -> redirect:
@bp.post("logout", endpoint="logout")
def logout() -> redirect:
def logout() -> Response:
"""Logs out the user.
:return: The redirection to the home page.
+4 -4
View File
@@ -1,7 +1,7 @@
# The Mia! Accounting Demonstration Website.
# Author: imacat@mail.imacat.idv.tw (imacat), 2023/4/12
# Copyright (c) 2023-2024 imacat.
# Copyright (c) 2023-2026 imacat.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@@ -20,7 +20,7 @@
import datetime as dt
from flask import Flask, Blueprint, url_for, flash, redirect, session, \
render_template, current_app
render_template, current_app, Response
from flask_babel import lazy_gettext
from accounting.utils.timezone import get_tz_today
@@ -45,7 +45,7 @@ def reset() -> str:
@bp.post("sample", endpoint="sample")
@admin_required
def reset_sample() -> redirect:
def reset_sample() -> Response:
"""Resets the sample data.
:return: Redirection to the accounting application.
@@ -60,7 +60,7 @@ def reset_sample() -> redirect:
@bp.post("reset", endpoint="clean-up")
@admin_required
def clean_up() -> redirect:
def clean_up() -> Response:
"""Clean-up the database data.
:return: Redirection to the accounting application.
+1 -1
View File
@@ -165,7 +165,7 @@ def match_journal_entry_detail(location: str) -> int:
:return: The journal entry ID.
:raise AssertionError: When the location is not the journal entry detail.
"""
m: re.Match = re.match(
m: re.Match[str] | None = re.match(
r"^/accounting/journal-entries/(\d+)\?next=", location)
assert m is not None
return int(m.group(1))
+14 -9
View File
@@ -224,13 +224,14 @@ def __mess_up_debit(form: dict[str, str], currency_prefix: str) \
:return: The messed-up form.
"""
key: str
m: re.Match
m: re.Match[str] | None
# Remove the office disbursement
key = [x for x in form
if x.startswith(currency_prefix)
and form[x] == Accounts.OFFICE][0]
m = re.match(r"^((.+-)\d+-)account_code$", key)
assert m is not None
debit_prefix: str = m.group(2)
line_item_prefix: str = m.group(1)
amount: Decimal = Decimal(form[f"{line_item_prefix}amount"])
@@ -265,13 +266,14 @@ def __mess_up_credit(form: dict[str, str], currency_prefix: str) \
:return: The messed-up form.
"""
key: str
m: re.Match
m: re.Match[str] | None
# Remove the sales receipt
key = [x for x in form
if x.startswith(currency_prefix)
and form[x] == Accounts.SALES][0]
m = re.match(r"^((.+-)\d+-)account_code$", key)
assert m is not None
credit_prefix: str = m.group(2)
line_item_prefix: str = m.group(1)
amount: Decimal = Decimal(form[f"{line_item_prefix}amount"])
@@ -304,7 +306,6 @@ def __mess_up_currencies(form: dict[str, str]) -> dict[str, str]:
:return: The messed-up form.
"""
key: str
m: re.Match
# Remove JPY
currency_prefix: str = __get_currency_prefix(form, "JPY")
@@ -312,7 +313,7 @@ def __mess_up_currencies(form: dict[str, str]) -> dict[str, str]:
# Add AUD
indices: set[int] = set()
for key in form:
m = re.match(r"^currency-(\d+)-code$", key)
m: re.Match[str] | None = re.match(r"^currency-(\d+)-code$", key)
if m is not None:
indices.add(int(m.group(1)))
new_index: int = max(indices) + 5 + randbelow(20)
@@ -363,7 +364,8 @@ def __get_line_item_no_key(form: dict[str, str], currency_prefix: str,
key: str = [x for x in form
if x.startswith(currency_prefix)
and form[x] == code][0]
m: re.Match = re.match(r"^(.+-\d+-)account_code$", key)
m: re.Match[str] | None = re.match(r"^(.+-\d+-)account_code$", key)
assert m is not None
return f"{m.group(1)}no"
@@ -375,7 +377,8 @@ def __get_currency_prefix(form: dict[str, str], code: str) -> str:
:return: The prefix of the currency.
"""
key: str = [x for x in form if form[x] == code][0]
m: re.Match = re.match(r"^(.+-)code$", key)
m: re.Match[str] | None = re.match(r"^(.+-)code$", key)
assert m is not None
return m.group(1)
@@ -388,7 +391,7 @@ def set_negative_amount(form: dict[str, str]) -> None:
amount_keys: list[str] = []
prefix: str = ""
for key in form.keys():
m: re.Match = re.match(r"^(.+)-\d+-amount$", key)
m: re.Match[str] | None = re.match(r"^(.+)-\d+-amount$", key)
if m is None:
continue
if prefix != "" and prefix != m.group(1):
@@ -407,7 +410,8 @@ def remove_debit_in_a_currency(form: dict[str, str]) -> None:
:return: None.
"""
key: str = [x for x in form if "-debit-" in x][0]
m: re.Match = re.match(r"^(.+-debit-)", key)
m: re.Match[str] | None = re.match(r"^(.+-debit-)", key)
assert m is not None
keys: set[str] = {x for x in form if x.startswith(m.group(1))}
for key in keys:
del form[key]
@@ -420,7 +424,8 @@ def remove_credit_in_a_currency(form: dict[str, str]) -> None:
:return: None.
"""
key: str = [x for x in form if "-credit-" in x][0]
m: re.Match = re.match(r"^(.+-credit-)", key)
m: re.Match[str] | None = re.match(r"^(.+-credit-)", key)
assert m is not None
keys: set[str] = {x for x in form if x.startswith(m.group(1))}
for key in keys:
del form[key]