Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Order independent kwargs handling during cache key calculation #133

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions cachier/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import hashlib
import os
import pickle
from collections import OrderedDict
from concurrent.futures import ThreadPoolExecutor
from functools import wraps
from typing import TYPE_CHECKING, Callable, Literal, Optional, TypedDict, Union
Expand Down Expand Up @@ -257,10 +258,11 @@ def func_wrapper(*args, **kwds): # pylint: disable=C0111,R0911
_print = print
if ignore_cache or not _default_params['caching_enabled']:
return func(*args, **kwds)
kwds_ordered = OrderedDict(sorted(kwds.items()))
if core.func_is_method:
key, entry = core.get_entry(args[1:], kwds)
key, entry = core.get_entry(args[1:], kwds_ordered)
else:
key, entry = core.get_entry(args, kwds)
key, entry = core.get_entry(args, kwds_ordered)
if overwrite_cache:
return _calc_entry(core, key, func, args, kwds)
if entry is not None: # pylint: disable=R0101
Expand Down
18 changes: 18 additions & 0 deletions tests/test_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,3 +275,21 @@ def do_operation():
do_operation()
do_operation()
assert count == 1


def test_order_independent_kwargs_handling():
count = 0

@cachier.cachier()
def func(a=None, b=None):
nonlocal count
count += 1
return 0

func.clear_cache()
assert count == 0
func(a=1, b=2)
func(a=1, b=2)
assert count == 1
func(b=2, a=1)
assert count == 1
Loading