What is the correct way to document a **kwargs parameter?

后端 未结 8 1001
清酒与你
清酒与你 2021-01-30 02:48

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 exam

8条回答
  •  执笔经年
    2021-01-30 03:41

    After finding this question I settled on the following, which is valid Sphinx and works fairly well:

    def some_function(first, second="two", **kwargs):
        r"""Fetches and returns this thing
    
        :param first:
            The first parameter
        :type first: ``int``
        :param second:
            The second parameter
        :type second: ``str``
        :param \**kwargs:
            See below
    
        :Keyword Arguments:
            * *extra* (``list``) --
              Extra stuff
            * *supplement* (``dict``) --
              Additional content
    
        """
    

    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.

提交回复
热议问题