问题
I am using sphinx to document a project and am having problems with functions wrapped in decorators. I have seen similar questions asked but no solution seems to fit my problem
I have hundreds of functions all wrapped in a custom decorator that itself can accept parameters
from functools import wraps
def CustomFunctionDecorator(id, name):
"""Custom decorator"""
def outer(f):
@wraps(f)
def inner(*args, **kwargs):
...do stuff....
f(*args, **kwargs)
return inner
return outer
My function will then look like this
@CustomFunctionDecorator(123, 'Test')
def TestFunction(a, b, c=None):
"""Test Documentation"""
..do something....
Now when i use sphinx and autodoc to generate my documentation, all of my functions wrapped in the CustomFunctionDecorator hide the actual arguments of the function in the sphinx documentation and appear like this
TestFunction(*args, **kwargs)
Test Documentation
The documentation works, but the function parameters don't....
Any ideas? hope i made myself clear
回答1:
Per this answer:
functools.wraps
only preserves__name__
,__doc__
, and__module__
. To preserve the signature as well take a look at Michele Simionato's Decorator module.
It's a workaround rather than a fix, but per the documentation (emphasis mine):
It’s possible to override the signature for explicitly documented callable objects (functions, methods, classes) with the regular syntax that will override the signature gained from introspection:
.. autoclass:: Noodle(type) .. automethod:: eat(persona)
This is useful if the signature from the method is hidden by a decorator.
New in version 0.4.
Obviously that won't be fun for "hundreds of functions"...
来源:https://stackoverflow.com/questions/25102943/getting-sphinx-to-display-function-parameters-when-wrapped-with-decorator