Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would I cross-reference a function generated by autodoc in Sphinx?

I am using the Sphinx autodoc feature to generate documentation based on the docstrings of my Python library.

The syntax for cross referencing is found here

A label must precede the section in order to allow that section to be referenced from other areas of the documentation.

What I have is a .rst (ReStructeredText) file for one of my classes. It uses

.. autoclass:: classname
    :members:

To generate documentation for the class.

My question is, how would I reference the auto-generated methods of the class from another .rst document in the documentation? If I try to place a label within the method's docstring, Sphinx complains. If I try to place a label before the method heading, Sphinx doesn't recognize it.

Is there a simple way to do this, or will I have to explicitly write in my class file the method name and precede that with a label?

Here is an example a reference within the [Python documentation2 doing what I need (I am assuming it used the autodoc feature, though I don't know for sure)

like image 291
Matthew Stamy Avatar asked Mar 27 '14 22:03

Matthew Stamy


People also ask

How does Sphinx Autodoc work?

autodoc provides several directives that are versions of the usual py:module , py:class and so forth. On parsing time, they import the corresponding module and extract the docstring of the given objects, inserting them into the page source under a suitable py:module , py:class etc. directive.

What is Intersphinx?

ext. intersphinx – Link to other projects' documentation. New in version 0.5. This extension can generate links to the documentation of objects in external projects, either explicitly through the external role, or as a fallback resolution for any other cross-reference.


2 Answers

You don't need to add labels. In order to refer to a Python class, method, or other documented object, use the markup provided by the Python domain.

For example, the following defines a cross-reference to the mymethod method:

:py:meth:`mymodule.MyClass.mymethod`

Or even simpler (since the Python domain is the default):

:meth:`mymodule.MyClass.mymethod`

The documentation of TextWrapper.wrap that you link to in the question includes two cross-references of this kind (click on "Show Source" to see the reST markup).

like image 53
mzjn Avatar answered Sep 21 '22 11:09

mzjn


In addition to the excellent answer already provided:

To add an alias to the referenced module (method, function, attribute, etc.), the following syntax is used:

:mod:`Alias Name <package.module>`

This will appear as a reference to Alias Name in the docs, and link to the module provided.

like image 23
S3DEV Avatar answered Sep 19 '22 11:09

S3DEV