MarkG
MarkG

Reputation: 43

How to exclude imports from automodapi output?

I am trying to use automodapi to generate documentation for my Django project. When I call automodapi like this:

.. automodapi:: mypackage.mymodule

the output includes all imported classes and functions, e.g, the Django Model class, in the index of Functions and Classes. I would like to exclude the imports and only list those classes and functions declared in the module I specified.

I couldn't see anything about this in the documentation.

Is there a way to do this, preferably without modifying modules?

UPDATE: @saimn has provided a working solution using __all__ but my project doesn't use __all__. It would be nice if there was a solution that didn't involve modifying the modules.

Upvotes: 1

Views: 530

Answers (2)

jmm
jmm

Reputation: 394

Patching automodapi to only include locals also addresses this issue while not requiring any changes to your code:

def patch_automodapi(app):
    """Monkey-patch the automodapi extension to exclude imported members"""
    from sphinx_automodapi import automodsumm
    from sphinx_automodapi.utils import find_mod_objs
    automodsumm.find_mod_objs = lambda *args: find_mod_objs(args[0], onlylocals=True)

def setup(app):
    app.connect("builder-inited", patch_automodapi)

Source: https://github.com/astropy/sphinx-automodapi/issues/119

The above snippet goes into your conf.py sphinx configuration file.

Upvotes: 2

saimn
saimn

Reputation: 443

You can use the __all__ variable (this should probably be stated more clearly in the documentation).

Upvotes: 1

Related Questions