简体   繁体   中英

Sphinx adds documentation for NumPy modules to my docs

I want to generate Sphinx documentation for my Python code. This code makes some imports from numpy . I use sphinx-apidoc on mycode and then run make html . It generates the documentation, but also it includes the documentation for uniform function from numpy in it. 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; 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:

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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