Search code examples
pythonpython-sphinxdocstringautodoc

How to indicate a return type that is nested (e.g. a list of ints)


I have a python function with nested return types, how can I indicate the return types correctly? (For example pycharm might detect the return type as Tuple[list, int, int] where every type is linked individually)

e.g.

def random_list(num):
    """Returns a list of random numbers
    :param num:
        Number of random numbers to generate
    :type num:
        int
    :return:
        A list of random floats
    :rtype:
        ????
    """
    return [random() for x in range(num)]

Solution

  • I would look into type hints and use the same expression, e.g:

    from typing import List
    
    def random_list(num: int) -> List[float]:  # Py 3.5
        """Returns a list of random numbers
        :param num:
            Number of random numbers to generate
        :type num:
            int
        :return:
            A list of random floats
        :rtype:
            List[float]
        """
        return [random() for x in range(num)]