Skip to content
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
22 changes: 2 additions & 20 deletions flask_classy.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import inspect
from werkzeug.routing import parse_rule
from flask import request, Response, make_response
from undecorated import undecorated
import re

_py2 = sys.version_info[0] == 2
Expand Down Expand Up @@ -294,26 +295,7 @@ def get_interesting_members(base_class, cls):
def get_true_argspec(method):
"""Drills through layers of decorators attempting to locate the actual argspec for the method."""

argspec = inspect.getargspec(method)
args = argspec[0]
if args and args[0] == 'self':
return argspec
if hasattr(method, '__func__'):
method = method.__func__
if not hasattr(method, '__closure__') or method.__closure__ is None:
raise DecoratorCompatibilityError

closure = method.__closure__
for cell in closure:
inner_method = cell.cell_contents
if inner_method is method:
continue
if not inspect.isfunction(inner_method) \
and not inspect.ismethod(inner_method):
continue
true_argspec = get_true_argspec(inner_method)
if true_argspec:
return true_argspec
return inspect.getargspec(undecorated(method))


class DecoratorCompatibilityError(Exception):
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
include_package_data=True,
platforms='any',
install_requires=[
'undecorated',
'Flask>=0.9'
],
classifiers=[
Expand All @@ -37,4 +38,4 @@
'Topic :: Software Development :: Libraries :: Python Modules'
],
test_suite='test_classy'
)
)
4 changes: 3 additions & 1 deletion test_classy/test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,6 @@ def test_params_decorator_delete():
eq_(b"Params Decorator Delete 1234", resp.data)



def test_explicit_params_decorator():
resp = client.get('/decorated/explicit/foo/bar')
eq_(b"Explicit param foobar", resp.data)
10 changes: 10 additions & 0 deletions test_classy/view_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,13 @@ def decorated_function(*args, **kwargs):
return decorator


def explicit_params_decorator(f):
@wraps(f)
def wrapper(self, arg1, *args, **kwargs):
return f(self, arg1, *args, **kwargs)
return wrapper


def recursive_decorator(f):
@wraps(f)
def decorated_view(*args, **kwargs):
Expand Down Expand Up @@ -253,6 +260,9 @@ def someval(self, val):
def anotherval(self, val):
return "Anotherval " + val

@explicit_params_decorator
def explicit(self, arg1, arg2):
return "Explicit param " + arg1 + arg2


class InheritanceView(BasicView):
Expand Down