Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include docstrings/comments located in a module, but outside of class and methods in Sphinx

I have a utils module in my package. It consists of several misc standalone methods that do not require instantiation.

I would like to place some generic comments/docstring inside this utils file, such as:

import os
import json

"""
Miscellaneous methods that help in <<blah blah>>
Does not require explicit instantiation.

The following actions can be performed:
===============   ===============
Action            Method
===============   ===============
Get a             :meth:`methoda`
Get b             :meth:`methodb`
"""

def methoda(data):
    "Gets A ..."
    ...

def methodb(data):
    "Gets B ..."
    ...

As seen above, the docstring has a table including links to the individual methods. Currently, my index.rst has this part to include utils:

Utilities
============
.. automodule:: packagename.utils
   :members:

Currently, I get the docstrings of the individual methods properly displayed in the documentation, but not the top-level docstring of the module (outside of any class or method). What is the best way to have have sphinx include the above?

One option might be to move the top-level docstring outside of this file, such as to index.rst etc. But I would prefer not doing this, and retain it within the source file.

like image 702
Shailesh Appukuttan Avatar asked Nov 02 '17 09:11

Shailesh Appukuttan


People also ask

How do you comment out a docstring in a function?

Declaring Docstrings: The docstrings are declared using ”'triple single quotes”' or “””triple double quotes””” just below the class, method or function declaration. All functions should have a docstring.

Where do you put docstrings?

Module docstrings are placed at the top of the file even before any imports. Module docstrings should include the following: A brief description of the module and its purpose. A list of any classes, exception, functions, and any other objects exported by the module.

Is docstring a comment in Python?

A python comment may be a single line comment or a multiline comment written using single line comments or multiline string constants. Document strings or docstrings are also multiline string constants in python but they have very specific properties unlike python comment.

Are docstrings same as comments?

Comments are great for leaving notes for people working on your program. Docstrings provide documentation about functions, classes, and modules. Use docstrings to teach other developers how to use your program.


1 Answers

Thanks to jonsharpe for his comment, and leading me to the proper way of doing this.

For future reference by others, I basically moved the docstrings to the start of the file:

"""
Miscellaneous methods that help in <<blah blah>>
Does not require explicit instantiation.

The following actions can be performed:
===============   ===============
Action            Method
===============   ===============
Get a             :meth:`methoda`
Get b             :meth:`methodb`
"""

import os
import json

def methoda(data):
    "Gets A ..."
    ...

def methodb(data):
    "Gets B ..."
    ...

And that's it! Everything works.

like image 185
Shailesh Appukuttan Avatar answered Oct 20 '22 02:10

Shailesh Appukuttan