如何将:特殊成员:仅应用于自动模块指令中的一个类? [英] How can I apply :special-members: to just one class inside an automodule directive?

查看:50
本文介绍了如何将:特殊成员:仅应用于自动模块指令中的一个类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用autodocnapoleon为一个Python2.7项目整理Sphinx驱动的(v1.3.1)文档。在我想用单个automodule::指令记录的一个模块中,我有一个要应用:special-members:标志的特定类。将automodule::标记为:special-members:会显示模块中所有的特价商品,这不太好。

如何执行此操作?

添加标记为:special-members:autoclass::指令时,仍会将非专门化文档保留为automodule::内容的一部分,从而导致内容重复。

我想我可以显式地键入模块中的所有类,automodule::上的:members:指令中的特殊类除外,但是我必须记住在每次向模块添加或删除类时更新该列表。

推荐答案

解决方案是排除您希望在automodule中应用不同选项的成员。然后将它们包含在自己的指令中,在该指令上设置您想要的特定选项。

以下示例将ClassBautomodule指令中排除。之后ClassB包含在automodule的上下文中,并带有自己的autoclass指令。在:special-members:选项下,仅设置要显示的成员。

对应.rst档案:

selective module
================

.. automodule:: selective
    :members:
    :exclude-members: ClassB

    .. autoclass:: ClassB
        :special-members: __init__, __special_func_two__

对应.py档案:

"""This modules docstring."""


class ClassA:
    """ClassA docstring."""

    def __special_func_one__(self, two):
        """Special method docstring."""
        self.two = two

    def __init__(self, one):
        """Special method docstring."""
        self.one = one


class ClassB:
    """ClassB docstring."""

    def __special_func_two__(self, two):
        """Special method docstring."""
        self.two = two

    def __special_func_three__(self, three):
        """Special method docstring."""
        self.three = three

    def __init__(self, one):
        """Special method docstring."""
        self.one = one
这最大限度地减少了您必须键入的异常数量,因为默认规则仍然正常地应用于模块的其余成员,除非您另有说明。在大多数IDE中,此解决方案还将重构对Python源代码所做的更改。

special-membersprivate-members没有包含在autodoc_default_options中。相关的sphinx.ext.napolon设置被设置为napoleon_include_special_with_doc = False。但是,个别指令设置仍将优先于以前的常规配置。

这篇关于如何将:特殊成员:仅应用于自动模块指令中的一个类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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