【发布时间】:2017-08-03 10:16:16
【问题描述】:
Sphinx 不记录用装饰器包装的函数。我曾尝试使用类风格装饰器和函数风格装饰器,但无济于事。这些函数没有出现在我生成的 html 中,而同一模块中的其他函数确实出现了
唯一成功的方法是用装饰器装饰器包装我的类装饰器,但这不会在类中使用__call__ 函数,我需要从装饰器返回一个值
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:
【问题讨论】:
-
使用minimal reproducible example,可能会更容易提供帮助。
-
所有功能都记录在案
-
只是示例代码
-
您提到了一个带有
__call__方法的类。它不在您的示例代码中。 -
我尝试将装饰器实现为一个类,看看是否有帮助,但没有。你有什么建议吗?
标签: python python-sphinx python-decorators