Skip to content

Commit

Permalink
Decorators support introspection (#213)
Browse files Browse the repository at this point in the history
  • Loading branch information
argaen authored May 6, 2017
1 parent 0d19917 commit be015bf
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
3 changes: 3 additions & 0 deletions aiocache/decorators.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import inspect
import functools

from aiocache.log import logger
from aiocache import SimpleMemoryCache, caches
Expand Down Expand Up @@ -39,6 +40,7 @@ def cached(
cache_kwargs = kwargs

def cached_decorator(func):
@functools.wraps(func)
async def wrapper(*args, **kwargs):
if alias:
cache_instance = caches.create(alias)
Expand Down Expand Up @@ -102,6 +104,7 @@ def multi_cached(
cache_kwargs = kwargs

def multi_cached_decorator(func):
@functools.wraps(func)
async def wrapper(*args, **kwargs):
if alias:
cache_instance = caches.create(alias)
Expand Down
21 changes: 21 additions & 0 deletions tests/ut/test_decorators.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import sys
import pytest
import random
import inspect
import asynctest

from unittest import mock
Expand Down Expand Up @@ -197,6 +198,16 @@ async def test_cached_alias_takes_precedence(self, mocker, memory_mock_cache, mo
assert memory_mock_cache.get.call_count == 0
assert memory_mock_cache.set.call_count == 0

@pytest.mark.asyncio
async def test_cached_keeps_signature(self):
@cached()
async def what(self, a, b):
return "1"

assert what.__name__ == "what"
assert str(inspect.signature(what)) == '(self, a, b)'
assert inspect.getfullargspec(what.__wrapped__).args == ['self', 'a', 'b']


class TestMultiCachedDecorator:

Expand Down Expand Up @@ -337,6 +348,16 @@ async def test_multi_cached_alias_takes_precedence(self, mocker, memory_mock_cac
assert memory_mock_cache.multi_get.call_count == 0
assert memory_mock_cache.multi_set.call_count == 0

@pytest.mark.asyncio
async def test_multi_cached_keeps_signature(self):
@multi_cached('keys')
async def what(self, keys, a, b):
return "1"

assert what.__name__ == "what"
assert str(inspect.signature(what)) == '(self, keys, a, b)'
assert inspect.getfullargspec(what.__wrapped__).args == ['self', 'keys', 'a', 'b']


def test_get_args_dict():

Expand Down

0 comments on commit be015bf

Please sign in to comment.