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.
This commit is contained in:
Brett Smith 2021-02-26 16:13:02 -05:00
parent 6752a40206
commit ca12496880
10 changed files with 22 additions and 18 deletions

View file

@ -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

View file

@ -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

View file

@ -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 <https://github.com/python/typeshed/issues/4638>
return self._rt_wrapper(credentials, client) # type:ignore[arg-type]

View file

@ -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:

View file

@ -21,7 +21,6 @@ from typing import (
Callable,
Collection,
Dict,
Hashable,
Iterable,
Iterator,
List,

View file

@ -27,7 +27,6 @@ from typing import (
Callable,
Collection,
Dict,
Hashable,
Iterable,
Iterator,
List,

View file

@ -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:

View file

@ -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)

View file

@ -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,

View file

@ -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]