From 4fe00478311a1631307671af0cb6d2db4bfca3e0 Mon Sep 17 00:00:00 2001 From: Jirka Date: Mon, 11 Mar 2024 12:31:48 +0100 Subject: [PATCH 1/3] ruff: A, B, C4 --- pyproject.toml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pyproject.toml b/pyproject.toml index 82ade930..6530ca3e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -46,6 +46,11 @@ lint.select = [ "S", # see: https://pypi.org/project/flake8-bandit "SIM", ] +lint.extend-select = [ + "A", # see: https://pypi.org/project/flake8-builtins + "B", # see: https://pypi.org/project/flake8-bugbear + "C4", # see: https://pypi.org/project/flake8-comprehensions +] lint.ignore = [ "E203", "C901", From f81bee4f769d9ba2250a246167b368ea0877c13d Mon Sep 17 00:00:00 2001 From: Jirka Date: Mon, 11 Mar 2024 12:40:43 +0100 Subject: [PATCH 2/3] --unsafe-fixes --- cachier/core.py | 4 ++-- tests/speed_eval.py | 4 ++-- tests/test_memory_core.py | 4 ++-- tests/test_mongo_core.py | 4 ++-- tests/test_pickle_core.py | 8 ++++---- 5 files changed, 12 insertions(+), 12 deletions(-) diff --git a/cachier/core.py b/cachier/core.py index b2bc0e94..7764ab93 100644 --- a/cachier/core.py +++ b/cachier/core.py @@ -245,7 +245,7 @@ def func_wrapper(*args, **kwds): _print = print if ignore_cache or not _default_params["caching_enabled"]: return func(**kwargs) - key, entry = core.get_entry(tuple(), kwargs) + key, entry = core.get_entry((), kwargs) if overwrite_cache: return _calc_entry(core, key, func, args, kwds) if entry is None: @@ -314,7 +314,7 @@ def _precache_value(*args, value_to_cache, **kwds): kwargs = _convert_args_kwargs( func, _is_method=core.func_is_method, args=args, kwds=kwds ) - return core.precache_value(tuple(), kwargs, value_to_cache) + return core.precache_value((), kwargs, value_to_cache) func_wrapper.clear_cache = _clear_cache func_wrapper.clear_being_calculated = _clear_being_calculated diff --git a/tests/speed_eval.py b/tests/speed_eval.py index 398d98a1..11fe7ee2 100644 --- a/tests/speed_eval.py +++ b/tests/speed_eval.py @@ -55,12 +55,12 @@ def test_separate_files_vs_single_file(): _test_separate_files_speed.clear_cache() _test_single_file_speed.clear_cache() start_time = time() - for i in range(3): + for _ in range(3): for j in range(10): _test_separate_files_speed(j, 2) print(f"separate files time: {time() - start_time}") start_time = time() - for i in range(3): + for _ in range(3): for j in range(10): _test_single_file_speed(j, 2) print(f"single file time: {time() - start_time}") diff --git a/tests/test_memory_core.py b/tests/test_memory_core.py index 608ac4bb..7e302046 100644 --- a/tests/test_memory_core.py +++ b/tests/test_memory_core.py @@ -314,8 +314,8 @@ def _params_with_dataframe(*args, **kwargs): _params_with_dataframe.clear_cache() - df_a = pd.DataFrame.from_dict(dict(a=[0], b=[2], c=[3])) - df_b = pd.DataFrame.from_dict(dict(a=[0], b=[2], c=[3])) + df_a = pd.DataFrame.from_dict({"a": [0], "b": [2], "c": [3]}) + df_b = pd.DataFrame.from_dict({"a": [0], "b": [2], "c": [3]}) value_a = _params_with_dataframe(df_a, 1) value_b = _params_with_dataframe(df_b, 1) diff --git a/tests/test_mongo_core.py b/tests/test_mongo_core.py index fcbd2171..b886b4ec 100644 --- a/tests/test_mongo_core.py +++ b/tests/test_mongo_core.py @@ -351,8 +351,8 @@ def _params_with_dataframe(*args, **kwargs): _params_with_dataframe.clear_cache() - df_a = pd.DataFrame.from_dict(dict(a=[0], b=[2], c=[3])) - df_b = pd.DataFrame.from_dict(dict(a=[0], b=[2], c=[3])) + df_a = pd.DataFrame.from_dict({"a": [0], "b": [2], "c": [3]}) + df_b = pd.DataFrame.from_dict({"a": [0], "b": [2], "c": [3]}) value_a = _params_with_dataframe(df_a, 1) value_b = _params_with_dataframe(df_b, 1) diff --git a/tests/test_pickle_core.py b/tests/test_pickle_core.py index c83105c1..f2f4fe1d 100644 --- a/tests/test_pickle_core.py +++ b/tests/test_pickle_core.py @@ -406,7 +406,7 @@ def test_bad_cache_file(separate_files): for sleeptime in sleeptimes: if _helper_bad_cache_file(sleeptime, separate_files): return - assert False + raise AssertionError() def _delete_cache(arg_1, arg_2): @@ -501,7 +501,7 @@ def test_delete_cache_file(separate_files): for sleeptime in sleeptimes: if _helper_delete_cache_file(sleeptime, separate_files): return - assert False + raise AssertionError() @pytest.mark.pickle @@ -598,8 +598,8 @@ def _params_with_dataframe(*args, **kwargs): _params_with_dataframe.clear_cache() - df_a = pd.DataFrame.from_dict(dict(a=[0], b=[2], c=[3])) - df_b = pd.DataFrame.from_dict(dict(a=[0], b=[2], c=[3])) + df_a = pd.DataFrame.from_dict({"a": [0], "b": [2], "c": [3]}) + df_b = pd.DataFrame.from_dict({"a": [0], "b": [2], "c": [3]}) value_a = _params_with_dataframe(df_a, 1) value_b = _params_with_dataframe(df_b, 1) From e2e1d90aee1e93e7af635b33c5db4c622d34bc49 Mon Sep 17 00:00:00 2001 From: Jirka Date: Mon, 11 Mar 2024 12:56:27 +0100 Subject: [PATCH 3/3] fixing --- .pre-commit-config.yaml | 7 ------- cachier/cores/mongo.py | 5 +++-- cachier/cores/pickle.py | 14 +++++++------- pyproject.toml | 5 +++-- tests/test_general.py | 2 +- 5 files changed, 14 insertions(+), 19 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 32c5d849..5e2d34f1 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -56,13 +56,6 @@ repos: # hooks: # - id: mypy - - repo: https://github.com/asottile/yesqa - rev: v1.5.0 - hooks: - - id: yesqa - additional_dependencies: - - flake8-bandit - - repo: https://github.com/astral-sh/ruff-pre-commit rev: v0.2.1 hooks: diff --git a/cachier/cores/mongo.py b/cachier/cores/mongo.py index 51da39ec..b199845d 100644 --- a/cachier/cores/mongo.py +++ b/cachier/cores/mongo.py @@ -41,8 +41,9 @@ def __init__( ): if "pymongo" not in sys.modules: warnings.warn( - "Cachier warning: pymongo was not found. " - "MongoDB cores will not function." + "`pymongo` was not found. MongoDB cores will not function.", + ImportWarning, + stacklevel=2, ) # pragma: no cover super().__init__(hash_func, wait_for_calc_timeout) diff --git a/cachier/cores/pickle.py b/cachier/cores/pickle.py index 9b4a9fd2..2dab17d3 100644 --- a/cachier/cores/pickle.py +++ b/cachier/cores/pickle.py @@ -115,9 +115,9 @@ def _get_cache(self): self._reload_cache() return self.cache - def _get_cache_by_key(self, key=None, hash=None): + def _get_cache_by_key(self, key=None, hash_str=None): fpath = self.cache_fpath - fpath += f"_{key}" if hash is None else f"_{hash}" + fpath += f"_{hash_str or key}" try: with portalocker.Lock(fpath, mode="rb") as cache_file: return pickle.load(cache_file) # noqa: S301 @@ -134,17 +134,17 @@ def _clear_being_calculated_all_cache_files(self): path, name = os.path.split(self.cache_fpath) for subpath in os.listdir(path): if subpath.startswith(name): - entry = self._get_cache_by_key(hash=subpath.split("_")[-1]) + entry = self._get_cache_by_key(hash_str=subpath.split("_")[-1]) if entry is not None: entry["being_calculated"] = False - self._save_cache(entry, hash=subpath.split("_")[-1]) + self._save_cache(entry, hash_str=subpath.split("_")[-1]) - def _save_cache(self, cache, key=None, hash=None): + def _save_cache(self, cache, key=None, hash_str=None): fpath = self.cache_fpath if key is not None: fpath += f"_{key}" - elif hash is not None: - fpath += f"_{hash}" + elif hash_str is not None: + fpath += f"_{hash_str}" with self.lock: self.cache = cache with portalocker.Lock(fpath, mode="wb") as cache_file: diff --git a/pyproject.toml b/pyproject.toml index 6530ca3e..22177758 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -41,10 +41,11 @@ lint.select = [ "W", # see: https://pypi.org/project/pycodestyle "F", # see: https://pypi.org/project/pyflakes "I", #see: https://pypi.org/project/isort/ -# "D", # see: https://pypi.org/project/pydocstyle -# "N", # see: https://pypi.org/project/pep8-naming + #"D", # see: https://pypi.org/project/pydocstyle + #"N", # see: https://pypi.org/project/pep8-naming "S", # see: https://pypi.org/project/flake8-bandit "SIM", + "RUF100" # alternative to yesqa ] lint.extend-select = [ "A", # see: https://pypi.org/project/flake8-builtins diff --git a/tests/test_general.py b/tests/test_general.py index c677355d..14e7351e 100644 --- a/tests/test_general.py +++ b/tests/test_general.py @@ -296,7 +296,7 @@ def test_list_inputs(): count = 0 @cachier.cachier() - def dummy_func(a: list, b: list = [2]): + def dummy_func(a: list, b: list = [2]): # noqa: B006 nonlocal count count += 1 return a + b