hapic
hapic copied to clipboard
Manage non-hapic and non-functools wraped decorators on views
Related to: https://github.com/tracim/tracim/issues/594
Current problem
When views are decorated with decorators who are not using functools.wraps
, doc string is lost during the process. Example:
def my_decorator(func):
def wrapper(*args, **kwargs):
return func(*args, **kwargs)
return wrapper
@hapic.with_api_doc()
@my_decorator
def my_view(hapic_data=None):
"""
Hello doc
"""
pass
my_decorator
function return a wrapper
function who are not using functools.wraps
or similar method. So some information from decorated function are not copied to wrapper
function. Things like doc string are lost.
Solution
To solve this problem, we must keep link to the original function. We can do that by adding an hapic decorator (only required when "non-hapic and non-functools wraped" decorator used) who must be placed directly near view function. Example:
@hapic.with_api_doc()
@my_decorator
@hapic.track_view()
def my_view(hapic_data=None):
hapic.track_view
job will be to store reference of decorated function into hapic
instance, and use this reference in hapic.with_api_doc
instead wrapped function (in our case, the wrapper
function returned by my_decorator
). There is an extract of hapic.with_api_doc
code:
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
return func(*args, **kwargs)
Here, func
is the wrapper
function returned by my_decorator
and it is given to @functools.wraps(func)
. By using the correct function (stored by reference by hapic.track_view
), doc string will be correctly given to hapic.with_api_doc
wrapper.
To do
- @lebouquetin Find correct name for
hapic.track_view
. - Code proposed solution
- Write test