用装饰器包装后,使狮身人面像显示功能参数 [英] Getting sphinx to display function parameters when wrapped with decorator

查看:80
本文介绍了用装饰器包装后,使狮身人面像显示功能参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 sphinx 记录一个项目,并且装饰器中包装的函数有问题。我曾问过类似的问题,但似乎没有解决方案适合我的问题

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

我的函数将如下所示

@CustomFunctionDecorator(123, 'Test')
def TestFunction(a, b, c=None):
    """Test Documentation"""
    ..do something....

现在,当我使用sphinx和autodoc生成我的文档时,包装在CustomFunctionDecorator中的所有函数都会隐藏该函数的实际参数。狮身人面像并像这样

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)

测试文档

该文档有效,但函数参数不起作用't ....

The documentation works, but the function parameters don't....

有什么想法吗?希望我能弄清楚自己

Any ideas? hope i made myself clear

推荐答案

每个这个答案


functools.wraps 仅保留 __ name __ __ doc __ __ module __ 。要保留签名,还请看Michele Simionato的装饰器模块

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):


对于明确记录的$ b可以覆盖签名$ b具有常规语法
的可调用对象(函数,方法,类),它们将覆盖从自省中获得的签名:

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)  

如果方法的签名被
装饰器隐藏,则很有用。

0.4版中的新功能。

很显然,这对于数百个函数而言并不有趣 ..

Obviously that won't be fun for "hundreds of functions"...

这篇关于用装饰器包装后,使狮身人面像显示功能参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