Skip to content

Strange behaviour for @multi_cached decorator #1037

@alvassin

Description

@alvassin

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions