aio-libs / aio-libs/aiocache

Strange behaviour for @multi_cached decorator

Open
#1,037 5 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
1.4k
Forks
181
PR merge metrics
No merged PRs in 30d

Description

When i specify `key_builder` for `@multi_cached` decorator, it makes two strange things: it modifies input for target function, and also return modified keys:

```
import asyncio

from aiocache import multi_cached

def key_builder(key, f, *args, **kwargs):
return key, args[0]

class MyClass:
@multi_cached(
keys_from_attr='ids',
key_builder=key_builder,
)
async def example(self, ids):
print('example called', ids)
return {id_: id_ for id_ in ids}

async def main():
obj = MyClass()
# {
# (1, <__main__.MyClass object at 0x10d1bf490>): (1, <__main__.MyClass object at 0x10d1bf490>),
# (2, <__main__.MyClass object at 0x10d1bf490>): (2, <__main__.MyClass object at 0x10d1bf490>)
# }
results = await obj.example(ids=[1, 2])
print(results)

asyncio.run(main())
```

I expect that this decorator at least should not change input parameters for target function. If i call MyClass().example(ids=[1,2]) i should get ids=[1,2] in my target function.

I also expected that result keys of function MyClass.example(ids=[1,2]) would also be unchanged - it is very confusing that decorator changes function result value. It should just perform caching.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.