From ca12496880d64d9c40f1ff379a2ce435ce61c589 Mon Sep 17 00:00:00 2001 From: Brett Smith Date: Fri, 26 Feb 2021 16:13:02 -0500 Subject: [PATCH] typing: Updates to pass type checking under mypy>=0.800. Most of these account for the fact that mypy now reports that Hashable is not an allowed return type for sort key functions. That, plus the new ignore for the regression in config.py. --- conservancy_beancount/beancount_types.py | 3 +++ conservancy_beancount/cliutil.py | 10 +++++----- conservancy_beancount/config.py | 3 ++- conservancy_beancount/reconcile/paypal.py | 6 ++++-- conservancy_beancount/reports/balance_sheet.py | 1 - conservancy_beancount/reports/budget.py | 1 - conservancy_beancount/reports/core.py | 4 ++-- conservancy_beancount/reports/ledger.py | 4 ++-- conservancy_beancount/tools/opening_balances.py | 4 ++-- conservancy_beancount/tools/sort_entries.py | 4 ++-- 10 files changed, 22 insertions(+), 18 deletions(-) diff --git a/conservancy_beancount/beancount_types.py b/conservancy_beancount/beancount_types.py index a954c4f..5f54e4e 100644 --- a/conservancy_beancount/beancount_types.py +++ b/conservancy_beancount/beancount_types.py @@ -28,6 +28,9 @@ from typing import ( if TYPE_CHECKING: from . import errors as errormod + from _typeshed import SupportsLessThan as Sortable +else: + from typing import Hashable as Sortable Account = bc_data.Account Currency = bc_data.Currency diff --git a/conservancy_beancount/cliutil.py b/conservancy_beancount/cliutil.py index 73268d9..ee9ce7f 100644 --- a/conservancy_beancount/cliutil.py +++ b/conservancy_beancount/cliutil.py @@ -42,7 +42,6 @@ from typing import ( Callable, Container, Generic, - Hashable, IO, Iterable, Iterator, @@ -58,6 +57,7 @@ from typing import ( ) from .beancount_types import ( MetaKey, + Sortable, ) ET = TypeVar('ET', bound=enum.Enum) @@ -114,7 +114,7 @@ class EnumArgument(Generic[ET]): def choices_str(self, sep: str=', ', fmt: str='{!r}') -> str: """Return a user-formatted string of enum names""" - sortkey: Callable[[ET], Hashable] = getattr( + sortkey: Callable[[ET], Sortable] = getattr( self.base, '_choices_sortkey', self._choices_sortkey, ) return sep.join( @@ -122,7 +122,7 @@ class EnumArgument(Generic[ET]): for choice in sorted(self.base, key=sortkey) ) - def _choices_sortkey(self, choice: ET) -> Hashable: + def _choices_sortkey(self, choice: ET) -> Sortable: return choice.name @@ -207,7 +207,7 @@ class ExtendAction(argparse.Action): parser.add_argument( '--option', ..., action=ExtendAction, - const=regexp_pattern, # default is r'\s*,\s*' + const=regexp_pattern, # default is '\\s*,\\s*' ..., ) """ @@ -258,7 +258,7 @@ class LogLevel(enum.IntEnum): ERR = ERROR CRIT = CRITICAL - def _choices_sortkey(self) -> Hashable: + def _choices_sortkey(self) -> Sortable: return self.value diff --git a/conservancy_beancount/config.py b/conservancy_beancount/config.py index 3d93716..fd9f278 100644 --- a/conservancy_beancount/config.py +++ b/conservancy_beancount/config.py @@ -228,4 +228,5 @@ class Config: ) -> Optional[rtutil.RT]: if credentials is None: credentials = self.rt_credentials() - return self._rt_wrapper(credentials, client) + # type ignore for + return self._rt_wrapper(credentials, client) # type:ignore[arg-type] diff --git a/conservancy_beancount/reconcile/paypal.py b/conservancy_beancount/reconcile/paypal.py index 69d9c2f..621bcec 100644 --- a/conservancy_beancount/reconcile/paypal.py +++ b/conservancy_beancount/reconcile/paypal.py @@ -34,7 +34,6 @@ from ..reports import core from typing import ( Any, - Hashable, Iterable, Iterator, List, @@ -46,6 +45,9 @@ from typing import ( Tuple, Union, ) +from ..beancount_types import ( + Sortable, +) PROGNAME = 'reconcile-paypal' logger = logging.getLogger('conservancy_beancount.reconcile.paypal') @@ -164,7 +166,7 @@ class PayPalReconciler: worst_problem = worst_problem or problems return worst_problem - def sort_key(self) -> Hashable: + def sort_key(self) -> Sortable: try: post = self.statement_posts[0] except IndexError: diff --git a/conservancy_beancount/reports/balance_sheet.py b/conservancy_beancount/reports/balance_sheet.py index 3125b4e..a07292e 100644 --- a/conservancy_beancount/reports/balance_sheet.py +++ b/conservancy_beancount/reports/balance_sheet.py @@ -21,7 +21,6 @@ from typing import ( Callable, Collection, Dict, - Hashable, Iterable, Iterator, List, diff --git a/conservancy_beancount/reports/budget.py b/conservancy_beancount/reports/budget.py index e42f22f..ee31c1a 100644 --- a/conservancy_beancount/reports/budget.py +++ b/conservancy_beancount/reports/budget.py @@ -27,7 +27,6 @@ from typing import ( Callable, Collection, Dict, - Hashable, Iterable, Iterator, List, diff --git a/conservancy_beancount/reports/core.py b/conservancy_beancount/reports/core.py index 3c5fb37..43550f1 100644 --- a/conservancy_beancount/reports/core.py +++ b/conservancy_beancount/reports/core.py @@ -52,7 +52,6 @@ from typing import ( Collection, Dict, Generic, - Hashable, Iterable, Iterator, List, @@ -70,6 +69,7 @@ from typing import ( from ..beancount_types import ( MetaKey, MetaValue, + Sortable, ) OPENING_BALANCE_NAME = "OPENING BALANCE" @@ -439,7 +439,7 @@ class Balances: # Ensure the balance exists in the mapping class_bals[key.classification] norm_func = normalize_amount_func(f'{account}:RootsOK') - def sortkey(acct: data.Account) -> Hashable: + def sortkey(acct: data.Account) -> Sortable: prefix, _, _ = acct.rpartition(':') balance = norm_func(class_bals[acct]) try: diff --git a/conservancy_beancount/reports/ledger.py b/conservancy_beancount/reports/ledger.py index b90444d..c22e1e0 100644 --- a/conservancy_beancount/reports/ledger.py +++ b/conservancy_beancount/reports/ledger.py @@ -46,7 +46,6 @@ from typing import ( Any, Callable, Dict, - Hashable, Iterable, Iterator, List, @@ -61,6 +60,7 @@ from typing import ( cast, ) from ..beancount_types import ( + Sortable, Transaction, ) @@ -530,7 +530,7 @@ class ReportType(enum.IntFlag): else: return cls.DEBIT_TRANSACTIONS - def _choices_sortkey(self) -> Hashable: + def _choices_sortkey(self) -> Sortable: subtype, _, maintype = self.name.partition('_') return (maintype, subtype) diff --git a/conservancy_beancount/tools/opening_balances.py b/conservancy_beancount/tools/opening_balances.py index db0a08e..b7e8c96 100644 --- a/conservancy_beancount/tools/opening_balances.py +++ b/conservancy_beancount/tools/opening_balances.py @@ -27,7 +27,6 @@ import sys from typing import ( Dict, - Hashable, Iterable, Iterator, Mapping, @@ -41,6 +40,7 @@ from ..beancount_types import ( Error, MetaKey, MetaValue, + Sortable, Transaction, ) @@ -76,7 +76,7 @@ class AccountWithFund(NamedTuple): account: data.Account fund: Optional[MetaValue] - def sortkey(self) -> Hashable: + def sortkey(self) -> Sortable: account, fund = self return ( 0 if fund is None else 1, diff --git a/conservancy_beancount/tools/sort_entries.py b/conservancy_beancount/tools/sort_entries.py index e50d3fd..9d22334 100644 --- a/conservancy_beancount/tools/sort_entries.py +++ b/conservancy_beancount/tools/sort_entries.py @@ -22,7 +22,6 @@ from beancount import loader as bc_loader from beancount.parser import printer as bc_printer from typing import ( - Hashable, Optional, Sequence, TextIO, @@ -31,6 +30,7 @@ from ..beancount_types import ( Directive, Entries, Errors, + Sortable, ) from .. import cliutil @@ -56,7 +56,7 @@ def parse_arguments(arglist: Optional[Sequence[str]]=None) -> argparse.Namespace """) return parser.parse_args(arglist) -def entry_sorter(entry: Directive) -> Hashable: +def entry_sorter(entry: Directive) -> Sortable: type_name = type(entry).__name__ if type_name == 'Transaction': return (entry.date, type_name, entry.narration, entry.payee or '') # type:ignore[attr-defined]