简体   繁体   English

Python-sphinx:启用“autodoc_mock_imports”时,不会显示子 class 的文档

[英]Python-sphinx: documentation of a child class won't show when "autodoc_mock_imports" is enabled

Question

Is there a way to generate sphinx documentation of a child class without installing the library containing its parent class on GitLab CI (or any comparable CI tool)?有没有一种方法可以生成子 class 的 sphinx 文档,而无需在 GitLab CI(或任何类似的 CI 工具)上安装包含其父 class 的库?

Edit : I have 7 such classes and ca.编辑:我有 7 个这样的类和 ca。 10 member functions per class on average to be documented.平均每个 class 有 10 个成员函数被记录。 Thus, an automated solution is strongly preferred, as it cost too much time to hard-code the docstrings into the .rst files.因此,强烈推荐使用自动化解决方案,因为将文档字符串硬编码到.rst文件中会花费太多时间。

If the problem cannot be solved by changing Sphinx settings alone, then I will only accept an answer that provides clear instructions to get the desired documentation generated and published.如果仅通过更改 Sphinx 设置无法解决问题,那么我将只接受提供明确说明以生成和发布所需文档的答案。

Context语境

Specifically, I made a child class of tensorflow.keras.callbacks.Callback and want to show its docstring on the documentation page.具体来说,我做了一个孩子 class of tensorflow.keras.callbacks.Callback并想在文档页面上显示它的文档字符串。 By default, Sphinx has to import everything generate documentation.默认情况下,Sphinx 必须导入所有生成文档。 But it doesn't seem right to install tensorflow (and tens of other libraries that sums up to several GBs) on the CI image just for this.但是仅仅为了这个就在 CI 镜像上安装tensorflow (以及其他数十个库,总计达数 GB)似乎并不合适。 I just want my docstring to be shown and I don't care about their parent classes.我只想显示我的文档字符串,我不关心它们的父类。 This is the reason why I turned on autodoc_mock_imports in conf.py (the Sphinx configuration file).这就是为什么我在conf.py (Sphinx 配置文件)中打开autodoc_mock_imports的原因。 The docs were build without error, but the documentation of that child class was missing.文档构建时没有错误,但缺少该子项 class 的文档。

In the MWE below, the customized class is keras_callback.py .在下面的 MWE 中,自定义的 class 是keras_callback.py The sphinx directives is contained in keras_callback.rst as follows. sphinx 指令包含在keras_callback.rst中,如下所示。

.. automodule:: keras_callback

    :members:
    :inherited-members:

Minimum Working Example最小工作示例

There is an MWE and Sphinx-generated docs on my GitLab repo to reproduce the problem.我的 GitLab 存储库中有一个MWESphinx 生成的文档来重现该问题。

Desired documentation of the child class looks like this.孩子 class 的所需文档如下所示。

在此处输入图像描述

At the minimum, the documentation of my custom function should be shown.至少,应该显示我的自定义 function 的文档。 Member functions from the parent class can be turned off.来自父级class的成员函数可以关闭。

In addition to the "auto" directives (such as automodule and autoclass ) that extract documentation from Python code, Sphinx also provides "non-auto" directives ( module , class etc.) where all the documentation goes into.rst files.除了从 Python 代码中提取文档的“自动”指令(例如automoduleautoclass )之外,Sphinx 还提供“非自动”指令moduleclass等),其中所有文档都进入 .rst 文件。

My suggestion is to replace .. automodule:: keras_callback with the following:我的建议是将.. automodule:: keras_callback替换为以下内容:

.. class:: keras_callback.MyKerasCallback
 
   An inherited Keras ``Callback`` class.
 
   .. method:: __init__(dic=None)
 
      Constructor
 
   .. method:: on_epoch_begin(epoch, logs=None)
           
      Inherited method
 
   .. method:: custom_method
 
      Custom method

.. autofunction:: keras_callback.util_func

I finally found a simple workaround: Build locally and then overwrite the CI-built page using the locally built page.我终于找到了一个简单的解决方法:在本地构建,然后使用本地构建的页面覆盖 CI 构建的页面。 If the desired page need not rebuild frequently, then this solution may save considerable time hard-coding the members.如果不需要经常重建所需的页面,则此解决方案可以节省大量时间来对成员进行硬编码。

Steps脚步

  1. Build locally without autodoc_mock_imports in conf.py .conf.pyautodoc_mock_imports在本地构建。

  2. Copy the correct webpage ( keras_callback.html ) to _static folder.将正确的网页 ( keras_callback.html ) 复制到_static文件夹。

  3. Re-enable autodoc_mock_imports .重新启用autodoc_mock_imports

  4. Add a cp command to overwrite the CI-built page in .gitlab-ci.yml.gitlab-ci.yml中添加cp命令覆盖 CI 构建的页面

image: python:3.7-alpine pages: script: - pip install sphinx sphinx-rtd-theme recommonmark - sphinx-build -d _build/doctrees. _build/html - mv _build/html public - cp _static/keras_callback.html public artifacts: paths: - public only: - master
  1. Commit, push and check the webpage.提交,推送并检查网页。 Worked for this particular MWE (not shown in the repo).为这个特定的 MWE 工作(未在 repo 中显示)。

The drawback is of course that the maintainer has to rebuild that page manually whenever it is updated.缺点当然是维护者必须在更新该页面时手动重建该页面。 But this should suffice for many small independent projects because docs-publishing usually happens only at the very end stage of development.但这对于许多小型独立项目来说应该足够了,因为文档发布通常只发生在开发的最后阶段。

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

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