I'm using sphinx and the autodoc plugin to generate API documentation for my Python modules. Whilst I can see how to nicely document specific parameters, I cannot find an example of how to document a **kwargs
parameter.
Does anyone have a good example of a clear way to document these?
Google Style docstrings parsed by Sphinx
Disclaimer: not tested.
From this cutout of the sphinx docstring example, the
*args
and**kwargs
are left unexpanded:I would suggest the following solution for compactness:
Notice how,
Optional
is not required for**key
arguments.Otherwise, you can try to explicitly list the *args under
Other Parameters
and**kwargs
under theKeyword Args
(see parsed sections):There is a doctstring example for Sphinx in their documentation. Specifically they show the following:
Though you asked about sphinx explicitly, I would also point to the Google Python Style Guide. Their docstring example seems to imply that they don't call out kwargs specifically. (other_silly_variable=None)
A-B-B has a question about the accepted answer of referencing the subprocess management documentation. If you import a module, you can quickly see the module docstrings via inspect.getsource.
An example from the python interpreter using Silent Ghost's recommendation:
Of course you can also view the module documentation via help function. For example help(subprocess)
I'm not personally a fan of the subprocess docstring for kwargs as an example, but like the Google example it doesn't list kwargs seperately as shown in the Sphinx documentation example.
I'm including this answer to A-B-B's question because it's worth noting that you can review any module's source or documentation this way for insights and inspiration for commenting your code.
After finding this question I settled on the following, which is valid Sphinx and works fairly well:
The
r"""..."""
is required to make this a "raw" docstring and thus keep the\*
intact (for Sphinx to pick up as a literal*
and not the start of "emphasis").The chosen formatting (bulleted list with parenthesized type and m-dash-separated description) is simply to match the automated formatting provided by Sphinx.
Once you've gone to this effort of making the "Keyword Arguments" section look like the default "Parameters" section, it seems like it might be easier to roll your own parameters section from the outset (as per some of the other answers), but as a proof of concept this is one way to achieve a nice look for supplementary
**kwargs
if you're already using Sphinx.I think
subprocess
-module's docs is a good example. Give an exhaustive list of all parameters for a top/parent class. Then just refer to that list for all other occurrences of**kwargs
.This depends on the style of documentation you use, but if you are using the numpydoc style it is recommend to document
**kwargs
usingOther Parameters
.For example, following quornian's example:
Note especially that it is recommended to give the defaults of kwargs, since these are not obvious from the function signature.
If anyone else is looking for some valid syntax.. Here's an example docstring. This is just how I did it, I hope it's useful to you, but I can't claim that it's compliant with anything in particular.