Skip to content
New issue

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

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

Already on GitHub? # to your account

caching: fix cached_per_instance #84

Merged
merged 2 commits into from
Jan 18, 2022
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
3 changes: 3 additions & 0 deletions qcore/caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,9 @@ def get_args_tuple(args, kwargs, arg_names, kwargs_defaults):
else:
args_list.append(kwargs[arg_name])
args_len += 1
remaining_keys = sorted([k for k in kwargs if k not in arg_names])
for k in remaining_keys:
args_list.append((k, kwargs[k]))
except KeyError as e:
raise TypeError("Missing argument %r" % (e.args[0],))
return tuple(args_list)
Expand Down
16 changes: 16 additions & 0 deletions qcore/tests/test_caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,11 @@ def with_kwargs(self, x=1, y=2, z=3):
self.x += x + y + z
return self.x

@cached_per_instance()
def with_variable_kwargs(self, **kwargs):
self.x += sum(kwargs.values())
return self.x


def test_cached_per_instance():
get_x_cache = TestClass.get_x.__cached_per_instance_cache__
Expand Down Expand Up @@ -413,6 +418,17 @@ def test_cached_per_instance():
assert_eq(0, len(get_x_cache), extra=repr(get_x_cache))
assert_eq(0, len(with_kwargs_cache), extra=repr(with_kwargs_cache))

object3 = TestClass(0)
assert_eq(0, object3.x)
object3.with_variable_kwargs(k1=2)
assert_eq(2, object3.x)
object3.with_variable_kwargs(k1=2)
assert_eq(2, object3.x)
object3.with_variable_kwargs(k2=2)
assert_eq(4, object3.x)
object3.with_variable_kwargs(k1=2, k2=2)
assert_eq(8, object3.x)
JelleZijlstra marked this conversation as resolved.
Show resolved Hide resolved


class PickleTestClass(object):
@cached_per_instance()
Expand Down