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

convert args to kwargs -> reduce keys for identical inputs #138

Closed
Borda opened this issue Jan 30, 2024 · 3 comments · Fixed by #140
Closed

convert args to kwargs -> reduce keys for identical inputs #138

Borda opened this issue Jan 30, 2024 · 3 comments · Fixed by #140

Comments

@Borda
Copy link
Contributor

Borda commented Jan 30, 2024

It seems that calling the same sample function with identical argument values can yield two different entry keys; see an example of a simple function:

def sample_keys(a, b, c=3):
    return a + b + c

The default hash function

cachier/cachier/core.py

Lines 83 to 89 in 28c8d0d

def _default_hash_func(args, kwds):
# pylint: disable-next=protected-access
key = functools._make_key(args, kwds, typed=True)
hash = hashlib.sha256()
for item in key:
hash.update(pickle.dumps(item))
return hash.hexdigest()

will give these keys:

  • for sample_keys(1, 2, c=3) gives:
     args, kwds = (1, 2), {"c": 3}
     functools._make_key(args, kwds, typed=True)
     Out[1]: [1, 2, <object at 0x7fc500943380>, 'c', 3, int, int, int]
  • for sample_keys(1, b=2, c=3) gives:
     args, kwds = (1,), {"b": 2, "c": 3}
     functools._make_key(args, kwds, typed=True)
     Out[2]: [1, <object at 0x7fc500943380>, 'b', 2, 'c', 3, int, int, int]
@Borda
Copy link
Contributor Author

Borda commented Jan 30, 2024

So my suggestion is to immediately convert all args to kwargs, for example: https://stackoverflow.com/a/831164/4521646

@Borda Borda changed the title convert args to kwargs to robustness convert args to kwargs -> reduce keys for identical inputs Jan 31, 2024
@shaypal5
Copy link
Collaborator

Looks good! If you're willing to implement it, I'd suggest giving it a go; If all tests pass locally, I'll pull it into a dev branch to run all Mongo core tests against an actual MongoDB server (I just can't get PRs to run tests with secrets), and then - if it doesn't break anything - it can be merged and released. 🥇

@Borda
Copy link
Contributor Author

Borda commented Feb 2, 2024

fixed in #140 and improved in #134

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants