Skip to content

Commit 791a428

Browse files
authored
update default args+kwargs hashing (#183)
1 parent 7039391 commit 791a428

2 files changed

Lines changed: 24 additions & 6 deletions

File tree

cachier/core.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
# python 2 compatibility
1111

1212
import datetime
13-
import functools
1413
import hashlib
1514
import inspect
1615
import os
@@ -70,11 +69,12 @@ def _calc_entry(core, key, func, args, kwds):
7069

7170

7271
def _default_hash_func(args, kwds):
73-
key = functools._make_key(args, kwds, typed=True)
74-
hash = hashlib.sha256()
75-
for item in key:
76-
hash.update(pickle.dumps(item))
77-
return hash.hexdigest()
72+
# Sort the kwargs to ensure consistent ordering
73+
sorted_kwargs = sorted(kwds.items())
74+
# Serialize args and sorted_kwargs using pickle or similar
75+
serialized = pickle.dumps((args, sorted_kwargs))
76+
# Create a hash of the serialized data
77+
return hashlib.sha256(serialized).hexdigest()
7878

7979

8080
def _convert_args_kwargs(

tests/test_general.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,24 @@ def dummy_func(a: int, b: int = 2, c: int = 3):
299299
assert count == 1
300300

301301

302+
def test_list_inputs():
303+
count = 0
304+
305+
@cachier.cachier()
306+
def dummy_func(a: list, b: list = [2]):
307+
nonlocal count
308+
count += 1
309+
return a + b
310+
311+
dummy_func.clear_cache()
312+
assert count == 0
313+
dummy_func([1])
314+
dummy_func([1], [2])
315+
dummy_func([1], b=[2])
316+
dummy_func(a=[1], b=[2])
317+
assert count == 1
318+
319+
302320
def test_order_independent_kwargs_handling():
303321
count = 0
304322

0 commit comments

Comments
 (0)