Question

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
Was it helpful?

Solution

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()

OTHER TIPS

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!
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top