Skip to content

Commit

Permalink
caching: fix cached_per_instance
Browse files Browse the repository at this point in the history
  • Loading branch information
Corey Sobel committed Jan 18, 2022
1 parent 719a446 commit daff55d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
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
15 changes: 15 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,16 @@ 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)

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

0 comments on commit daff55d

Please sign in to comment.