From 25d99041808ce95088a15c321997f8c9c7c9f19e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BE=9D=E7=91=AA=E8=B2=93?= Date: Sun, 3 Mar 2024 07:39:37 +0800 Subject: [PATCH] Applied the new type parameter syntax to the generic classes for Python 3.12. --- .../journal_entry/forms/journal_entry.py | 10 +++------- src/accounting/utils/pagination.py | 14 +++++--------- src/accounting/utils/user.py | 7 ++----- 3 files changed, 10 insertions(+), 21 deletions(-) diff --git a/src/accounting/journal_entry/forms/journal_entry.py b/src/accounting/journal_entry/forms/journal_entry.py index c8884ec..126034f 100644 --- a/src/accounting/journal_entry/forms/journal_entry.py +++ b/src/accounting/journal_entry/forms/journal_entry.py @@ -1,7 +1,7 @@ # The Mia! Accounting Project. # Author: imacat@mail.imacat.idv.tw (imacat), 2023/2/18 -# Copyright (c) 2023 imacat. +# Copyright (c) 2023-2024 imacat. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -19,7 +19,7 @@ """ import datetime as dt from abc import ABC, abstractmethod -from typing import TypeVar, Generic, Type +from typing import Type import sqlalchemy as sa from flask_babel import LazyString @@ -308,11 +308,7 @@ class JournalEntryForm(FlaskForm): return db.session.scalar(select) -T = TypeVar("T", bound=JournalEntryForm) -"""A journal entry form variant.""" - - -class LineItemCollector(Generic[T], ABC): +class LineItemCollector[T: JournalEntryForm](ABC): """The line item collector.""" def __init__(self, form: T, obj: JournalEntry): diff --git a/src/accounting/utils/pagination.py b/src/accounting/utils/pagination.py index 67ea050..b765527 100644 --- a/src/accounting/utils/pagination.py +++ b/src/accounting/utils/pagination.py @@ -1,7 +1,7 @@ # The Mia! Accounting Project. # Author: imacat@mail.imacat.idv.tw (imacat), 2023/1/25 -# Copyright (c) 2023 imacat. +# Copyright (c) 2023-2024 imacat. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -19,7 +19,6 @@ This module should not import any other module from the application. """ -from typing import TypeVar, Generic from urllib.parse import urlparse, parse_qsl, urlencode, urlunparse, \ ParseResult @@ -62,11 +61,8 @@ class Redirection(RequestRedirect): DEFAULT_PAGE_SIZE: int = 10 """The default page size.""" -T = TypeVar("T") -"""The pagination item type.""" - -class Pagination(Generic[T]): +class Pagination[T]: """The pagination utility.""" def __init__(self, items: list[T], is_reversed: bool = False): @@ -92,7 +88,7 @@ class Pagination(Generic[T]): """The options to the number of items in a page.""" -class AbstractPagination(Generic[T]): +class AbstractPagination[T]: """An abstract pagination.""" def __init__(self): @@ -109,12 +105,12 @@ class AbstractPagination(Generic[T]): """The options to the number of items in a page.""" -class EmptyPagination(AbstractPagination[T]): +class EmptyPagination[T](AbstractPagination[T]): """The pagination from empty data.""" pass -class NonEmptyPagination(AbstractPagination[T]): +class NonEmptyPagination[T](AbstractPagination[T]): """The pagination with real data.""" PAGE_SIZE_OPTION_VALUES: list[int] = [10, 100, 200] """The page size options.""" diff --git a/src/accounting/utils/user.py b/src/accounting/utils/user.py index d6d3b87..646d227 100644 --- a/src/accounting/utils/user.py +++ b/src/accounting/utils/user.py @@ -20,17 +20,14 @@ This module should not import any other module from the application. """ from abc import ABC, abstractmethod -from typing import TypeVar, Generic, Type +from typing import Type import sqlalchemy as sa from flask import g, Response from flask_sqlalchemy.model import Model -T = TypeVar("T", bound=Model) -"""The user data model data type.""" - -class UserUtilityInterface(Generic[T], ABC): +class UserUtilityInterface[T: Model](ABC): """The interface for the user utilities.""" @abstractmethod