函数在 Sphinx autodoc 生成的文档中出现两次

发布于 2025-01-12 01:33:38 字数 1383 浏览 2 评论 0原文

背景信息

我通过 Sphinx autodoc 从文档字符串生成文档。源文件 mypackage/mypackage.py 中有一个函数 mypackage.mypackage.foo()。它在 __init__.py 中隐式导入,因此用户可以将其用作 mypackage.foo()

我确实操作了 Sphinx autodoc,它生成了 foo() 的文档,尊重它们的导入方式。这有效。

问题

关于foo()的文档生成了两次;一次为 mypackage.foo(),一次为 mypackage.mypackage.foo()

输入图片此处描述

详细信息

这是项目的结构:

mypackage
├── a.py
├── __init__.py
└── mypackage.py

__init__.py 执行导入,然后操作__all__变量:

from .mypackage import *
__all__ = ['foo']  # for alternative see: https://stackoverflow.com/a/66996523/4865723

以及mypackage/mypackage.py 定义 foo()

因此,我可以做这样的事情:

import mypackage
mypackage.foo()

相关问题和解答

以下是带我进入当前“部分解决方案”的问题和答案。其中 mypackage.foo() 按文档中的预期显示,但 mypackage.mypackage.foo() 不会消失。

Background information

I generate documentation from docstrings via Sphinx autodoc. There is a function mypackage.mypackage.foo() located in source file mypackage/mypackage.py. It is imported implicite in the __init__.py, so the user can it use as mypackage.foo().

I do manipulate Sphinx autodoc that it generates the docu for foo() respecting they way it is imported. This works.

The problem

The docu about foo() is generated twice; once as mypackage.foo() and once as mypackage.mypackage.foo().

enter image description here

Details

This is the structure of the project:

mypackage
├── a.py
├── __init__.py
└── mypackage.py

The __init__.py does the import and then manipulates the __all__ variable:

from .mypackage import *
__all__ = ['foo']  # for alternative see: https://stackoverflow.com/a/66996523/4865723

And the mypackage/mypackage.py defines foo().

Because of that I can do things like that:

import mypackage
mypackage.foo()

Related questions and answers

Here are questions and answers that took me to the current "part solution". Where mypackage.foo() appears as expected in the documentation but mypackage.mypackage.foo() does not disappear.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黯然#的苍凉 2025-01-19 01:33:39

@mzjn 给了我正确的方向。他是正确的,有两个 automodule 指令。因此,我们的目标是让 sphinx-apidoc(生成指令)忽略 mypackage/mypackage.py。

为此,使其成为“私有模块”,并修改其文件名以下划线(_)开头。

由此产生的项目结构:

mypackage
├── a.py
├── __init__.py
└── _mypackage.py

并且__init__.py中的导入需要修改为from ._mypackage import *

@mzjn gave me the right direction. He is correct that there are two automodule directives. So the goal is to make sphinx-apidoc (which generates the directives) ignore mypackage/mypackage.py.

For that make it a "private module" with modyfing it's filename starting with an underscore (_).

The resulting project structure:

mypackage
├── a.py
├── __init__.py
└── _mypackage.py

And the import in __init__.py need to modified to from ._mypackage import *.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文