简体   繁体   English

Sphinx Autodoc 从文档字符串中跳过成员

[英]Sphinx Autodoc skip member from docstring

I am documenting a class with Sphinx and simple want to skip one of the classes members:我正在用 Sphinx 记录一个 class 并且只是想跳过其中一个类成员:

class StatusUpdateAdapter(logging.LoggerAdapter):
    """
    """
    def __init__(self, status_update_func, logger, extra={}):
        """
        """
        pass

    def log(self, *args, **kwargs):
        pass

How can I cause sphinx NOT to document the log member?我怎样才能使 sphinx 不记录日志成员? I would like to do this in the StatusUpdateAdapter or log docstring's if possible.如果可能的话,我想在 StatusUpdateAdapter 或日志文档字符串中执行此操作。

You can now (as of version 0.6) use :exclude-members: to exclude specific members from the documentation:您现在可以(从 0.6 版开始)使用:exclude-members:从文档中排除特定成员:

The directives supporting member documentation also have a exclude-members option that can be used to exclude single member names from documentation, if all members are to be documented.支持成员文档的指令也有一个 exclude-members 选项,如果要记录所有成员,可用于从文档中排除单个成员名称。

New in version 0.6. 0.6 版中的新功能。

Source: http://www.sphinx-doc.org/en/stable/ext/autodoc.html来源: http : //www.sphinx-doc.org/en/stable/ext/autodoc.html

In your specific case, you would add :exclude-members: log into your .rst file.在您的特定情况下,您可以将:exclude-members: log到您的.rst文件中。

There doesn't appear to be any easy way to do this.似乎没有任何简单的方法可以做到这一点。

As a workaround, you can try something like this in your RST file:作为一种解决方法,您可以在 RST 文件中尝试类似的操作:

.. autoclass:: StatusUpdateAdapter
   :members: methodA, methodB

But that requires listing out all the methods you do want to document by hand, which could be quite laborious.但这需要列出您想要手动记录的所有方法,这可能非常费力。 It also probably doesn't interact well with :inherited-members: , if you're using that.如果您正在使用它,它也可能与:inherited-members:交互不佳。

Another option is to put a docstring on every method you want documented, but no docstring on the log() method, then (if necessary ) use :no-undoc-members: .另一种选择是在您想要记录的每个方法上放置一个文档字符串,但在log()方法上没有文档字符串,然后(如有必要)使用:no-undoc-members: This is obviously no good if you plan on documenting your internal interfaces or not documenting your public interfaces.如果您打算记录内部接口或不记录公共接口,这显然不好。

Finally, Autodoc skips anything whose name begins with an underscore unless configured otherwise ( :private-members: ), so if you use an underscore-prefixed name, the method will not appear.最后,除非另有配置( :private-members: ),否则 Autodoc 会跳过名称以下划线开头的任何内容,因此如果您使用下划线前缀的名称,该方法将不会出现。 Underscore-prefixing indicates a private interface under PEP 8, which may or may not match up with your intentions.下划线前缀表示PEP 8 下的私有接口,它可能符合也可能不符合您的意图。 This could also create backwards compatibility headaches in an established codebase.这也可能在已建立的代码库中造成向后兼容性问题。

I'm not sure how to do that with a docstring, but you can declare the function/method 'protected' with a prepended underscore.我不确定如何使用文档字符串来做到这一点,但您可以使用前置下划线声明函数/方法“受保护”。 Sphinx will not pull that function/method in. Sphinx 不会引入该函数/方法。

def _log(self, *args, **kwargs):
     pass

您可以使用:meta private:以便 Sphinx 认为该方法是私有的,如果您将 Sphinx 配置为隐藏私有方法,它将被隐藏。

Had a similar issue, and given that (even) as of today there seems to be no such feature, I found a workaround-ish option that worked for me: creating a custom autodoc-skip-member() function in the conf.py .有一个类似的问题,并且考虑到(甚至)到今天似乎没有这样的功能,我找到了一个对我有用的解决方法选项:在 conf.py 中创建自定义autodoc-skip-member() conf.py .

This allows to define when to skip members based on a few inputs including the type of the object, the name of the object and the object itself.这允许根据一些输入定义何时跳过成员,包括 object 的类型、object 的名称和 object 本身。

See this question for more details: Connect Sphinx autodoc-skip-member to my function , and in particular this answer (returning None allows to let Sphinx use the default behaviour for members not explicitly excluded).有关详细信息,请参阅此问题: 将 Sphinx autodoc-skip-member 连接到我的 function ,尤其是这个答案(返回None允许 Sphinx 使用未明确排除的成员的默认行为)。

Years too late, but an ugly workaround is to add an empty docstring to the public method that you want to skip.太晚了,但一个丑陋的解决方法是向要跳过的公共方法添加一个空的文档字符串。 Like that:像那样:

def log(self, *args, **kwargs):
    ""
    pass

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM