Search code examples
pythonpython-sphinxcross-reference

Referencing Python method documentation without full class name in Sphinx


I'm trying to reference

http://docs.python.org/2.7/reference/datamodel.html#object.__enter__

from my python Sphinx documentation, and would like the link in the documentation to look as it would for a func :func: role if __enter__ were implemented in the current class (i.e, in the same style and simply as __enter__ and not as object.__enter__.

Is there a way to accomplish this?


Solution

  • If you have set up intersphinx, the following markup will produce a hyperlink to the specified target in the Python documentation with the link text __enter__:

    :meth:`__enter__ <object.__enter__>`
    

    An alternative:

    :meth:`~.object.__enter__`
    

    This will result in the link text __enter__() (with parentheses by default; see http://sphinx-doc.org/config.html#confval-add_function_parentheses).

    Or just use this:

    `__enter__ <http://docs.python.org/2.7/reference/datamodel.html#object.__enter__>`_
    

    See http://sphinx-doc.org/markup/inline.html#cross-referencing-syntax and http://sphinx-doc.org/rest.html#hyperlinks.