pythonnumpydoc

How to type a list of int in numpydoc


What is the correct way to type a list of int in a numpydoc docstring.

Is this syntax int[] for example valid ?

def my_function(numbers):
    """
    Parameters
    ----------
    numbers : int[]
        List of numbers
    """

    return numbers


Solution

  • According to numpydoc docstring guide, under Sections - 4.Parameters, you find an example on how to document a list of str, so just change that to list of int.

    enter image description here

    The reason, to document it explicitly as type list of int and not just as int [], is because the square brackets [] in python (frequently called "array indexing") indicate the object can be indexed, sliced, and iterated over. The square bracket syntax, by itself, does not distinguish if you are dealing with an array, or with a list, except initially when instantiating the object. As python docs indicate, square brackets with the constructor of list([iterable]) indicate the object inside the brackets must only be iterable.

    enter image description here

    This is made evident when put side-by-side:

    def my_function_one(numbers):
        """
        Parameters
        ----------
        numbers : int[]
            List of numbers
        """
    
        return numbers
    
    
    def my_function_two(numbers):
        """
        Parameters
        ----------
        numbers : list of int
            List of numbers
        """
    
        return numbers
    

    The result so you can compare the two:

    list_of_ints_docstring