Getting sphinx to display function parameters when wrapped with decorator

时光毁灭记忆、已成空白 提交于 2019-12-11 11:40:01

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!