From 0e6dd32afe1a49de3dacecaede7ee149ab67e4b9 Mon Sep 17 00:00:00 2001 From: Dima Gerasimov Date: Sat, 3 Aug 2024 15:27:11 +0100 Subject: [PATCH] ci: minor fixes after mypy update --- my/core/common.py | 12 ++++++------ my/core/error.py | 4 ++-- my/core/query.py | 4 ++-- my/core/query_range.py | 2 +- my/core/serialize.py | 7 ++++--- my/instagram/android.py | 2 +- ruff.toml | 2 +- tox.ini | 2 +- 8 files changed, 18 insertions(+), 17 deletions(-) diff --git a/my/core/common.py b/my/core/common.py index c429c8c..f7bb010 100644 --- a/my/core/common.py +++ b/my/core/common.py @@ -1,6 +1,7 @@ from glob import glob as do_glob from pathlib import Path from datetime import datetime +from dataclasses import is_dataclass, asdict as dataclasses_asdict import functools from contextlib import contextmanager import os @@ -292,15 +293,14 @@ Json = Dict[str, Any] from typing import TypeVar, Callable, Generic -_C = TypeVar('_C') _R = TypeVar('_R') # https://stackoverflow.com/a/5192374/706389 class classproperty(Generic[_R]): - def __init__(self, f: Callable[[_C], _R]) -> None: + def __init__(self, f: Callable[..., _R]) -> None: self.f = f - def __get__(self, obj: None, cls: _C) -> _R: + def __get__(self, obj, cls) -> _R: return self.f(cls) @@ -580,9 +580,9 @@ def asdict(thing: Any) -> Json: # todo exception? if isinstance(thing, dict): return thing - import dataclasses as D - if D.is_dataclass(thing): - return D.asdict(thing) + if is_dataclass(thing): + assert not isinstance(thing, type) # to help mypy + return dataclasses_asdict(thing) if is_namedtuple(thing): return thing._asdict() raise TypeError(f'Could not convert object {thing} to dict') diff --git a/my/core/error.py b/my/core/error.py index e1737c1..fa59137 100644 --- a/my/core/error.py +++ b/my/core/error.py @@ -195,7 +195,7 @@ def warn_my_config_import_error(err: Union[ImportError, AttributeError], help_ur import click if help_url is None: help_url = MODULE_SETUP_URL - if type(err) == ImportError: + if type(err) is ImportError: if err.name != 'my.config': return False # 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}\ """, fg='yellow', err=True) return True - elif type(err) == AttributeError: + elif type(err) is AttributeError: # test if user had a nested config block missing # https://github.com/karlicoss/HPI/issues/223 if hasattr(err, 'obj') and hasattr(err, "name"): diff --git a/my/core/query.py b/my/core/query.py index 7c22838..071f7e0 100644 --- a/my/core/query.py +++ b/my/core/query.py @@ -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 """ key = obj_res.__class__ - if key == dict: + if key is dict: # assuming same keys signify same way to determine ordering return tuple(obj_res.keys()) # type: ignore[union-attr] return key @@ -583,7 +583,7 @@ def test_couldnt_determine_order() -> None: res = list(select(iter([object()]), order_value=lambda o: isinstance(o, datetime))) assert len(res) == 1 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 diff --git a/my/core/query_range.py b/my/core/query_range.py index dfb9e55..2b3a3d3 100644 --- a/my/core/query_range.py +++ b/my/core/query_range.py @@ -471,7 +471,7 @@ def test_range_predicate() -> None: ) # 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) 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 diff --git a/my/core/serialize.py b/my/core/serialize.py index c5f4cba..1f55f40 100644 --- a/my/core/serialize.py +++ b/my/core/serialize.py @@ -1,5 +1,5 @@ import datetime -import dataclasses +from dataclasses import is_dataclass, asdict from pathlib import Path from decimal import Decimal from typing import Any, Optional, Callable, NamedTuple @@ -33,8 +33,9 @@ def _default_encode(obj: Any) -> Any: # convert paths to their string representation if isinstance(obj, Path): return str(obj) - if dataclasses.is_dataclass(obj): - return dataclasses.asdict(obj) + if is_dataclass(obj): + assert not isinstance(obj, type) # to help mypy + return asdict(obj) if isinstance(obj, Exception): return error_to_json(obj) # if something was stored as 'decimal', you likely diff --git a/my/instagram/android.py b/my/instagram/android.py index ea5ee35..96b75d2 100644 --- a/my/instagram/android.py +++ b/my/instagram/android.py @@ -92,7 +92,7 @@ class MessageError(RuntimeError): super().__init__(msg_id, *rest) self.rest = rest - def __hash__(self, other): + def __hash__(self): return hash(self.rest) def __eq__(self, other) -> bool: diff --git a/ruff.toml b/ruff.toml index 0be93e0..54f621c 100644 --- a/ruff.toml +++ b/ruff.toml @@ -1,4 +1,4 @@ -ignore = [ +lint.ignore = [ ### too opinionated style checks "E501", # too long lines "E702", # Multiple statements on one line (semicolon) diff --git a/tox.ini b/tox.ini index 25874f4..0676eef 100644 --- a/tox.ini +++ b/tox.ini @@ -26,7 +26,7 @@ passenv = [testenv:ruff] commands = {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