From d9d649d430d81188b0c1b5b0155935c0e5ac14e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20Marussy?= Date: Mon, 25 Jan 2021 01:55:43 +0100 Subject: Fix type errors --- beancount_extras_kris7t/importers/otpbank/otpbank_csv.py | 3 ++- beancount_extras_kris7t/importers/transferwise/__main__.py | 2 +- beancount_extras_kris7t/importers/transferwise/client.py | 12 +++++++----- .../importers/transferwise/transferwise_json.py | 2 ++ beancount_extras_kris7t/plugins/selective_implicit_prices.py | 11 ++++++++--- 5 files changed, 20 insertions(+), 10 deletions(-) (limited to 'beancount_extras_kris7t') diff --git a/beancount_extras_kris7t/importers/otpbank/otpbank_csv.py b/beancount_extras_kris7t/importers/otpbank/otpbank_csv.py index 9e12ec3..c0f66ba 100644 --- a/beancount_extras_kris7t/importers/otpbank/otpbank_csv.py +++ b/beancount_extras_kris7t/importers/otpbank/otpbank_csv.py @@ -236,7 +236,8 @@ def extract_foreign_currencies( def extract_cards(card_accounts: Dict[str, str]) -> Extractor: def do_extract(row: Row) -> None: - if row.entry_type.lower() not in [ + entry_type = row.entry_type + if not entry_type or entry_type.lower() not in [ 'vásárlás kártyával', 'bankkártyával kapcs. díj', ]: diff --git a/beancount_extras_kris7t/importers/transferwise/__main__.py b/beancount_extras_kris7t/importers/transferwise/__main__.py index e25580d..4ac2058 100644 --- a/beancount_extras_kris7t/importers/transferwise/__main__.py +++ b/beancount_extras_kris7t/importers/transferwise/__main__.py @@ -4,7 +4,7 @@ Importer for Transferwise API transaction history. __copyright__ = 'Copyright (c) 2020 Kristóf Marussy ' __license__ = 'GNU GPLv2' -from importers.transferwise.client import main +from beancount_extras_kris7t.importers.transferwise.client import main if __name__ == '__main__': diff --git a/beancount_extras_kris7t/importers/transferwise/client.py b/beancount_extras_kris7t/importers/transferwise/client.py index a4b6629..5198122 100644 --- a/beancount_extras_kris7t/importers/transferwise/client.py +++ b/beancount_extras_kris7t/importers/transferwise/client.py @@ -25,8 +25,8 @@ def _parse_date_arg(date_str: str) -> dt.date: def _import_config(config_path: str) -> Importer: import runpy - config = runpy.run_path(config_path) - importer = config['TRANSFERWISE_CONFIG'] # type: ignore + config = runpy.run_path(config_path) # type: ignore + importer = config['TRANSFERWISE_CONFIG'] if isinstance(importer, Importer): LOG.info('Loaded configuration from %s', config_path) return importer @@ -65,7 +65,7 @@ def _get_last_transaction_date(ledger_path: str, skip_references: Set[str]) -> O def _get_date_range(from_date: Optional[dt.date], to_date: dt.date, ledger_path: Optional[str]) -> Tuple[dt.date, dt.date, Set[str]]: - skip_references = set() + skip_references: Set[str] = set() if not from_date and ledger_path: from_date = _get_last_transaction_date(ledger_path, skip_references) if not from_date: @@ -79,6 +79,7 @@ def _get_secrets(importer: Importer, proxy_uri: Optional[str]) -> Tuple[str, Optional[str]]: import urllib.parse + uri_parts: Optional[urllib.parse.SplitResult] if proxy_uri: uri_parts = urllib.parse.urlsplit(proxy_uri) else: @@ -122,7 +123,7 @@ def _get_secrets(importer: Importer, proxy_uri = uri else: LOG.info('No proxy password secret was found in SecretService') - assert api_key # Make pyright happy + assert api_key # Make mypy happy return api_key, proxy_uri @@ -141,8 +142,9 @@ def _fetch_statements(importer: Importer, uri_prefix = f'https://api.transferwise.com/v3/profiles/{importer.profile_id}/' + \ f'borderless-accounts/{importer.borderless_account_id}/statement.json' + \ f'?intervalStart={from_time_str}&intervalEnd={to_time_str}&type=COMPACT¤cy=' + beancount_version = beancount.__version__ # type: ignore # noqa: unused-type-ignore headers = { - 'User-Agent': f'Beancount {beancount.__version__} Transferwise importer {__copyright__}', + 'User-Agent': f'Beancount {beancount_version} Transferwise importer {__copyright__}', 'Authorization': f'Bearer {api_key}', } proxy_dict: Dict[str, str] = {} diff --git a/beancount_extras_kris7t/importers/transferwise/transferwise_json.py b/beancount_extras_kris7t/importers/transferwise/transferwise_json.py index c42de90..68ddc6d 100644 --- a/beancount_extras_kris7t/importers/transferwise/transferwise_json.py +++ b/beancount_extras_kris7t/importers/transferwise/transferwise_json.py @@ -211,6 +211,8 @@ class Row(utils.Row): # Also add the "fudge" amounts to the fees generated by rounding currency conversions. all_fees = Inventory() all_fees.add_inventory(self._fees) + if not self.postings: + raise InvalidEntry('Trying to create transaction without postings') for acc, assigned_units in self.postings: for conversion in self._conversions: units, price, fudge = conversion.get_fraction( diff --git a/beancount_extras_kris7t/plugins/selective_implicit_prices.py b/beancount_extras_kris7t/plugins/selective_implicit_prices.py index 07dc893..8f5186d 100644 --- a/beancount_extras_kris7t/plugins/selective_implicit_prices.py +++ b/beancount_extras_kris7t/plugins/selective_implicit_prices.py @@ -9,7 +9,9 @@ __copyright__ = "Copyright (C) 2015-2017 Martin Blais, " + \ __license__ = "GNU GPLv2" import collections -from typing import List, Tuple, Set +import datetime as dt +from decimal import Decimal +from typing import Dict, List, Optional, Set, Tuple from beancount.core.data import Commodity, Entries, Transaction from beancount.core import data @@ -22,6 +24,8 @@ __plugins__ = ('add_implicit_prices',) ImplicitPriceError = collections.namedtuple('ImplicitPriceError', 'source message entry') +EntryId = Tuple[dt.date, data.Currency, Optional[Decimal], data.Currency] + METADATA_FIELD = "__implicit_prices__" IMPLICIT_PRICES_META = "implicit-prices" @@ -65,9 +69,10 @@ def add_implicit_prices(entries: Entries, errors.extend(fetch_errors) # A dict of (date, currency, cost-currency) to price entry. - new_price_entry_map = {} + new_price_entry_map: Dict[EntryId, data.Price] = {} - balances = collections.defaultdict(inventory.Inventory) + balances: Dict[data.Account, inventory.Inventory] = collections.defaultdict( + inventory.Inventory) for entry in entries: # Always replicate the existing entries. new_entries.append(entry) -- cgit v1.2.3