Skip to content

Fix issue with cache hits #1977

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 0 additions & 3 deletions src/ragas/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,6 @@ def _make_hashable(o):


def _generate_cache_key(func, args, kwargs):
if inspect.ismethod(func):
args = args[1:]

filtered_kwargs = {k: v for k, v in kwargs.items() if k not in EXCLUDE_KEYS}

key_data = {
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,24 @@ def sample_func(a, b):
assert key1 != key3, "Cache keys should differ if kwargs differ"


def test_generate_cache_key_bound_method():
"""Test that cache keys stay the same, when caching bound methods of different objects."""

class Clazz():
def __init__(self, irrelevant):
self.irrelevant = irrelevant

def sample_func(self, a, b):
return a + b

object = Clazz(irrelevant=1)
object2 = Clazz(irrelevant=2)

key1 = _generate_cache_key(object.sample_func, (1, 2), {})
key2 = _generate_cache_key(object2.sample_func, (1, 2), {})
assert key1 == key2, "Cache keys should match even if the originating objects the methods are bound to are not the same, as long as the arguments match"


def test_no_cache_backend():
"""Test that if no cache backend is provided, results are not cached."""
call_count = {"count": 0}
Expand Down