Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I reference a documented Python function parameter using Sphinx markup?

I'd like to reference a previously-documented function parameter elsewhere in a Python docstring. Consider the following (admittedly completely artificial) example:

def foo(bar):     """Perform foo action     :param bar: The bar parameter     """      def nested():         """Some nested function that depends on enclosing scope's bar parameter.         I'd like to reference function foo's bar parameter here         with a link, is that possible?"""         return bar * bar      # ...     return nested() 

Is there a simple way to embed a parameter reference using Sphinx markup, or will this happen automagically?

(I'm a complete Sphinx newbie. I've been scanning the Sphinx docs and haven't found an answer to this question, or an example demonstrating proper markup.)

like image 460
Inactivist Avatar asked Jun 23 '12 08:06

Inactivist


People also ask

What is Ivar in Python?

ivar is an "instance variable", or a variable that is set on an instance object (an instance of a class). Typically these would be defined (in Python) inside of an __init__ method.


2 Answers

There is no simple way to get a direct reference to a parameter of a function with sphinx and I don't know an extension for this problem.

The documentation of the python domain explains which objects can be cross referenced.

A possible way to give the user a reference to parameter bar of function foo would be

See parameter ``bar`` in :func:`foo`. 

Maybe a direct reference would be possible by writing an extension.

like image 76
bmu Avatar answered Sep 16 '22 14:09

bmu


I've just built an extension to accomplish this task. So far it seems to be working with standalone HTML build and additionally with readthedocs (after some more tweaks).

the extension is available at: https://pypi.python.org/pypi/sphinx-paramlinks/.

I'm rolling it out right now for the Alembic and SQLAlchemy projects. (sample).

I take disagreement with the suggestion that linking to params means the docs are too lengthy. The Python standard library is a poor example here as stdlib functions are necessarily granular and simple. Software that is accomplishing a more coarse-grained task, where a single function rides on top of a complex problem to be solved, will often have parameters that require a lot more explanation; this explanation is often quite valuable as the solution to a particular problem elsewhere, and therefore being able to link to it is very important.

like image 39
zzzeek Avatar answered Sep 16 '22 14:09

zzzeek