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

Decorators support introspection #213

Merged
merged 1 commit into from
May 6, 2017
Merged
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
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']
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@OOPMan have a look at the behavior here. Dunno how zope does the interface check, but it should use the signature (https://docs.python.org/3/library/inspect.html#inspect.signature) one since it enables the follow_wrapper by default.

getfullargspec (https://docs.python.org/3/library/inspect.html#inspect.getfullargspec) doesn't support following the wrapped attribute that functools.wraps adds.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Iirc Zope uses inspect as you have since it's the only sane way to do it. There are probably some insane ways but why worry about those :-)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, my point was more that if Zope uses inspect.signature then it will work. If it uses inspect.getfullargspec it won't because the real signature is under the __wrapped__ attribute of the function and getfullargspec doesn't use it (while signature does :))



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