aio-libs / aio-libs/aiocache

Strange behaviour for @multi_cached decorator

未關閉
#1,037 5 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視
主要語言
Python
星號
1.4k
分支
181
PR 合併指標
30 天內沒有已合併 PR

描述

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.

貢獻指南

開啟貢獻指南

評估

這個 Issue 還沒有評估資料。

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。