Skip to content

Commit 425287d

Browse files
Chore linting with 120 line-length (#324)
* Chore linting with 120 line-length * [pre-commit.ci] auto fixes from pre-commit.com hooks * some more.... --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent f57a518 commit 425287d

23 files changed

Lines changed: 176 additions & 631 deletions

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ repos:
4242
- id: mdformat
4343
additional_dependencies:
4444
- mdformat-gfm
45-
- mdformat-black
45+
- mdformat-ruff
4646
- mdformat_frontmatter
4747
args: ["--number"]
4848

@@ -61,7 +61,7 @@ repos:
6161
- id: ruff-format
6262
name: Black by Ruff
6363
# basic check
64-
- id: ruff
64+
- id: ruff-check
6565
name: Ruff check
6666
args: ["--fix"] #, "--unsafe-fixes"
6767

examples/redis_example.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,7 @@ def demo_callable_client():
113113

114114
def get_redis_client():
115115
"""Get a Redis client."""
116-
return redis.Redis(
117-
host="localhost", port=6379, db=0, decode_responses=False
118-
)
116+
return redis.Redis(host="localhost", port=6379, db=0, decode_responses=False)
119117

120118
@cachier(backend="redis", redis_client=get_redis_client)
121119
def cached_with_callable(n):

pyproject.toml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,11 @@ namespaces = false # to disable scanning PEP 420 namespaces (true by default)
7272

7373
# === Linting & Formatting ===
7474

75-
[tool.black]
76-
line-length = 79
77-
7875
# --- ruff ---
7976

8077
[tool.ruff]
8178
target-version = "py39"
82-
line-length = 79
79+
line-length = 120
8380
# Exclude a variety of commonly ignored directories.
8481
exclude = [
8582
".eggs",

src/cachier/config.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,7 @@ class CacheEntry:
8383
_completed: bool = False
8484

8585

86-
def _update_with_defaults(
87-
param, name: str, func_kwargs: Optional[dict] = None
88-
):
86+
def _update_with_defaults(param, name: str, func_kwargs: Optional[dict] = None):
8987
import cachier
9088

9189
if func_kwargs:
@@ -107,8 +105,7 @@ def set_default_params(**params: Any) -> None:
107105
import warnings
108106

109107
warnings.warn(
110-
"Called `set_default_params` is deprecated and will be removed."
111-
" Please use `set_global_params` instead.",
108+
"Called `set_default_params` is deprecated and will be removed. Please use `set_global_params` instead.",
112109
DeprecationWarning,
113110
stacklevel=2,
114111
)
@@ -138,11 +135,7 @@ def set_global_params(**params: Any) -> None:
138135
"""
139136
import cachier
140137

141-
valid_params = {
142-
k: v
143-
for k, v in params.items()
144-
if hasattr(cachier.config._global_params, k)
145-
}
138+
valid_params = {k: v for k, v in params.items() if hasattr(cachier.config._global_params, k)}
146139
cachier.config._global_params = replace(
147140
cachier.config._global_params,
148141
**valid_params,
@@ -159,8 +152,7 @@ def get_default_params() -> Params:
159152
import warnings
160153

161154
warnings.warn(
162-
"Called `get_default_params` is deprecated and will be removed."
163-
" Please use `get_global_params` instead.",
155+
"Called `get_default_params` is deprecated and will be removed. Please use `get_global_params` instead.",
164156
DeprecationWarning,
165157
stacklevel=2,
166158
)

src/cachier/core.py

Lines changed: 21 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,7 @@ def _function_thread(core, key, func, args, kwds):
5656
print(f"Function call failed with the following exception:\n{exc}")
5757

5858

59-
def _calc_entry(
60-
core, key, func, args, kwds, printer=lambda *_: None
61-
) -> Optional[Any]:
59+
def _calc_entry(core, key, func, args, kwds, printer=lambda *_: None) -> Optional[Any]:
6260
core.mark_entry_being_calculated(key)
6361
try:
6462
func_res = func(*args, **kwds)
@@ -70,26 +68,18 @@ def _calc_entry(
7068
core.mark_entry_not_calculated(key)
7169

7270

73-
def _convert_args_kwargs(
74-
func, _is_method: bool, args: tuple, kwds: dict
75-
) -> dict:
71+
def _convert_args_kwargs(func, _is_method: bool, args: tuple, kwds: dict) -> dict:
7672
"""Convert mix of positional and keyword arguments to aggregated kwargs."""
7773
# unwrap if the function is functools.partial
7874
if hasattr(func, "func"):
7975
args = func.args + args
8076
kwds.update({k: v for k, v in func.keywords.items() if k not in kwds})
8177
func = func.func
8278
func_params = list(inspect.signature(func).parameters)
83-
args_as_kw = dict(
84-
zip(func_params[1:], args[1:])
85-
if _is_method
86-
else zip(func_params, args)
87-
)
79+
args_as_kw = dict(zip(func_params[1:], args[1:]) if _is_method else zip(func_params, args))
8880
# init with default values
8981
kwargs = {
90-
k: v.default
91-
for k, v in inspect.signature(func).parameters.items()
92-
if v.default is not inspect.Parameter.empty
82+
k: v.default for k, v in inspect.signature(func).parameters.items() if v.default is not inspect.Parameter.empty
9383
}
9484
# merge args expanded as kwargs and the original kwds
9585
kwargs.update(dict(**args_as_kw, **kwds))
@@ -99,8 +89,7 @@ def _convert_args_kwargs(
9989
def _pop_kwds_with_deprecation(kwds, name: str, default_value: bool):
10090
if name in kwds:
10191
warnings.warn(
102-
f"`{name}` is deprecated and will be removed in a future release,"
103-
" use `cachier__` alternative instead.",
92+
f"`{name}` is deprecated and will be removed in a future release, use `cachier__` alternative instead.",
10493
DeprecationWarning,
10594
stacklevel=2,
10695
)
@@ -201,18 +190,13 @@ def cachier(
201190
"""
202191
# Check for deprecated parameters
203192
if hash_params is not None:
204-
message = (
205-
"hash_params will be removed in a future release, "
206-
"please use hash_func instead"
207-
)
193+
message = "hash_params will be removed in a future release, please use hash_func instead"
208194
warn(message, DeprecationWarning, stacklevel=2)
209195
hash_func = hash_params
210196
# Update parameters with defaults if input is None
211197
backend = _update_with_defaults(backend, "backend")
212198
mongetter = _update_with_defaults(mongetter, "mongetter")
213-
size_limit_bytes = parse_bytes(
214-
_update_with_defaults(entry_size_limit, "entry_size_limit")
215-
)
199+
size_limit_bytes = parse_bytes(_update_with_defaults(entry_size_limit, "entry_size_limit"))
216200
# Override the backend parameter if a mongetter is provided.
217201
if callable(mongetter):
218202
backend = "mongo"
@@ -235,9 +219,7 @@ def cachier(
235219
)
236220
elif backend == "memory":
237221
core = _MemoryCore(
238-
hash_func=hash_func,
239-
wait_for_calc_timeout=wait_for_calc_timeout,
240-
entry_size_limit=size_limit_bytes,
222+
hash_func=hash_func, wait_for_calc_timeout=wait_for_calc_timeout, entry_size_limit=size_limit_bytes
241223
)
242224
elif backend == "sql":
243225
core = _SQLCore(
@@ -290,59 +272,37 @@ def _call(*args, max_age: Optional[timedelta] = None, **kwds):
290272
nonlocal allow_none, last_cleanup
291273
_allow_none = _update_with_defaults(allow_none, "allow_none", kwds)
292274
# print('Inside general wrapper for {}.'.format(func.__name__))
293-
ignore_cache = _pop_kwds_with_deprecation(
294-
kwds, "ignore_cache", False
295-
)
296-
overwrite_cache = _pop_kwds_with_deprecation(
297-
kwds, "overwrite_cache", False
298-
)
275+
ignore_cache = _pop_kwds_with_deprecation(kwds, "ignore_cache", False)
276+
overwrite_cache = _pop_kwds_with_deprecation(kwds, "overwrite_cache", False)
299277
verbose = _pop_kwds_with_deprecation(kwds, "verbose_cache", False)
300278
ignore_cache = kwds.pop("cachier__skip_cache", ignore_cache)
301-
overwrite_cache = kwds.pop(
302-
"cachier__overwrite_cache", overwrite_cache
303-
)
279+
overwrite_cache = kwds.pop("cachier__overwrite_cache", overwrite_cache)
304280
verbose = kwds.pop("cachier__verbose", verbose)
305-
_stale_after = _update_with_defaults(
306-
stale_after, "stale_after", kwds
307-
)
281+
_stale_after = _update_with_defaults(stale_after, "stale_after", kwds)
308282
_next_time = _update_with_defaults(next_time, "next_time", kwds)
309-
_cleanup_flag = _update_with_defaults(
310-
cleanup_stale, "cleanup_stale", kwds
311-
)
312-
_cleanup_interval_val = _update_with_defaults(
313-
cleanup_interval, "cleanup_interval", kwds
314-
)
283+
_cleanup_flag = _update_with_defaults(cleanup_stale, "cleanup_stale", kwds)
284+
_cleanup_interval_val = _update_with_defaults(cleanup_interval, "cleanup_interval", kwds)
315285
# merge args expanded as kwargs and the original kwds
316-
kwargs = _convert_args_kwargs(
317-
func, _is_method=core.func_is_method, args=args, kwds=kwds
318-
)
286+
kwargs = _convert_args_kwargs(func, _is_method=core.func_is_method, args=args, kwds=kwds)
319287

320288
if _cleanup_flag:
321289
now = datetime.now()
322290
with cleanup_lock:
323291
if now - last_cleanup >= _cleanup_interval_val:
324292
last_cleanup = now
325-
_get_executor().submit(
326-
core.delete_stale_entries, _stale_after
327-
)
293+
_get_executor().submit(core.delete_stale_entries, _stale_after)
328294

329295
_print = print if verbose else lambda x: None
330296

331297
# Check current global caching state dynamically
332298
from .config import _global_params
333299

334300
if ignore_cache or not _global_params.caching_enabled:
335-
return (
336-
func(args[0], **kwargs)
337-
if core.func_is_method
338-
else func(**kwargs)
339-
)
301+
return func(args[0], **kwargs) if core.func_is_method else func(**kwargs)
340302
key, entry = core.get_entry((), kwargs)
341303
if overwrite_cache:
342304
return _calc_entry(core, key, func, args, kwds, _print)
343-
if entry is None or (
344-
not entry._completed and not entry._processing
345-
):
305+
if entry is None or (not entry._completed and not entry._processing):
346306
_print("No entry found. No current calc. Calling like a boss.")
347307
return _calc_entry(core, key, func, args, kwds, _print)
348308
_print("Entry found.")
@@ -353,10 +313,7 @@ def _call(*args, max_age: Optional[timedelta] = None, **kwds):
353313
nonneg_max_age = True
354314
if max_age is not None:
355315
if max_age < ZERO_TIMEDELTA:
356-
_print(
357-
"max_age is negative. "
358-
"Cached result considered stale."
359-
)
316+
_print("max_age is negative. Cached result considered stale.")
360317
nonneg_max_age = False
361318
else:
362319
assert max_age is not None # noqa: S101
@@ -379,9 +336,7 @@ def _call(*args, max_age: Optional[timedelta] = None, **kwds):
379336
_print("Async calc and return stale")
380337
core.mark_entry_being_calculated(key)
381338
try:
382-
_get_executor().submit(
383-
_function_thread, core, key, func, args, kwds
384-
)
339+
_get_executor().submit(_function_thread, core, key, func, args, kwds)
385340
finally:
386341
core.mark_entry_not_calculated(key)
387342
return entry.value
@@ -426,9 +381,7 @@ def _precache_value(*args, value_to_cache, **kwds): # noqa: D417
426381
427382
"""
428383
# merge args expanded as kwargs and the original kwds
429-
kwargs = _convert_args_kwargs(
430-
func, _is_method=core.func_is_method, args=args, kwds=kwds
431-
)
384+
kwargs = _convert_args_kwargs(func, _is_method=core.func_is_method, args=args, kwds=kwds)
432385
return core.precache_value((), kwargs, value_to_cache)
433386

434387
func_wrapper.clear_cache = _clear_cache

src/cachier/cores/base.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,7 @@ def precache_value(self, args, kwds, value_to_cache):
8585

8686
def check_calc_timeout(self, time_spent):
8787
"""Raise an exception if a recalculation is needed."""
88-
calc_timeout = _update_with_defaults(
89-
self.wait_for_calc_timeout, "wait_for_calc_timeout"
90-
)
88+
calc_timeout = _update_with_defaults(self.wait_for_calc_timeout, "wait_for_calc_timeout")
9189
if calc_timeout > 0 and (time_spent >= calc_timeout):
9290
raise RecalculationNeeded()
9391

src/cachier/cores/memory.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ def __init__(
2424
def _hash_func_key(self, key: str) -> str:
2525
return f"{_get_func_str(self.func)}:{key}"
2626

27-
def get_entry_by_key(
28-
self, key: str, reload=False
29-
) -> Tuple[str, Optional[CacheEntry]]:
27+
def get_entry_by_key(self, key: str, reload=False) -> Tuple[str, Optional[CacheEntry]]:
3028
with self.lock:
3129
return key, self.cache.get(self._hash_func_key(key), None)
3230

@@ -112,8 +110,6 @@ def delete_stale_entries(self, stale_after: timedelta) -> None:
112110
"""Remove stale entries from the in-memory cache."""
113111
now = datetime.now()
114112
with self.lock:
115-
keys_to_delete = [
116-
k for k, v in self.cache.items() if now - v.time > stale_after
117-
]
113+
keys_to_delete = [k for k, v in self.cache.items() if now - v.time > stale_after]
118114
for key in keys_to_delete:
119115
del self.cache[key]

src/cachier/cores/mongo.py

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,7 @@ def __init__(
5555
entry_size_limit=entry_size_limit,
5656
)
5757
if mongetter is None:
58-
raise MissingMongetter(
59-
"must specify ``mongetter`` when using the mongo core"
60-
)
58+
raise MissingMongetter("must specify ``mongetter`` when using the mongo core")
6159
self.mongetter = mongetter
6260
self.mongo_collection = self.mongetter()
6361
index_inf = self.mongo_collection.index_information()
@@ -73,9 +71,7 @@ def _func_str(self) -> str:
7371
return _get_func_str(self.func)
7472

7573
def get_entry_by_key(self, key: str) -> Tuple[str, Optional[CacheEntry]]:
76-
res = self.mongo_collection.find_one(
77-
{"func": self._func_str, "key": key}
78-
)
74+
res = self.mongo_collection.find_one({"func": self._func_str, "key": key})
7975
if not res:
8076
return key, None
8177
val = None
@@ -146,16 +142,11 @@ def clear_cache(self) -> None:
146142

147143
def clear_being_calculated(self) -> None:
148144
self.mongo_collection.update_many(
149-
filter={
150-
"func": self._func_str,
151-
"processing": True,
152-
},
145+
filter={"func": self._func_str, "processing": True},
153146
update={"$set": {"processing": False}},
154147
)
155148

156149
def delete_stale_entries(self, stale_after: timedelta) -> None:
157150
"""Delete stale entries from the MongoDB cache."""
158151
threshold = datetime.now() - stale_after
159-
self.mongo_collection.delete_many(
160-
filter={"func": self._func_str, "time": {"$lt": threshold}}
161-
)
152+
self.mongo_collection.delete_many(filter={"func": self._func_str, "time": {"$lt": threshold}})

0 commit comments

Comments
 (0)