pythonparameterspython-sphinxsphinx-napoleon

Should I document parameters for similar function signatures?


I have some helper functions that, except for the first argument, take the same arguments as the core function. The parameters are thoroughly documented in the core function. Should I copy-paste this documentation to the helper function too, or rather just point to the core documentation?

It that matters, I primarily intend my API reference to be read as HTML generated by Sphinx, and I use the numpydoc style. I didn't find an answer in the numpydoc manual.

EDIT

Here is an MWE:

def core(param0, param1=3, param2=8):
    """Core function with thorough documentation.

    Parameters
    ----------
    param0 : ndarray
        Description.
    param1 : int
        Long description.
    param2 : int
        Long description.

    Returns
    -------
    param_out : ndarray
        Long description

    """
    pass
def helper(param3, param1=3, param2=8):
    """Helper function.

    """
    pass

As you can see, only the first parameter differs in the two functions.


Solution

  • The best, and easiest, way to do this is using Python Sphinx docstring sections from the sphinx.ext.napoleon extension.

    Only arguments unique to the helper function need to be explicitly documented, you can remit with a cross-reference to the function/method defining the shared parameters. The Google style guide for Python advises the same reasoning for overloading inherited methods from a base class :

    A method that overrides a method from a base class may have a simple docstring sending the reader to its overridden method’s docstring, such as """See base class.""". The rationale is that there is no need to repeat in many places documentation that is already present in the base method’s docstring. However, if the overriding method’s behavior is substantially different from the overridden method, or details need to be provided (e.g., documenting additional side effects), a docstring with at least those differences is required on the overriding method.

    • Args:

      List each parameter by name. A description should follow the name, and be separated by a colon and a space.

    The following example:

    def core(param0, param1=3, param2=8):
        """Core function with thorough documentation.
    
        Parameters
        ----------
        param0 : ndarray
            Description.
        param1 : int
            Long description.
        param2 : int
            Long description.
    
        Returns
        -------
        param_out : ndarray
            Long description
    
        """
        pass
    
    def helper(param3, param1=3, param2=8):
        """Remaining
    
        Parameters
        ----------
        param3 : int
            Description.
        Other Parameters
            A short string remitting reader to :py:func:`core` function.
        See Also
            A short string remitting reader to :py:func:`core` function.
        Note
            A short string remitting reader to :py:func:`core` function.
        """
        pass
    

    Would give this result:

    enter image description here