From ca12496880d64d9c40f1ff379a2ce435ce61c589 2021-02-26 21:13:02 From: Brett Smith Date: 2021-02-26 21:13:02 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. --- diff --git a/conservancy_beancount/beancount_types.py b/conservancy_beancount/beancount_types.py index a954c4feb7afb5fdaa8a0087aec9110aebfaee36..5f54e4e82ba2b18d6d97b85c567a485a2c22d291 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 73268d9b096cd298a67ba5d03e02e8b8f3c192de..ee9ce7f443da2285d1f1dbb1d289a60f6bef3f55 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 3d93716b9183373eeba6d8e06f50254df3d89348..fd9f27869595f67d036aa698af342c7e9ab74d41 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 69d9c2fce42e0b63ded47b0d55757cc0a4573cd8..621bcec1b64668271fb54c1fb1cf63444e80fdba 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 3125b4e66c53a29037303522ccf6db38f56d7e3a..a07292efa1a16a31297d9fa5c750455e1164b53e 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 e42f22f55423846577914ba06271cfe9e96a9ff9..ee31c1afa2612aa570552cd194c1db3e3ea2235b 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 3c5fb371519aafd9703b4efed8b3c8b4afd1c926..43550f1232f3713cb2d8a1d37aa466badf5b86a8 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 b90444de886a26d132fc89d3dfbbda9148622ff3..c22e1e0b4c36ede09344af221ef7594cb07dbf58 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 db0a08e6891a16b6c604c246b32f24f7fe65faac..b7e8c96ec367f347594cbca839ecdd18754633f8 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 e50d3fd0f1cd5cd3463ae3058233e5803e37e296..9d22334bd002667c59d356667c9e5760172e9e00 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]