From 2509c4dd701abe6fa5ef40847e5127e9505fba3a Mon Sep 17 00:00:00 2001 From: Joseph Montanaro Date: Sun, 7 Dec 2025 08:32:45 -0500 Subject: [PATCH] clean up typing a bit more --- src/amazon.py | 2 +- src/main.py | 52 ++++++++++++++++++++++++++------------------------- 2 files changed, 28 insertions(+), 26 deletions(-) diff --git a/src/amazon.py b/src/amazon.py index e2fa930..67daf41 100644 --- a/src/amazon.py +++ b/src/amazon.py @@ -18,7 +18,7 @@ class AmazonAccount: password: str -def get_accounts(env: dict[str, str] = os.environ) -> list[AmazonAccount]: +def get_accounts(env: os._Environ | dict[str, str] = os.environ) -> list[AmazonAccount]: accts = [] for k, v in env.items(): if k == 'AMAZON_EMAIL': diff --git a/src/main.py b/src/main.py index 3d02c7c..b242cdf 100644 --- a/src/main.py +++ b/src/main.py @@ -16,6 +16,29 @@ import ai import ynab_client +@dataclass +class Context: + sync_days: int + budget_id: str + ynab_token: str + amz_accts: list[tuple[str, str]] + ynab_client: ynab_client.YnabClient + + def __init__( + self, + sync_days: int, + budget_id: str | None = None, + ynab_token: str | None = None, + ): + self.sync_days = sync_days + self.amazon_accts = amazon.get_accounts() + self.ynab_client = ynab_client.YnabClient( + budget_id=budget_id if budget_id else os.environ['YNAB_BUDGET_ID'], + ynab_token=ynab_token if ynab_token else os.environ['YNAB_TOKEN'], + sync_days=sync_days, + ) + + @dataclass class TxCategory: category_name: str | None @@ -31,7 +54,8 @@ def tx_date_diff(amz_tx: Transaction, ynab_tx: TransactionDetail): def is_matchable_ynab(tx: TransactionDetail) -> bool: return ( # we only care about amazon transactions - 'amazon' in tx.payee_name.lower() + tx.payee_name is not None + and 'amazon' in tx.payee_name.lower() # don't categorize them if we already have, or if the user has manually approved and tx.flag_color != 'purple' and not tx.approved @@ -85,7 +109,8 @@ def get_tx_categories( details = item_details[item.title] by_category[details.category].append(item) - subtotal = sum(i.price for i in order.items) + # we have already ensured that order details are fully populated, so i.price should not be None + subtotal = sum(i.price for i in order.items) # type: ignore tx_categories = [] for category_name, items in by_category.items(): category_total = 0 @@ -143,29 +168,6 @@ def build_tx_update( ) -@dataclass -class Context: - sync_days: int - budget_id: str - ynab_token: str - amz_accts: list[tuple[str, str]] - ynab_client: ynab_client.YnabClient - - def __init__( - self, - sync_days: int, - budget_id: str | None = None, - ynab_token: str | None = None, - ): - self.sync_days = sync_days - self.amazon_accts = amazon.get_accounts() - self.ynab_client = ynab_client.YnabClient( - budget_id=budget_id if budget_id else os.environ['YNAB_BUDGET_ID'], - ynab_token=ynab_token if ynab_token else os.environ['YNAB_TOKEN'], - sync_days=sync_days, - ) - - def sync_account(ctx: Context, acct: AmazonAccount): print(f'Syncing transactions from Amazon account: {acct.email}') with amazon.Session(acct) as amz: