my.hypothesis: better mypy coverage

This commit is contained in:
Dima Gerasimov 2020-09-29 16:58:50 +01:00 committed by karlicoss
parent deefa9fbbc
commit 6199ed7916
3 changed files with 19 additions and 11 deletions

View file

@ -1,6 +1,8 @@
''' '''
Some backwards compatibility stuff/deprecation helpers Some backwards compatibility stuff/deprecation helpers
''' '''
from types import ModuleType
from . import warnings from . import warnings
from .common import LazyLogger from .common import LazyLogger
@ -13,7 +15,7 @@ def pre_pip_dal_handler(
e: ModuleNotFoundError, e: ModuleNotFoundError,
cfg, cfg,
requires=[], requires=[],
) -> None: ) -> ModuleType:
''' '''
https://github.com/karlicoss/HPI/issues/79 https://github.com/karlicoss/HPI/issues/79
''' '''

View file

@ -4,7 +4,7 @@ See https://beepb00p.xyz/mypy-error-handling.html#kiss for more detail
""" """
from itertools import tee from itertools import tee
from typing import Union, TypeVar, Iterable, List, Tuple, Type, Optional from typing import Union, TypeVar, Iterable, List, Tuple, Type, Optional, Callable, Any
T = TypeVar('T') T = TypeVar('T')
@ -44,7 +44,7 @@ def split_errors(l: Iterable[ResT[T, E]], ET: Type[E]) -> Tuple[Iterable[T], Ite
return (values, errors) return (values, errors)
def sort_res_by(items: Iterable[ResT], key) -> List[ResT]: def sort_res_by(items: Iterable[Res[T]], key: Callable[[T], Any]) -> List[Res[T]]:
""" """
The general idea is: just alaways carry errors with the entry that precedes them The general idea is: just alaways carry errors with the entry that precedes them
""" """
@ -58,7 +58,7 @@ def sort_res_by(items: Iterable[ResT], key) -> List[ResT]:
groups.append((i, group)) groups.append((i, group))
group = [] group = []
results = [] results: List[Res[T]] = []
for v, errs in sorted(groups, key=lambda p: key(p[0])): for v, errs in sorted(groups, key=lambda p: key(p[0])):
results.extend(errs) results.extend(errs)
results.append(v) results.append(v)
@ -125,7 +125,7 @@ def extract_error_datetime(e: Exception) -> Optional[datetime]:
return None return None
def test_datetime_errors(): def test_datetime_errors() -> None:
import pytz import pytz
dt_notz = datetime.now() dt_notz = datetime.now()
dt_tz = datetime.now(tz=pytz.timezone('Europe/Amsterdam')) dt_tz = datetime.now(tz=pytz.timezone('Europe/Amsterdam'))

View file

@ -2,7 +2,8 @@
[[https://hypothes.is][Hypothes.is]] highlights and annotations [[https://hypothes.is][Hypothes.is]] highlights and annotations
""" """
from dataclasses import dataclass from dataclasses import dataclass
from typing import Optional from datetime import datetime
from typing import Optional, Callable
from .core import Paths from .core import Paths
@ -50,12 +51,17 @@ def _dal() -> dal.DAL:
def highlights() -> List[Res[Highlight]]: def highlights() -> List[Res[Highlight]]:
return sort_res_by(_dal().highlights(), key=lambda h: h.created) # todo hmm. otherwise mypy complans
key: Callable[[Highlight], datetime] = lambda h: h.created
return sort_res_by(_dal().highlights(), key=key)
# TODO eh. always provide iterators? although sort_res_by could be neat too... # TODO eh. always provide iterators? although sort_res_by could be neat too...
def pages() -> List[Res[Page]]: def pages() -> List[Res[Page]]:
return sort_res_by(_dal().pages(), key=lambda h: h.created) # note: mypy report shows "No Anys on this line here", apparently a bug with type aliases
# https://github.com/python/mypy/issues/8594
key: Callable[[Page], datetime] = lambda h: h.created
return sort_res_by(_dal().pages(), key=key)
# todo not public api yet # todo not public api yet
@ -67,12 +73,12 @@ def stats():
} }
def _main(): def _main() -> None:
for page in get_pages(): for page in get_pages():
print(page) print(page)
if __name__ == '__main__': if __name__ == '__main__':
_main() _main()
get_highlights = highlights # TODO deprecate get_highlights = highlights # todo deprecate
get_pages = pages # TODO deprecate get_pages = pages # todo deprecate