如何在没有模块名称的情况下记录类? [英] How do I document classes without the module name?

查看:57
本文介绍了如何在没有模块名称的情况下记录类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用 sphinx 记录一个 python 包,并成功生成了 html 文件.我正在记录的包由一组 *.py 文件组成,大多数包含一个类,其中有几个文件是定义了函数的真正模块.我不需要公开每个类都在一个模块中的事实,所以我在 __init__.py 文件中添加了合适的 from 语句,例如

I am trying to document a python package with sphinx and have successfully generated html files. The package I am documenting consists of a set of *.py files, most containing one class with a couple of files being genuine modules with functions defined. I do not need to expose the fact that each class is in a module so I have added suitable from statements in the __init__.py file e.g.

from base import Base

这样用户就可以使用 import pkg 命令,而不必指定包含类的模块:

so that the user can use the import pkg command and does not then have to specify the module that contains the class:

import pkg
class MyBase(pkg.Base):  # instead of pkg.base.Base ...
...

问题是 sphinx 坚持将类记录为 pkg.base.Base.我试图在 conf.py 中设置 add_module_names = False.然而,这会导致 sphinx 将该类简单地显示为 Base 而不是 pkg.Base.此外,这也破坏了模块的几个 *.py 文件的文档.

The problem is that sphinx insists on documenting the class as pkg.base.Base. I have tried to set the add_module_names = False in conf.py. However this results in sphinx showing the class as simply Base instead of pkg.Base. Additionally this also ruins the documentation of the couple of *.py files that are modules.

如何让 sphinx 将类显示为 pkg.Base?以及如何为每个 *.py 文件有选择地设置 add_module_names 指令?

How do I make sphinx show a class as pkg.Base? And how do I set the add_module_names directive selectively for each *.py file?

推荐答案

以下是完成 OP 要求的方法:

Here is a way to accomplish what the OP asks for:

  1. 添加__all__<pkg/__init__.py 中的/code> 列表:

  1. Add an __all__ list in pkg/__init__.py:

from base import Base    # Or use 'from base import *'

__all__ = ["Base"]

  • 在 .rst 文件中使用 .. automodule:: pkg.

    Sphinx 现在将输出文档,其中类名显示为 pkg.Base 而不是 pkg.base.Base.

    Sphinx will now output documentation where the class name is shown as pkg.Base instead of pkg.base.Base.

    这篇关于如何在没有模块名称的情况下记录类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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