本文介绍了Python Sphinx autodoc和装饰成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Sphinx记录我的Python类。我这样做是使用autodoc:

I am attempting to use Sphinx to document my Python class. I do so using autodoc:

.. autoclass:: Bus
   :members:

虽然它可以正确地获取我的方法的文档字符串,但这些字符串是经过修饰的:

While it correctly fetches the docstrings for my methods, those that are decorated:

    @checkStale
    def open(self):
        """
        Some docs.
        """
        # Code

其中 @checkStale

def checkStale(f):
    @wraps(f)
    def newf(self, *args, **kwargs):
        if self._stale:
            raise Exception
        return f(self, *args, **kwargs)
    return newf

的原型不正确,例如 open(* args,** kwargs)

我该如何解决?我的印象是,使用 @wraps 可以解决这类问题。

How can I fix this? I was under the impression that using @wraps would fix up this kind of thing.

推荐答案

展开我的评论:

正如您在评论中所问的那样,decorator包不是

As you asked in your comment, the decorator package is not part of the standard library.

您可以使用以下代码(未经测试)回退:

You can fall back using code something like the following (untested):

try:
    from decorator import decorator
except ImportError:
    # No decorator package available. Create a no-op "decorator".
    def decorator(f):
        return f

这篇关于Python Sphinx autodoc和装饰成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 05:27