简体   繁体   English

Sphinx在我的文档中添加了有关NumPy模块的文档

[英]Sphinx adds documentation for NumPy modules to my docs

I want to generate Sphinx documentation for my Python code. 我想为我的Python代码生成Sphinx文档。 This code makes some imports from numpy . 此代码从numpy进行了一些导入。 I use sphinx-apidoc on mycode and then run make html . 我在mycode上使用sphinx-apidoc ,然后运行make html It generates the documentation, but also it includes the documentation for uniform function from numpy in it. 它生成文档,但也包括numpy中用于uniform函数的文档。 How to disable such redundant inclusions? 如何禁用这些多余的包含物?

Here is my code: 这是我的代码:

# encoding: utf-8
"""
This module does blah blah.
"""

from numpy.random import uniform  #WHY IS IT INCLUDED IN SPHINX DOC?!!!!

class UberMegaClass(object):
    """Hell yeah!"""
    def __init__(self, num_of_something):
        print("---")
        self.num_of_something = num_of_something

This is a known bug in Sphinx's handling of C++ extensions; 这是Sphinx处理C ++扩展的一个已知错误。 they can be included into documentation unnecessarily. 它们可以不必要地包含在文档中。 There is a semi-official workaround advising you to replace these modules with Mock objects in your conf.py , like this: 有一个半官方的解决方法,建议您用conf.py Mock对象替换这些模块,如下所示:

import sys

class Mock(object):

    __all__ = []

    def __init__(self, *args, **kwargs):
        pass

    def __call__(self, *args, **kwargs):
        return Mock()

    @classmethod
    def __getattr__(cls, name):
        if name in ('__file__', '__path__'):
            return '/dev/null'
        elif name[0] == name[0].upper():
            mockType = type(name, (), {})
            mockType.__module__ = __name__
            return mockType
        else:
            return Mock()

MOCK_MODULES = ['pygtk', 'gtk', 'gobject', 'argparse',
                'numpy', 'numpy.random']
for mod_name in MOCK_MODULES:
    sys.modules[mod_name] = Mock()

One way around that would be to change your rst. 解决该问题的一种方法是更改​​您的rst。

 .. automodule:: module

 .. autoclass:: module.UberMegaClass
     :members:
     :undoc-members:
     :show-inheritance:

That outputs: 输出:

This module does blah blah.

class module.UberMegaClass(num_of_something)
    Bases: object

    Hell yeah!

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

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