ci: minor fixes after mypy update

This commit is contained in:
Dima Gerasimov 2024-08-03 15:27:11 +01:00 committed by karlicoss
parent c9c0e19543
commit 0e6dd32afe
8 changed files with 18 additions and 17 deletions

View file

@ -1,6 +1,7 @@
from glob import glob as do_glob from glob import glob as do_glob
from pathlib import Path from pathlib import Path
from datetime import datetime from datetime import datetime
from dataclasses import is_dataclass, asdict as dataclasses_asdict
import functools import functools
from contextlib import contextmanager from contextlib import contextmanager
import os import os
@ -292,15 +293,14 @@ Json = Dict[str, Any]
from typing import TypeVar, Callable, Generic from typing import TypeVar, Callable, Generic
_C = TypeVar('_C')
_R = TypeVar('_R') _R = TypeVar('_R')
# https://stackoverflow.com/a/5192374/706389 # https://stackoverflow.com/a/5192374/706389
class classproperty(Generic[_R]): class classproperty(Generic[_R]):
def __init__(self, f: Callable[[_C], _R]) -> None: def __init__(self, f: Callable[..., _R]) -> None:
self.f = f self.f = f
def __get__(self, obj: None, cls: _C) -> _R: def __get__(self, obj, cls) -> _R:
return self.f(cls) return self.f(cls)
@ -580,9 +580,9 @@ def asdict(thing: Any) -> Json:
# todo exception? # todo exception?
if isinstance(thing, dict): if isinstance(thing, dict):
return thing return thing
import dataclasses as D if is_dataclass(thing):
if D.is_dataclass(thing): assert not isinstance(thing, type) # to help mypy
return D.asdict(thing) return dataclasses_asdict(thing)
if is_namedtuple(thing): if is_namedtuple(thing):
return thing._asdict() return thing._asdict()
raise TypeError(f'Could not convert object {thing} to dict') raise TypeError(f'Could not convert object {thing} to dict')

View file

@ -195,7 +195,7 @@ def warn_my_config_import_error(err: Union[ImportError, AttributeError], help_ur
import click import click
if help_url is None: if help_url is None:
help_url = MODULE_SETUP_URL help_url = MODULE_SETUP_URL
if type(err) == ImportError: if type(err) is ImportError:
if err.name != 'my.config': if err.name != 'my.config':
return False return False
# parse name that user attempted to import # parse name that user attempted to import
@ -207,7 +207,7 @@ You may be missing the '{section_name}' section from your config.
See {help_url}\ See {help_url}\
""", fg='yellow', err=True) """, fg='yellow', err=True)
return True return True
elif type(err) == AttributeError: elif type(err) is AttributeError:
# test if user had a nested config block missing # test if user had a nested config block missing
# https://github.com/karlicoss/HPI/issues/223 # https://github.com/karlicoss/HPI/issues/223
if hasattr(err, 'obj') and hasattr(err, "name"): if hasattr(err, 'obj') and hasattr(err, "name"):

View file

@ -214,7 +214,7 @@ def _determine_order_by_value_key(obj_res: ET) -> Any:
Returns either the class, or a tuple of the dictionary keys Returns either the class, or a tuple of the dictionary keys
""" """
key = obj_res.__class__ key = obj_res.__class__
if key == dict: if key is dict:
# assuming same keys signify same way to determine ordering # assuming same keys signify same way to determine ordering
return tuple(obj_res.keys()) # type: ignore[union-attr] return tuple(obj_res.keys()) # type: ignore[union-attr]
return key return key
@ -583,7 +583,7 @@ def test_couldnt_determine_order() -> None:
res = list(select(iter([object()]), order_value=lambda o: isinstance(o, datetime))) res = list(select(iter([object()]), order_value=lambda o: isinstance(o, datetime)))
assert len(res) == 1 assert len(res) == 1
assert isinstance(res[0], Unsortable) assert isinstance(res[0], Unsortable)
assert type(res[0].obj) == object assert type(res[0].obj) is object
# same value type, different keys, with clashing keys # same value type, different keys, with clashing keys

View file

@ -471,7 +471,7 @@ def test_range_predicate() -> None:
) )
# filter from 0 to 5 # filter from 0 to 5
rn: Optional[RangeTuple] = RangeTuple("0", "5", None) rn: RangeTuple = RangeTuple("0", "5", None)
zero_to_five_filter: Optional[Where] = int_filter_func(unparsed_range=rn) zero_to_five_filter: Optional[Where] = int_filter_func(unparsed_range=rn)
assert zero_to_five_filter is not None assert zero_to_five_filter is not None
# this is just a Where function, given some input it return True/False if the value is allowed # this is just a Where function, given some input it return True/False if the value is allowed

View file

@ -1,5 +1,5 @@
import datetime import datetime
import dataclasses from dataclasses import is_dataclass, asdict
from pathlib import Path from pathlib import Path
from decimal import Decimal from decimal import Decimal
from typing import Any, Optional, Callable, NamedTuple from typing import Any, Optional, Callable, NamedTuple
@ -33,8 +33,9 @@ def _default_encode(obj: Any) -> Any:
# convert paths to their string representation # convert paths to their string representation
if isinstance(obj, Path): if isinstance(obj, Path):
return str(obj) return str(obj)
if dataclasses.is_dataclass(obj): if is_dataclass(obj):
return dataclasses.asdict(obj) assert not isinstance(obj, type) # to help mypy
return asdict(obj)
if isinstance(obj, Exception): if isinstance(obj, Exception):
return error_to_json(obj) return error_to_json(obj)
# if something was stored as 'decimal', you likely # if something was stored as 'decimal', you likely

View file

@ -92,7 +92,7 @@ class MessageError(RuntimeError):
super().__init__(msg_id, *rest) super().__init__(msg_id, *rest)
self.rest = rest self.rest = rest
def __hash__(self, other): def __hash__(self):
return hash(self.rest) return hash(self.rest)
def __eq__(self, other) -> bool: def __eq__(self, other) -> bool:

View file

@ -1,4 +1,4 @@
ignore = [ lint.ignore = [
### too opinionated style checks ### too opinionated style checks
"E501", # too long lines "E501", # too long lines
"E702", # Multiple statements on one line (semicolon) "E702", # Multiple statements on one line (semicolon)

View file

@ -26,7 +26,7 @@ passenv =
[testenv:ruff] [testenv:ruff]
commands = commands =
{envpython} -m pip install --use-pep517 -e .[testing] {envpython} -m pip install --use-pep517 -e .[testing]
{envpython} -m ruff my/ {envpython} -m ruff check my/
# just the very core tests with minimal dependencies # just the very core tests with minimal dependencies