Sphinx automodule: how to reference classes in same module?

dino picture dino · Oct 13, 2011 · Viewed 21.6k times · Source

I am trying to use the sphinx autodoc extension and specifically the automodule directive to automatically generate documentation for django app I am working on. The problem is that I want to create internal references to different classes within the module, without having to use autoclass and autofunction on every single class/function within the project. For a source file like this:

# source_code.py
class A:
    """docs for A
    """
    pass

class B:
    """docs for B with 
    :ref:`internal reference to A <XXXX-some-reference-to-A-XXXX>`
    """
    pass

I would like to be able to have a sphinx documentation file like this:

.. automodule: source_code

What reference can I use for XXXX-some-reference-to-A-XXXX? Is there an easy way to accomplish this? Thanks in advance for your help.

Answer

jterrace picture jterrace · Oct 13, 2011

You can reference a class like this:

class B(object):
    """docs for B with reference to :class:`.A`"""
    pass

Sphinx will intelligently try and figure out what you're referencing. If there are multiple classes with the name A, you might get a warning, but it should pick up the one in the current module.