Referencing long names with Python Sphinx
Asked Answered
S

4

18

I'm working on documentation for my Python module (using Sphinx and reST), and I'm finding that when cross-referencing other Python objects (modules, classes, functions, etc) the full object name ends up being incredibly long. Often it is longer than 80 characters, which I would like to avoid at all costs.

Here is an example:

def exampleFunction():
    '''Here is an example docstring referencing another
    :class:`module1.module2.module3.module4.module5.ReallyLongExampleClassName`

    '''

The issue is that when creating the documentation for the ReallyLongExampleClassName class, I generated it for the full path name module1.module2.module3.module4.module5.ReallyLongExampleClassaName.

I'm wondering if there is any way to solve this? I have tried the following methods, with no success:

1) Adding a line break in the middle of the module name. Example:

:class:`module1.module2.module3.module4.
module5.ReallyLongExampleClassName`

2) Referencing the class name in a different (but still Python importable) way. Example:

:class:`module1.module2.ReallyLongClassName`

I believe that since the documentation for ReallyLongClassName is tied to the full path names that Sphinx cannot correlate the shortened version with the fully named version.


Edit 04/05/2012:

As per the answer/suggestion of j13r (see below) I tried the following:

:class:`module1.module2.module3.module4.module5\
ReallyLongExampleClassName`

And this worked successfully. The only caveat to get this to work, is that the second line must not have spaces before it (which is quite frustrating when using this in a docstring). Thus to make my original example work it would look like:

def exampleFunction():
    '''Here is an example docstring referencing another
    :class:`module1.module2.module3.module4.module5.\
ReallyLongExampleClassName`

    '''

Nice, and ugly. If you were to put spaces before ReallyLongExampleClassName to indent it to the same level as the line above it the output would include the spaces and thus Sphinx would try to reference something like module1.module2.module3.module4.module5.ReallyLongExampleClassName.

I should also note that I tried two other variations of this, which did NOT work:

    # Note: Trying to put a space before the '\'
    :class:`module1.module2.module3.module4.module5. \
ReallyLongExampleClassName`

    # Note: Trying to leave out the '\'
    :class:`module1.module2.module3.module4.module5.
ReallyLongExampleClassName`

I was looking for a solution that didn't involve destroying the formatting of the docstring, but I suppose it will do...I think I actually prefer a line that goes past 80 characters to this.

Thanks to j13r for the answer!

Slapbang answered 5/4, 2012 at 18:52 Comment(0)
C
24

According to the sphinx documentation (https://www.sphinx-doc.org/en/master/usage/restructuredtext/domains.html#cross-referencing-python-objects) you could use a dot before your target class:

:class:`.ReallyLongExampleClassName`

or

:class:`.module5.ReallyLongExampleClassName`

and let sphinx search for the class:

... if the name is prefixed with a dot, and no exact match is found, the target is taken as a suffix and all object names with that suffix are searched. For example, :py:meth:.TarFile.close references the tarfile.TarFile.close() function, even if the current module is not tarfile. Since this can get ambiguous, if there is more than one possible match, you will get a warning from Sphinx.

Coral answered 8/4, 2012 at 21:43 Comment(1)
This only seems to work in docstrings, not regular documentation using py:func: to reference autogenerated documentationModify
M
6

You can use ~ as prefix, it does exactly what you want.

https://www.sphinx-doc.org/en/master/usage/restructuredtext/roles.html#cross-referencing-syntax

Misplace answered 10/7, 2015 at 10:26 Comment(0)
S
2

Another strategy is to use reST Substitutions. This will let you save more space in the text by calling the :class: cross-reference later on:

def exampleFunction():
    '''Here is an example docstring referencing another
    |ReallyLongExampleClassName|

    .. |ReallyLongExampleClassName| replace:: 
                                    :class:`.ReallyLongExampleClassName`

    '''

If you're referring to the same class in many of your files, you could instead put the substitution in your Sphinx conf.py file, using the rst_epilog setting. From the Sphinx documentation:

rst_epilog

A string of reStructuredText that will be included at the end of every source file that is read. This is the right place to add substitutions that should be available in every file. An example:

rst_epilog = """
.. |psf| replace:: Python Software Foundation
"""

New in version 0.6.

Then your docstring would just be:

def exampleFunction():
    '''Here is an example docstring referencing another
    |ReallyLongExampleClassName|

    '''
Subjectivism answered 3/2, 2016 at 2:28 Comment(1)
Awesome! I was using (e.g.,) :ref:`<topic-name>` to put links in some table column headers, which was making the whole source table unmaintainably wide. Changing the headers to just |topic| makes it much easier to edit.Denticulate
C
1

Wild stab in the dark. Perhaps this works:

:class:`module1.module2.module3.module4.\
module5.ReallyLongExampleClassName`

It would be valid Python

import scipy.\
stats
Cowles answered 5/4, 2012 at 22:6 Comment(1)
Doesn't work at all for me. In docstring, as a Python string, \⟨newline⟩ is removed so it seems impossible for this to work.Miamiami

© 2022 - 2024 — McMap. All rights reserved.