Skip to content

Commit a719bfc

Browse files
refactor: clarify the casher arguments (#198)
* clarify the casher arguments * fix none * _pop_kwds_with_deprecation * tests * fixing --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 90488c9 commit a719bfc

8 files changed

Lines changed: 112 additions & 56 deletions

File tree

README.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ Cachier also accepts several keyword arguments in the calls of the function it w
236236
Ignore Cache
237237
~~~~~~~~~~~~
238238

239-
You can have ``cachier`` ignore any existing cache for a specific function call by passing ``ignore_cache=True`` to the function call. The cache will neither be checked nor updated with the new return value.
239+
You can have ``cachier`` ignore any existing cache for a specific function call by passing ``cachier__skip_cache=True`` to the function call. The cache will neither be checked nor updated with the new return value.
240240

241241
.. code-block:: python
242242
@@ -245,17 +245,17 @@ You can have ``cachier`` ignore any existing cache for a specific function call
245245
return first_num + second_num
246246
247247
def main():
248-
print(sum(5, 3, ignore_cache=True))
248+
print(sum(5, 3, cachier__skip_cache=True))
249249
250250
Overwrite Cache
251251
~~~~~~~~~~~~~~~
252252

253-
You can have ``cachier`` overwrite an existing cache entry - if one exists - for a specific function call by passing ``overwrite_cache=True`` to the function call. The cache will not be checked but will be updated with the new return value.
253+
You can have ``cachier`` overwrite an existing cache entry - if one exists - for a specific function call by passing ``cachier__overwrite_cache=True`` to the function call. The cache will not be checked but will be updated with the new return value.
254254

255255
Verbose Cache Call
256256
~~~~~~~~~~~~~~~~~~
257257

258-
You can have ``cachier`` print out a detailed explanation of the logic of a specific call by passing ``verbose_cache=True`` to the function call. This can be useful if you are not sure why a certain function result is, or is not, returned.
258+
You can have ``cachier`` print out a detailed explanation of the logic of a specific call by passing ``cachier__verbose=True`` to the function call. This can be useful if you are not sure why a certain function result is, or is not, returned.
259259

260260
Cache `None` Values
261261
~~~~~~~~~~~~~~~~~~~

cachier/config.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,15 @@ class Params(TypedDict):
4545
}
4646

4747

48-
def _update_with_defaults(param, name: str):
48+
def _update_with_defaults(
49+
param, name: str, func_kwargs: Optional[dict] = None
50+
):
4951
import cachier
5052

53+
if func_kwargs:
54+
kw_name = f"cachier__{name}"
55+
if kw_name in func_kwargs:
56+
return func_kwargs.pop(kw_name)
5157
if param is None:
5258
return cachier.config._default_params[name]
5359
return param

