簡體   English   中英

使用動態類和動態模塊時如何使用Sphinx自動生成Python文檔

[英]How to autogenerate Python documentation using Sphinx when using dynamic classes and dynamic modules

我目前有一個用例,其中我在使用以下代碼在模塊中創建Python類。

...
module = type.ModuleType(module_name)
...
klass = type(name, (object, ), dict(__doc__='docstring'))
...
setattr(module, name, klass)
...

但是,Sphinx無法為這些類生成文檔。 它甚至找不到類。 有沒有一種方法可以向Sphinx添加擴展以處理此用例? 有什么想法嗎?

完整的最低工作示例位於此處

您在modules.rst中具有以下automodule指令:

.. automodule:: modules
   :members:
   :undoc-members:
   :show-inheritance:

但是動態模塊的合格名稱是modules.Module1modules.Module2 (這些名稱已添加到_factory.py中的sys.modules中)。

以下對我有用:

.. automodule:: modules.Module1
    :members:
    :undoc-members:
    :show-inheritance:

.. automodule:: modules.Module2
    :members:
    :undoc-members:
    :show-inheritance:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM