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

code objects analysis replaced with inspect #487

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions aiocache/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,13 +207,11 @@ def _get_cache(cache=Cache.MEMORY, serializer=None, plugins=None, **cache_kwargs


def _get_args_dict(func, args, kwargs):
defaults = {
arg_name: arg.default
for arg_name, arg in inspect.signature(func).parameters.items()
if arg.default is not inspect._empty # TODO: bug prone..
}
args_names = func.__code__.co_varnames[: func.__code__.co_argcount]
return {**defaults, **dict(zip(args_names, args)), **kwargs}
signature = inspect.signature(func)
incoming_args = signature.bind(*args, **kwargs)
incoming_args.apply_defaults()
incoming_args = dict(incoming_args.arguments)
return incoming_args


class multi_cached:
Expand Down Expand Up @@ -341,7 +339,7 @@ def get_cache_keys(self, f, args, kwargs):
keys = args_dict.get(self.keys_from_attr, []) or []
keys = [self.key_builder(key, f, *args, **kwargs) for key in keys]

args_names = f.__code__.co_varnames[: f.__code__.co_argcount]
args_names = list(inspect.signature(f).parameters.keys())
new_args = list(args)
keys_index = -1
if self.keys_from_attr in args_names and self.keys_from_attr not in kwargs:
Expand Down
19 changes: 19 additions & 0 deletions tests/acceptance/test_decorators.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import asyncio
import functools

import pytest
import random

Expand Down Expand Up @@ -157,3 +159,20 @@ async def fn(keys):

await fn([pytest.KEY])
assert await cache.exists(pytest.KEY) is True

@pytest.mark.asyncio
async def test_decorator_over_decorator(self, cache):
def dummy_d(fn):
@functools.wraps(fn)
async def wrapper(*args, **kwargs):
return await fn(*args, **kwargs)

return wrapper

@multi_cached("keys")
@dummy_d
async def fn(keys):
return {pytest.KEY: 1}

await fn([pytest.KEY])
assert await cache.exists(pytest.KEY) is True