5

I'm writing documentation for package I've published, and I find the more thorough your documentation, the easier people find your package to use (duh). I'm actually having a lot of fun lovingly writing up all the features and details of my code.

However, I'm completely flummoxed by how to write Sphinx-compatible documentation for class-level variables. In particular, I've got some enum classes I'd like to document, but for the life of me I can't figure out a way to attach documentation to the enum values. The result is I've got these long and awkward sections of my documentation where there's no documentation except variable names.

I realize using straight docstrings is out of the question because variables don't have docstrings, but surely Sphinx has some sort of functionality around this? Otherwise, how would people document publicly visible values like constants?

bad_coder
  • 11,289
  • 20
  • 44
  • 72
alexgolec
  • 26,898
  • 33
  • 107
  • 159
  • Check [this answer](https://stackoverflow.com/a/60094069) about documenting Enums. The post may seem a bit contrived because its focus is circumventing a bug, however it includes usage of most elements your are bound to need when documenting Enum members. – bad_coder Apr 20 '20 at 02:57

1 Answers1

2

While it's true Python variables can't have docstrings. Using Sphinx autodoc extension, the autodata and autoattribute directives allow documenting variables and constants. Notice the use is different in case of a module level variable or a class member.

Additionally, should you want to arbitrate a value for the member in the documentation that is different from the programmatic value, the best way is using annotations.

autodata and autoattribute support the annotation option.

Sphinx can pick up comments on variable declarations and include them in the documentation (while those comments aren't docstrings they will be rendered in the documentation). Let's look at a minimal working example:

Source file your_module_name.py:

"""This modules documentation."""

ONE_CONSTANT = "A constant value."
"""Turns out the comment is rendered as a docstring if we put it underneath."""

#: Lets try it like this
TWO_CONSTANTS = 2000


class OneClass:
    """Commenting members of a class."""

    #: Lets try the third comment like this.
    THREE_CONSTANTS = 3000

    #: Lets try the forth comment like this.
    FOUR_CONSTANTS = 4000

Corresponding your_module_name.rst:

your\_module\_name module
=========================

.. automodule:: your_module_name
   :members: ONE_CONSTANT, TWO_CONSTANTS

   .. autodata:: ONE_CONSTANT
      :annotation: =this annotation

   .. autoclass:: OneClass
      :members:
      :undoc-members:
      :show-inheritance:

The resulting HTML:

enter image description here

Final note: This may force adapting some conventions you previously used for commenting variables in your source code. Also, if using annotations you'll want to not include that member in autodata or automodule to avoid it being included twice.

bad_coder
  • 11,289
  • 20
  • 44
  • 72
  • 1
    Sorry for the delayed response, but I'm attempting to implement your advice regarding enums here: https://github.com/alexgolec/tda-api/blob/autodoc-not-showing-enum-documentation/docs/streaming.rst and I'm finding instead of the beautiful docs you present it shows just the list of enum values with no documentation. In particular, check out the `QOSLevel` enum. Source is here: https://github.com/alexgolec/tda-api/blob/autodoc-not-showing-enum-documentation/tda/streaming.py#L328 – alexgolec May 28 '20 at 13:29
  • Please, let me confirm we're "on the same page" you mean `QOSLevel(Enum)` in `streaming.py` documented through `streaming.rst`? (Looks good btw...) I was under the impression you wanted to use Sphinx to generate HTML?! But the github page just renders the `.rst` as a source code excerpt. (Notice there's not 1 HTML file in your project, the reStructuredText-Sphinx directives aren't being processed to generate anything, they're just quoted as-is)...You could also use double-quotes in your docstrings for PEP8 conformity. Did you try `make html` locally?! – bad_coder May 28 '20 at 14:33
  • @Alex consider [taking the steps in this thread](https://stackoverflow.com/a/60159862) on your local machine. – bad_coder May 28 '20 at 14:37
  • Sorry, I should have been clearer. I am generating HTML, but link I sent is to github's view of the source of my documentation, and they do a best effort to render `rst` on their platform. HTML is generated by calling `make -f Makefile.sphinx html` according to the documentation here: https://github.com/alexgolec/tda-api/blob/master/CONTRIBUTING.rst. You can read the raw source here: https://raw.githubusercontent.com/alexgolec/tda-api/autodoc-not-showing-enum-documentation/docs/streaming.rst – alexgolec May 28 '20 at 14:45