简体   繁体   English

如何使 Sphinx autodoc 扩展包含下划线/私有 Python 模块

[英]How to make Sphinx autodoc extension include an underscore/private Python module

I am autogenerating documentation for a Python package using Sphinx with the autodoc extension.我正在使用带有autodoc扩展名的 Sphinx 自动生成 Python 包的文档。 The issue I am facing is that the autodoc skips any modules with an underscore.我面临的问题是autodoc跳过任何带有下划线的模块。

Some modules were underscored to discourage users from importing them.一些模块被强调以阻止用户导入它们。 However, the classes inside these underscored files have no underscores.但是,这些带下划线的文件中的类没有下划线。

When I add the underscored module manually to the package_name.rst and ran make html , it shows.当我手动将下划线模块添加到package_name.rst并运行make html ,它会显示。 So my problem is how to automate that from autodoc .所以我的问题是如何从autodoc自动化。

I am trying to avoid parsing the package_name.rst via a script to add them.我试图避免通过脚本解析package_name.rst来添加它们。 I am hoping for an autodoc flag or a hack!我希望有一个 autodoc 标志或一个黑客!

It's perhaps a misconception to think the ..automodule :: directive applied to a package will automatically document sub-modules as members (by comparison with classes, variables, etc).认为应用于包的..automodule ::指令会自动将子模块记录为成员(通过与类、变量等进行比较)可能是一种误解。

I just tested this but it can not be done using :private-members: and or :special-members: .我刚刚对此进行了测试,但无法使用:private-members:和或:special-members: Not by writing either option in the .. automodule:: directive corresponding to the package.不是通过在与包对应的.. automodule::指令中写入任一选项。 (Trying to set both options in autodoc_default_options gives the same result.) (尝试在autodoc_default_options设置这两个选项autodoc_default_options产生相同的结果。)

The following example of package and module layout:以下包和模块布局示例:

C:.
│ 
└────your_package
   │
   │   public_module.py
   │   _private_module.py
   │   __init__.py

Using a .rst with a single .. automodule:: for the package:使用带有单个.. automodule::.rst包:

Your package rst
================

.. automodule:: your_package
    :members:
    :undoc-members:
    :private-members:
    :special-members:

Minimal example _private_module.py with docstrings ( public_module.py is the same except for the title):带有文档字符串的最小示例_private_module.pypublic_module.py除了标题外是相同的):

"""Private module docstring."""

class PublicClass:
    """Docstring."""
    pass

Does indeed give an empty documentation:确实给出了一个空文档:

在此处输入图片说明

But if you remove the underscore from the module you get the exact same result.但是如果你从模块中删除下划线,你会得到完全相同的结果。

I am trying to avoid parsing the package_name.rst via a script to add them我试图避免通过脚本解析 package_name.rst 来添加它们

If you are generating the .rst files with sphinx-apidoc if using the -P flag:如果您使用-P标志生成带有sphinx-apidoc.rst文件:

-P, --private -P, --private

Include “_private” modules.包括“_private”模块。 New in version 1.2. 1.2 版中的新功能。

The generated files will include an .. automodule:: directive for the private modules, this does have the side-effect of also including the :private-members: option as noted in another post "Include __main__.py in sphinx-apidoc generated files" .生成的文件将包含一个用于私有模块的.. automodule::指令,这确实具有还包括:private-members:选项的副作用,如另一篇文章“在 sphinx-apidoc 生成的文件中包含 __main__.py ”

Example explicitly including the .. automodule:: directives:显式包含.. automodule::指令的示例:

Your package rst
================

.. automodule:: your_package
    :members:
    :undoc-members:

    .. automodule:: your_package.public_module
        :members:
        :undoc-members:

    .. automodule:: your_package._private_module
        :members:
        :undoc-members:

The result:结果:

在此处输入图片说明

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

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