Replaced importing the "typing" module as "t" with importing the individual names in the "typing" module. Since Python 3.9 introduced type hinting generics in standard collections, we do not have as many names to import now. This is also to be consistent with the practices of most major and standard packages and examples.

This commit is contained in:
2023-04-26 18:22:45 +08:00
parent ee5b447c23
commit cda9e4e3c6
25 changed files with 116 additions and 113 deletions

View File

@ -18,8 +18,8 @@
"""
import datetime as dt
import typing as t
from abc import ABC, abstractmethod
from typing import TypeVar, Generic, Type
import sqlalchemy as sa
from flask_babel import LazyString
@ -29,13 +29,13 @@ from wtforms import DateField, FieldList, FormField, TextAreaField, \
from wtforms.validators import DataRequired, ValidationError
from accounting import db
from accounting.journal_entry.utils.account_option import AccountOption
from accounting.journal_entry.utils.description_editor import DescriptionEditor
from accounting.journal_entry.utils.original_line_items import \
get_selectable_original_line_items
from accounting.locale import lazy_gettext
from accounting.models import JournalEntry, Account, JournalEntryLineItem, \
JournalEntryCurrency
from accounting.journal_entry.utils.account_option import AccountOption
from accounting.journal_entry.utils.original_line_items import \
get_selectable_original_line_items
from accounting.journal_entry.utils.description_editor import DescriptionEditor
from accounting.utils.random_id import new_id
from accounting.utils.strip_text import strip_multiline_text
from accounting.utils.user import get_current_user_pk
@ -123,7 +123,7 @@ class JournalEntryForm(FlaskForm):
super().__init__(*args, **kwargs)
self.is_modified: bool = False
"""Whether the journal entry is modified during populate_obj()."""
self.collector: t.Type[LineItemCollector] = LineItemCollector
self.collector: Type[LineItemCollector] = LineItemCollector
"""The line item collector. The default is the base abstract
collector only to provide the correct type. The subclass forms should
provide their own collectors."""
@ -155,7 +155,7 @@ class JournalEntryForm(FlaskForm):
self.__set_date(obj, self.date.data)
obj.note = self.note.data
collector_cls: t.Type[LineItemCollector] = self.collector
collector_cls: Type[LineItemCollector] = self.collector
collector: collector_cls = collector_cls(self, obj)
collector.collect()
@ -309,11 +309,11 @@ class JournalEntryForm(FlaskForm):
return db.session.scalar(select)
T = t.TypeVar("T", bound=JournalEntryForm)
T = TypeVar("T", bound=JournalEntryForm)
"""A journal entry form variant."""
class LineItemCollector(t.Generic[T], ABC):
class LineItemCollector(Generic[T], ABC):
"""The line item collector."""
def __init__(self, form: T, obj: JournalEntry):

View File

@ -18,7 +18,7 @@
"""
import re
import typing as t
from typing import Literal
import sqlalchemy as sa
@ -124,12 +124,12 @@ class DescriptionTag:
class DescriptionType:
"""A description type"""
def __init__(self, type_id: t.Literal["general", "travel", "bus"]):
def __init__(self, type_id: Literal["general", "travel", "bus"]):
"""Constructs a description type.
:param type_id: The type ID, either "general", "travel", or "bus".
"""
self.id: t.Literal["general", "travel", "bus"] = type_id
self.id: Literal["general", "travel", "bus"] = type_id
"""The type ID."""
self.__tag_dict: dict[str, DescriptionTag] = {}
"""A dictionary from the tag name to their corresponding tag."""
@ -181,12 +181,12 @@ class DescriptionRecurring:
class DescriptionDebitCredit:
"""The description on debit or credit."""
def __init__(self, debit_credit: t.Literal["debit", "credit"]):
def __init__(self, debit_credit: Literal["debit", "credit"]):
"""Constructs the description on debit or credit.
:param debit_credit: Either "debit" or "credit".
"""
self.debit_credit: t.Literal["debit", "credit"] = debit_credit
self.debit_credit: Literal["debit", "credit"] = debit_credit
"""Either debit or credit."""
self.general: DescriptionType = DescriptionType("general")
"""The general tags."""
@ -194,14 +194,14 @@ class DescriptionDebitCredit:
"""The travel tags."""
self.bus: DescriptionType = DescriptionType("bus")
"""The bus tags."""
self.__type_dict: dict[t.Literal["general", "travel", "bus"],
self.__type_dict: dict[Literal["general", "travel", "bus"],
DescriptionType] \
= {x.id: x for x in {self.general, self.travel, self.bus}}
"""A dictionary from the type ID to the corresponding tags."""
self.recurring: list[DescriptionRecurring] = []
"""The recurring transactions."""
def add_tag(self, tag_type: t.Literal["general", "travel", "bus"],
def add_tag(self, tag_type: Literal["general", "travel", "bus"],
name: str, account: Account, freq: int) -> None:
"""Adds a tag.
@ -278,7 +278,7 @@ class DescriptionEditor:
accounts: dict[int, Account] \
= {x.id: x for x in Account.query
.filter(Account.id.in_({x.account_id for x in result})).all()}
debit_credit_dict: dict[t.Literal["debit", "credit"],
debit_credit_dict: dict[Literal["debit", "credit"],
DescriptionDebitCredit] \
= {x.debit_credit: x for x in {self.debit, self.credit}}
for row in result:

View File

@ -17,19 +17,19 @@
"""The operators for different journal entry types.
"""
import typing as t
from abc import ABC, abstractmethod
from typing import Type
from flask import render_template, request, abort
from flask_wtf import FlaskForm
from accounting.models import JournalEntry
from accounting.template_globals import default_currency_code
from accounting.utils.journal_entry_types import JournalEntryType
from accounting.journal_entry.forms import JournalEntryForm, \
CashReceiptJournalEntryForm, CashDisbursementJournalEntryForm, \
TransferJournalEntryForm
from accounting.journal_entry.forms.line_item import LineItemForm
from accounting.models import JournalEntry
from accounting.template_globals import default_currency_code
from accounting.utils.journal_entry_types import JournalEntryType
class JournalEntryOperator(ABC):
@ -39,7 +39,7 @@ class JournalEntryOperator(ABC):
@property
@abstractmethod
def form(self) -> t.Type[JournalEntryForm]:
def form(self) -> Type[JournalEntryForm]:
"""Returns the form class.
:return: The form class.
@ -100,7 +100,7 @@ class CashReceiptJournalEntry(JournalEntryOperator):
"""The order when checking the journal entry operator."""
@property
def form(self) -> t.Type[JournalEntryForm]:
def form(self) -> Type[JournalEntryForm]:
"""Returns the form class.
:return: The form class.
@ -170,7 +170,7 @@ class CashDisbursementJournalEntry(JournalEntryOperator):
"""The order when checking the journal entry operator."""
@property
def form(self) -> t.Type[JournalEntryForm]:
def form(self) -> Type[JournalEntryForm]:
"""Returns the form class.
:return: The form class.
@ -243,7 +243,7 @@ class TransferJournalEntry(JournalEntryOperator):
"""The order when checking the journal entry operator."""
@property
def form(self) -> t.Type[JournalEntryForm]:
def form(self) -> Type[JournalEntryForm]:
"""Returns the form class.
:return: The form class.