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

Named parameters are not handled correctly for key generation #1

Open
Ryan0751 opened this issue Dec 13, 2023 · 1 comment
Open

Named parameters are not handled correctly for key generation #1

Ryan0751 opened this issue Dec 13, 2023 · 1 comment

Comments

@Ryan0751
Copy link

If I memoize a class:

@memoize class TestManager:

And then use named parameters in the object construction:

test1 = TestManager("10.1.1.1", test_param="1.1.1.1")

The part of the library the computes the cache key fails:
/home/rruel/.pyenv/versions/3.10.8/envs/test/lib/python3.10/site-packages/nemoize/memoize.py:87: TypeError =========================================================================================================================================================== short test summary info =========================================================================================================================================================== FAILED test_connection.py::test_connmanager_memoization - TypeError: unsupported operand type(s) for +=: 'int' and 'object'

If I do not use named parameters, everything works fine.

Ideally I would like the keys to be computed such that if a user requests a new instance, it shouldn't matter the ordering of kwargs, or a mix of named vs. unamed parameters,

@Ryan0751
Copy link
Author

Ryan0751 commented Dec 13, 2023

This solution for key generation seems to be working well:

            # Bind args and kwargs to the memoized signature to get the actual arg names values that will be passed.
            bound_args = inspect.signature(self._f).bind(*args, **kwargs)

            # Build the cache key using the bound arguments.
            key = ""
            for k, v in bound_args.arguments.items():
                key += str((self._arg_hash_func(k)))
                key += str((self._arg_hash_func(v)))

            key = hash(key)

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

No branches or pull requests

1 participant