cachier/core.py

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import datetime
1111
import inspect
1212
import os
13+
import warnings
1314
from collections import OrderedDict
1415
from concurrent.futures import ThreadPoolExecutor
1516
from functools import wraps
@@ -93,6 +94,17 @@ def _convert_args_kwargs(
9394
return OrderedDict(sorted(kwargs.items()))
9495

9596

97+
def _pop_kwds_with_deprecation(kwds, name: str, default_value: bool):
98+
if name in kwds:
99+
warnings.warn(
100+
f"`{name}` is deprecated and will be removed in a future release,"
101+
" use `cachier__` alternative instead.",
102+
DeprecationWarning,
103+
stacklevel=2,
104+
)
105+
return kwds.pop(name, default_value)
106+
107+
96108
def cachier(
97109
hash_func: Optional[HashFunc] = None,
98110
hash_params: Optional[HashFunc] = None,
@@ -205,18 +217,31 @@ def _cachier_decorator(func):
205217
@wraps(func)
206218
def func_wrapper(*args, **kwds):
207219
nonlocal allow_none
208-
_allow_none = _update_with_defaults(allow_none, "allow_none")
220+
_allow_none = _update_with_defaults(allow_none, "allow_none", kwds)
209221
# print('Inside general wrapper for {}.'.format(func.__name__))
210-
ignore_cache = kwds.pop("ignore_cache", False)
211-
overwrite_cache = kwds.pop("overwrite_cache", False)
212-
verbose_cache = kwds.pop("verbose_cache", False)
222+
ignore_cache = _pop_kwds_with_deprecation(
223+
kwds, "ignore_cache", False
224+
)
225+
overwrite_cache = _pop_kwds_with_deprecation(
226+
kwds, "overwrite_cache", False
227+
)
228+
verbose = _pop_kwds_with_deprecation(kwds, "verbose_cache", False)
229+
ignore_cache = kwds.pop("cachier__skip_cache", ignore_cache)
230+
overwrite_cache = kwds.pop(
231+
"cachier__overwrite_cache", overwrite_cache
232+
)
233+
verbose = kwds.pop("cachier__verbose", verbose)
234+
_stale_after = _update_with_defaults(
235+
stale_after, "stale_after", kwds
236+
)
237+
_next_time = _update_with_defaults(next_time, "next_time", kwds)
213238
# merge args expanded as kwargs and the original kwds
214239
kwargs = _convert_args_kwargs(
215240
func, _is_method=core.func_is_method, args=args, kwds=kwds
216241
)
217242

218243
_print = lambda x: None # noqa: E731
219-
if verbose_cache:
244+
if verbose:
220245
_print = print
221246
if ignore_cache or not _default_params["caching_enabled"]:
222247
return func(**kwargs)
@@ -229,25 +254,21 @@ def func_wrapper(*args, **kwds):
229254
_print("Entry found.")
230255
if _allow_none or entry.get("value", None) is not None:
231256
_print("Cached result found.")
232-
local_stale_after = _update_with_defaults(
233-
stale_after, "stale_after"
234-
)
235-
local_next_time = _update_with_defaults(next_time, "next_time")
236257
now = datetime.datetime.now()
237-
if now - entry["time"] <= local_stale_after:
258+
if now - entry["time"] <= _stale_after:
238259
_print("And it is fresh!")
239260
return entry["value"]
240261
_print("But it is stale... :(")
241262
if entry["being_calculated"]:
242-
if local_next_time:
263+
if _next_time:
243264
_print("Returning stale.")
244265
return entry["value"] # return stale val
245266
_print("Already calc. Waiting on change.")
246267
try:
247268
return core.wait_on_entry_calc(key)
248269
except RecalculationNeeded:
249270
return _calc_entry(core, key, func, args, kwds)
250-
if local_next_time:
271+
if _next_time:
251272
_print("Async calc and return stale")
252273
try:
253274
core.mark_entry_being_calculated(key)

tests/test_defaults.py

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,7 @@ def test_allow_none_default_param():
126126
separate_files=True,
127127
verbose_cache=True,
128128
)
129-
allow_count = 0
130-
disallow_count = 0
129+
allow_count = disallow_count = 0
131130

132131
@cachier.cachier(cache_dir=tempfile.mkdtemp())
133132
def allow_none():
@@ -141,18 +140,20 @@ def disallow_none():
141140
disallow_count += 1
142141
return None
143142

144-
allow_none.clear_cache()
145143
assert allow_count == 0
146144
allow_none()
147145
allow_none()
148146
assert allow_count == 1
149147

150-
disallow_none.clear_cache()
151148
assert disallow_count == 0
152149
disallow_none()
153150
disallow_none()
154151
assert disallow_count == 2
155152

153+
disallow_none(cachier__allow_none=True)
154+
disallow_none(cachier__allow_none=True)
155+
assert disallow_count == 2
156+
156157

157158
parametrize_keys = "backend,mongetter"
158159
parametrize_values = [
@@ -245,3 +246,48 @@ def _calls_wait_for_calc_timeout_slow(res_queue):
245246
res4 = _wait_for_calc_timeout_slow(1, 2)
246247
# One of the cached values is returned
247248
assert res1 == res4 or res2 == res4 or res3 == res4
249+
250+
251+
def test_default_kwargs_handling():
252+
count = 0
253+
254+
@cachier.cachier()
255+
def dummy_func(a, b=2):
256+
nonlocal count
257+
count += 1
258+
return a + b
259+
260+
dummy_func.clear_cache()
261+
assert count == 0
262+
assert dummy_func(1) == 3
263+
assert dummy_func(a=1) == 3
264+
assert dummy_func(a=1, b=2) == 3
265+
assert count == 1
266+
267+
268+
def test_deprecated_func_kwargs():
269+
count = 0
270+
271+
@cachier.cachier()
272+
def dummy_func(a, b=2):
273+
nonlocal count
274+
count += 1
275+
return a + b
276+
277+
dummy_func.clear_cache()
278+
assert count == 0
279+
with pytest.deprecated_call(
280+
match="`verbose_cache` is deprecated and will be removed"
281+
):
282+
assert dummy_func(1, verbose_cache=True) == 3
283+
assert count == 1
284+
with pytest.deprecated_call(
285+
match="`ignore_cache` is deprecated and will be removed"
286+
):
287+
assert dummy_func(1, ignore_cache=True) == 3
288+
assert count == 2
289+
with pytest.deprecated_call(
290+
match="`overwrite_cache` is deprecated and will be removed"
291+
):
292+
assert dummy_func(1, overwrite_cache=True) == 3
293+
assert count == 3

tests/test_general.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -327,23 +327,6 @@ def dummy_func(a, b):
327327
assert count == 1
328328

329329

330-
def test_default_kwargs_handling():
331-
count = 0
332-
333-
@cachier.cachier()
334-
def dummy_func(a, b=2):
335-
nonlocal count
336-
count += 1
337-
return a + b
338-
339-
dummy_func.clear_cache()
340-
assert count == 0
341-
assert dummy_func(1) == 3
342-
assert dummy_func(a=1) == 3
343-
assert dummy_func(a=1, b=2) == 3
344-
assert count == 1
345-
346-
347330
@pytest.mark.parametrize("backend", ["memory", "pickle"])
348331
def test_diff_functions_same_args(tmpdir, backend: str):
349332
count_p = count_m = 0

tests/test_memory_core.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def test_memory_core():
2626
_takes_2_seconds.clear_cache()
2727
_takes_2_seconds("a", "b")
2828
start = time()
29-
_takes_2_seconds("a", "b", verbose_cache=True)
29+
_takes_2_seconds("a", "b", cachier__verbose=True)
3030
end = time()
3131
assert end - start < 1
3232
_takes_2_seconds.clear_cache()
@@ -38,7 +38,7 @@ def test_memory_core_keywords():
3838
_takes_2_seconds.clear_cache()
3939
_takes_2_seconds("a", arg_2="b")
4040
start = time()
41-
_takes_2_seconds("a", arg_2="b", verbose_cache=True)
41+
_takes_2_seconds("a", arg_2="b", cachier__verbose=True)
4242
end = time()
4343
assert end - start < 1
4444
_takes_2_seconds.clear_cache()
@@ -111,7 +111,7 @@ def test_overwrite_cache():
111111
int1 = _random_num()
112112
int2 = _random_num()
113113
assert int2 == int1
114-
int3 = _random_num(overwrite_cache=True)
114+
int3 = _random_num(cachier__overwrite_cache=True)
115115
assert int3 != int1
116116
int4 = _random_num()
117117
assert int4 == int3
@@ -121,7 +121,7 @@ def test_overwrite_cache():
121121
int1 = _random_num_with_arg("a")
122122
int2 = _random_num_with_arg("a")
123123
assert int2 == int1
124-
int3 = _random_num_with_arg("a", overwrite_cache=True)
124+
int3 = _random_num_with_arg("a", cachier__overwrite_cache=True)
125125
assert int3 != int1
126126
int4 = _random_num_with_arg("a")
127127
assert int4 == int3
@@ -135,7 +135,7 @@ def test_ignore_cache():
135135
int1 = _random_num()
136136
int2 = _random_num()
137137
assert int2 == int1
138-
int3 = _random_num(ignore_cache=True)
138+
int3 = _random_num(cachier__skip_cache=True)
139139
assert int3 != int1
140140
int4 = _random_num()
141141
assert int4 != int3
@@ -146,7 +146,7 @@ def test_ignore_cache():
146146
int1 = _random_num_with_arg("a")
147147
int2 = _random_num_with_arg("a")
148148
assert int2 == int1
149-
int3 = _random_num_with_arg("a", ignore_cache=True)
149+
int3 = _random_num_with_arg("a", cachier__skip_cache=True)
150150
assert int3 != int1
151151
int4 = _random_num_with_arg("a")
152152
assert int4 != int3

tests/test_mongo_core.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,11 @@ def _test_mongo_caching(arg_1, arg_2):
114114
val1 = _test_mongo_caching(1, 2)
115115
val2 = _test_mongo_caching(1, 2)
116116
assert val1 == val2
117-
val3 = _test_mongo_caching(1, 2, ignore_cache=True)
117+
val3 = _test_mongo_caching(1, 2, cachier__skip_cache=True)
118118
assert val3 != val1
119119
val4 = _test_mongo_caching(1, 2)
120120
assert val4 == val1
121-
val5 = _test_mongo_caching(1, 2, overwrite_cache=True)
121+
val5 = _test_mongo_caching(1, 2, cachier__overwrite_cache=True)
122122
assert val5 != val1
123123
val6 = _test_mongo_caching(1, 2)
124124
assert val6 == val5
@@ -137,11 +137,11 @@ def _test_mongo_caching(arg_1, arg_2):
137137
val1 = _test_mongo_caching(1, arg_2=2)
138138
val2 = _test_mongo_caching(1, arg_2=2)
139139
assert val1 == val2
140-
val3 = _test_mongo_caching(1, arg_2=2, ignore_cache=True)
140+
val3 = _test_mongo_caching(1, arg_2=2, cachier__skip_cache=True)
141141
assert val3 != val1
142142
val4 = _test_mongo_caching(1, arg_2=2)
143143
assert val4 == val1
144-
val5 = _test_mongo_caching(1, arg_2=2, overwrite_cache=True)
144+
val5 = _test_mongo_caching(1, arg_2=2, cachier__overwrite_cache=True)
145145
assert val5 != val1
146146
val6 = _test_mongo_caching(1, arg_2=2)
147147
assert val6 == val5

tests/test_pickle_core.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def test_pickle_core(reload, separate_files):
6262
_takes_2_seconds_decorated.clear_cache()
6363
_takes_2_seconds_decorated("a", "b")
6464
start = time()
65-
_takes_2_seconds_decorated("a", "b", verbose_cache=True)
65+
_takes_2_seconds_decorated("a", "b", cachier__verbose=True)
6666
end = time()
6767
assert end - start < 1
6868
_takes_2_seconds_decorated.clear_cache()
@@ -78,7 +78,7 @@ def test_pickle_core_keywords(separate_files):
7878
_takes_2_seconds_decorated.clear_cache()
7979
_takes_2_seconds_decorated("a", arg_2="b")
8080
start = time()
81-
_takes_2_seconds_decorated("a", arg_2="b", verbose_cache=True)
81+
_takes_2_seconds_decorated("a", arg_2="b", cachier__verbose=True)
8282
end = time()
8383
assert end - start < 1
8484
_takes_2_seconds_decorated.clear_cache()
@@ -168,7 +168,7 @@ def test_overwrite_cache(separate_files):
168168
int1 = _random_num_decorated()
169169
int2 = _random_num_decorated()
170170
assert int2 == int1
171-
int3 = _random_num_decorated(overwrite_cache=True)
171+
int3 = _random_num_decorated(cachier__overwrite_cache=True)
172172
assert int3 != int1
173173
int4 = _random_num_decorated()
174174
assert int4 == int3
@@ -178,7 +178,7 @@ def test_overwrite_cache(separate_files):
178178
int1 = _random_num_with_arg_decorated("a")
179179
int2 = _random_num_with_arg_decorated("a")
180180
assert int2 == int1
181-
int3 = _random_num_with_arg_decorated("a", overwrite_cache=True)
181+
int3 = _random_num_with_arg_decorated("a", cachier__overwrite_cache=True)
182182
assert int3 != int1
183183
int4 = _random_num_with_arg_decorated("a")
184184
assert int4 == int3
@@ -199,7 +199,7 @@ def test_ignore_cache(separate_files):
199199
int1 = _random_num_decorated()
200200
int2 = _random_num_decorated()
201201
assert int2 == int1
202-
int3 = _random_num_decorated(ignore_cache=True)
202+
int3 = _random_num_decorated(cachier__skip_cache=True)
203203
assert int3 != int1
204204
int4 = _random_num_decorated()
205205
assert int4 != int3
@@ -210,7 +210,7 @@ def test_ignore_cache(separate_files):
210210
int1 = _random_num_with_arg_decorated("a")
211211
int2 = _random_num_with_arg_decorated("a")
212212
assert int2 == int1
213-
int3 = _random_num_with_arg_decorated("a", ignore_cache=True)
213+
int3 = _random_num_with_arg_decorated("a", cachier__skip_cache=True)
214214
assert int3 != int1
215215
int4 = _random_num_with_arg_decorated("a")
216216
assert int4 != int3
@@ -342,7 +342,7 @@ def _bad_cache(arg_1, arg_2):
342342

343343
def _calls_bad_cache(bad_cache_func, res_queue, trash_cache, separate_files):
344344
try:
345-
res = bad_cache_func(0.13, 0.02, verbose_cache=True)
345+
res = bad_cache_func(0.13, 0.02, cachier__verbose=True)
346346
if trash_cache:
347347
with open(_BAD_CACHE_FPATHS[separate_files], "w") as cache_file:
348348
cache_file.seek(0)
@@ -566,7 +566,7 @@ def test_pickle_core_custom_cache_dir(separate_files):
566566
_takes_2_seconds_custom_dir_decorated.clear_cache()
567567
_takes_2_seconds_custom_dir_decorated("a", "b")
568568
start = time()
569-
_takes_2_seconds_custom_dir_decorated("a", "b", verbose_cache=True)
569+
_takes_2_seconds_custom_dir_decorated("a", "b", cachier__verbose=True)
570570
end = time()
571571
assert end - start < 1
572572
_takes_2_seconds_custom_dir_decorated.clear_cache()

0 commit comments

Comments
 (0)