如何让sphinx识别经过修饰的python函数
Sphinx不记录用装饰器包装的函数。我尝试过使用类风格的装饰器和函数风格的装饰器,但都没有用。这些函数不会出现在我生成的html中,而同一模块中的其他函数会出现 唯一一个半有效的方法是用decorator decorator包装我的类decorator,但是这样就不会在类中使用如何让sphinx识别经过修饰的python函数,python,python-sphinx,python-decorators,Python,Python Sphinx,Python Decorators,Sphinx不记录用装饰器包装的函数。我尝试过使用类风格的装饰器和函数风格的装饰器,但都没有用。这些函数不会出现在我生成的html中,而同一模块中的其他函数会出现 唯一一个半有效的方法是用decorator decorator包装我的类decorator,但是这样就不会在类中使用\uuuu call\uuuu函数,我需要从decorator返回一个值 import decorator import functools @decorator.decorator def MyDecoratorA(
\uuuu call\uuuu
函数,我需要从decorator返回一个值
import decorator
import functools
@decorator.decorator
def MyDecoratorA(fn, *args, **kwargs):
# do things
return fn(*args, **kwargs)
def MyDecoratorB(fn):
@functools.wraps(fn)
def inner(*args, **kwargs):
# do things
return fn(*args, **kwargs)
return inner
@MyDecoratorA
def TestA(a, b=None):
"""This is a doc
:param a: variable b
:type a: int
:param b: variable b
:type b: list
:returns: None
"""
pass
@MyDecoratorB
def TestB(a, b=None):
"""This is a doc
:param a: variable b
:type a: int
:param b: variable b
:type b: list
:returns: None
"""
pass
然后我有一个运行的批处理文件
sphinx-apidoc -f -l -M -T -o /tmp/source/testfunctions ${DIR}/modules/testfunctions/ 1>/dev/null
make html
这将生成一个名为testfunctions.rst的文件,其中包含testfunctions文件夹中每个模块的一个部分
testfunctions.cluster module
----------------------------
.. automodule:: testfunctions.cluster
:members:
:undoc-members:
:show-inheritance:
这是:
注
如果对修饰过的函数或方法编制文档,请记住autodoc通过导入模块并检查给定函数或方法的doc属性来检索其文档字符串。这意味着,如果装饰器用另一个函数替换装饰后的函数,它必须将原始的doc复制到新函数。
在Python2.5中,可以使用functools.wrapps()创建行为良好的装饰函数
我已经试过了,问题是函数的签名发生了变化,参数列表隐藏在*args和**kwargs后面,我看不到我在函数中定义的参数至少你的修饰函数出现了,是吗?-)不,它们没有出现,这可能会更容易提供帮助。所有函数都是documentedIts,只是示例代码您提到了一个带有
\uuu调用\uu
方法的类。这不在您的示例代码中。我尝试将decorator作为一个类来实现,看看这是否有帮助,但没有。你有什么建议吗?