python-redis-cache icon indicating copy to clipboard operation
python-redis-cache copied to clipboard

fix(cache): support for unpassed default args

Open mhmdkanj opened this issue 1 year ago • 1 comments

This PR is based on top of #38 and addressed the following:

Defined functions with some parameters holding default values, in which function calls without passing a value to these parameters, leads to them being ignored when being cached. I.e. the normalized args does not include args not passed to parameters with default values.

Current Behavior

If function foo was defined as such:

@cache
def foo(a, b=2):
  return a+b

Calls to foo that look like this: foo(1) would be cached based on the argument given to parameter a. However, if this default value changes while the call to the function doesn't, then the same cache result is retrieved regardless of the value of b used inside the function.

@cache
def foo(a, b=999):
  return a+b
# assuming the cached value is retained
foo(1) == 3  # before change
foo(1) == 3  # after change

Expected Behavior

Since it would be safe to assume that defaults of parameters are used by the function's implementation, and thus potentially affect the return value, then it would be sensible to assume that a cached function call also depends on those defaults, which should be hashed consequently.

# assuming the cached value is retained
foo(1) == 3  # before change
foo(1) == 1000  # after change

mhmdkanj avatar May 12 '24 23:05 mhmdkanj

@mhmdkanj If you are able to rebase this code to a single commit, I will merge it

taylorhakes avatar Jun 17 '24 06:06 taylorhakes