2

I'm trying to write Sphinx docstrings that link to some classes and methods in other parts of my package, but can't figure out how to construct these links or get them to display as I need them to.

I have

# a.py

from .b import *

class A(object):

    def func_one():
        """Does stuff using `func_two` from `B`."""
        some_b = B ...
        some_b.func_two()
        # ...

and

# b.py

class B(object):

    def func_two():
        # ...

where my package is organized

my_package/
    a.py
    b.py

and I want the Sphinx docs for A.func_one to display as

Does stuff using func_two from B.

and to contain links to func_two and B.

I've tried various combinations of the full names of the method and class, but non seem to work. How do I accomplish this?

bad_coder
  • 11,289
  • 20
  • 44
  • 72
orome
  • 45,163
  • 57
  • 202
  • 418
  • 1
    Does this help? http://stackoverflow.com/a/22714510/407651 – mzjn Dec 03 '15 at 10:01
  • Yes, and I can even do `.b.func_two` rather than `my_package.b.func_two` to get a working link. But I still see "b.func_two" in the docs, and I'm shooting for "func_two". – orome Dec 03 '15 at 12:54

1 Answers1

10

To create cross-references to Python objects, use the roles (:class:, :meth:, etc.) provided by the Python domain. See https://www.sphinx-doc.org/en/master/usage/restructuredtext/domains.html#cross-referencing-python-objects.

Example:

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

To get the link text to be only the last part of the target, use a ~ (tilde) prefix. See https://www.sphinx-doc.org/en/master/usage/restructuredtext/domains.html#cross-referencing-syntax.

Example:

:py:meth:`~mymodule.MyClass.mymethod`
dan
  • 37
  • 1
  • 8
mzjn
  • 48,958
  • 13
  • 128
  • 248
  • And: if one sets `default_role = 'py:obj'` in `conf.py`, one can leave off the `:py:meth:` part. – orome Dec 03 '15 at 15:27
  • Maybe you can also help with [another Sphinx question](http://stackoverflow.com/q/34010116/656912). – orome Dec 03 '15 at 15:41