Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 684
Aran-Fey Avatar asked Sep 17 '17 10:09

Aran-Fey


People also ask

What does Sphinx autodoc do?

autodoc imports the modules to be documented. If any modules have side effects on import, these will be executed by autodoc when sphinx-build is run. If you document scripts (as opposed to library modules), make sure their main routine is protected by a if __name__ == '__main__' condition.

Where is Conf PY located?

This is because our conf.py file is located in simpleble-master/docs/source , while our Python source codes, in this case the file simpleble.py , are located inside simpleble-master/simpleble .


2 Answers

Try adding add_module_names = False in conf.py

like image 193
Kanchan Srivastava Avatar answered Oct 23 '22 20:10

Kanchan Srivastava


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'] 
like image 36
Nikhil Kumar Avatar answered Oct 23 '22 19:10

Nikhil Kumar