How can I prevent sphinx from displaying the full path to my class?

4.3k views Asked by At

I have a project structure like this:

package/
    __init__.py
    module.py

__init__.py contains:

from .module import Class

module.py contains:

class Class:
    pass

Using sphinx-apidoc -o package/docs/ package/ and sphinx-build package/docs/ package/docs/_build, the documentation for Class looks like this:

class package.module.Class

     Bases: object

I'd like to have this output instead:

class package.Class

     Bases: object

Or, even better, without the package name:

class Class

     Bases: object

The user doesn't have to know in which file the class is defined; this information is completely irrelevant, if not confusing. Since __init__.py is importing Class directly into the package's namespace, users will import this class as from package import Class, not as from package.module import Class, and I want the docs to reflect that.

Is there a way to have sphinx output the path relative to the package's namespace?

2

There are 2 answers

0
Kanchan Srivastava On

Try adding add_module_names = False in conf.py

0
Nikhil Kumar On

While adding add_module_names = False (refer this answer) makes Sphinx render package.module.Class as Class, it does not help if you wanted to render package.module.Class as package.Class (i.e., document the class as part of the package namespace).

If you want Sphinx to document package.module.class as package.Class, include the following lines in the __init__.py for the package (refer this answer):

# This lets you use package.module.Class as package.Class in your code.
from .module import Class

# This lets Sphinx know you want to document package.module.Class as package.Class.
__all__ = ['Class']