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

后端 未结 8 1002
清酒与你
清酒与你 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:17

    If you are looking for how to do this in numpydoc style, you can simply mention **kwargs in Parameters section without specifying type - as demonstrated in numpydoc example from the sphinx extension napolean and docstring guide from pandas documentation sprint 2018.

    Here's an example I found from LSST developer guide which very well explains what should be the description of **kwargs parameter:

    def demoFunction(namedArg, *args, flag=False, **kwargs):
        """Demonstrate documentation for additional keyword and
        positional arguments.
    
        Parameters
        ----------
        namedArg : `str`
            A named argument that is documented like always.
        *args : `str`
            Additional names.
    
            Notice how the type is singular since the user is expected to pass individual
            `str` arguments, even though the function itself sees ``args`` as an iterable
            of `str` objects).
        flag : `bool`
            A regular keyword argument.
        **kwargs
            Additional keyword arguments passed to `otherApi`.
    
            Usually kwargs are used to pass parameters to other functions and
            methods. If that is the case, be sure to mention (and link) the
            API or APIs that receive the keyword arguments.
    
            If kwargs are being used to generate a `dict`, use the description to
            document the use of the keys and the types of the values.
        """
    

    Alternatively, building upon what @Jonas Adler suggested, I find it better to put the **kwargs and its description in Other Parameters section - even this example from matplotlib documentation guide suggests the same.

提交回复
热议问题