简体   繁体   English

使用动态类和动态模块时如何使用Sphinx自动生成Python文档

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

I currently have a use case where I am creating Python classes in modules using the following code. 我目前有一个用例,其中我在使用以下代码在模块中创建Python类。

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

However Sphinx is not able to generate documentation for these classes. 但是,Sphinx无法为这些类生成文档。 It is not even able to find the classes. 它甚至找不到类。 Is there a way to add an extension to Sphinx to handle this use case? 有没有一种方法可以向Sphinx添加扩展以处理此用例? Thoughts? 有什么想法吗?

The full minimum working example for this is located here . 完整的最低工作示例位于此处

You have the following automodule directive in modules.rst: 您在modules.rst中具有以下automodule指令:

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

But the qualified names of the dynamic modules are modules.Module1 and modules.Module2 (these names are added to sys.modules in _factory.py). 但是动态模块的合格名称是modules.Module1modules.Module2 (这些名称已添加到_factory.py中的sys.modules中)。

The following works for me: 以下对我有用:

.. 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