Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Referencing parameters in a Python docstring

I use Sphinx and the autodocs feature to ensure we have good docs in our project.

So I'm familiar with info field lists and I'm familiar with using cross-referencing in our docs.

However, when writing docstring for a method or function I find it useful to refer to their parameters in the text. But there doesn't seem to be a structured way to do this.

  1. We could say e.g.

    Use ``name`` to set the username
    

    but that has no structure, requires you to remember what style you used for that and if you change style you have to hunt down and kill all the incorrect styles.

  2. :param: doesn't work outside of a info field list so you can't write

    Use :param:`name` to set the username
    
  3. I've seen some projects use :parm: but that isn't documented and doesn't seem to work. So they must have some customisation

  4. I could use generic_roles but that seems like me working around a problem that I'm sure others have encountered.

So hopefully I've just missed something blindingly obvious.

like image 518
Dwayne Avatar asked Dec 14 '12 13:12

Dwayne


People also ask

What should be included in a docstring Python?

Class method docstrings should contain the following: A brief description of what the method is and what it's used for. Any arguments (both required and optional) that are passed including keyword arguments. Label any arguments that are considered optional or have a default value.

How do you access a docstring in Python?

All functions should have a docstring. Accessing Docstrings: The docstrings can be accessed using the __doc__ method of the object or using the help function.

How do you make a good Python docstring?

Best practicesAll modules, classes, methods, and functions, including the __init__ constructor in packages should have docstrings. Descriptions are capitalized and have end-of-sentence punctuation. Always use """Triple double quotes.""" around docstrings. Docstrings are not followed by a blank line.

What should a function docstring contain?

The docstring for a function or method should summarize its behavior and document its arguments, return value(s), side effects, exceptions raised, and restrictions on when it can be called (all if applicable). Optional arguments should be indicated.


1 Answers

You can write your own extension using autodoc-process-docstring - it's really simple.

Have the extension search for :param: and replace it with your choice of style.

like image 86
sions Avatar answered Oct 01 '22 01:10

sions