Python > Core Python Basics > Functions > Docstrings

Advanced Docstring Example with Type Hints and Exceptions

This snippet builds upon the basic docstring example, demonstrating how to incorporate type hints and document potential exceptions. Including this information makes the documentation even more comprehensive and helpful.

Docstring with Type Hints and Exceptions

In this example, we've added type hints (: float and -> float) to specify the expected data types of the arguments and the return value. We've also included a Raises: section to document the ZeroDivisionError exception that the function might raise if the denominator is zero. This information helps users understand the potential issues and how to handle them.

def divide(x: float, y: float) -> float:
    """Divides two numbers.

    Args:
        x: The numerator.
        y: The denominator.

    Returns:
        The result of the division.

    Raises:
        ZeroDivisionError: If the denominator is zero.
    """
    if y == 0:
        raise ZeroDivisionError("Cannot divide by zero")
    return x / y

help(divide)

Using Sphinx for Documentation Generation

Sphinx is a powerful tool for generating documentation from Python docstrings. It supports various formats, including HTML, PDF, and LaTeX. Sphinx uses reStructuredText (reST) as its markup language, which provides a rich set of formatting options. Sphinx automatically generates indexes, tables of contents, and cross-references, making it easy to navigate the documentation.

To use Sphinx, you'll typically create a conf.py file to configure the documentation project. You'll then use Sphinx to extract the docstrings from your Python code and generate the documentation in the desired format.

Here's a basic example of how to use Sphinx:

  1. Install Sphinx: pip install sphinx
  2. Create a Sphinx project: sphinx-quickstart
  3. Configure conf.py to point to your Python code.
  4. Run sphinx-build to generate the documentation.

Benefits of using Type Hints in Docstrings

Type hints enhance code readability and help prevent errors during development. They make it clear what data types are expected and returned by functions, reducing the risk of type-related bugs. Type hints also improve the effectiveness of static analysis tools, which can detect potential type errors before runtime.

FAQ

  • What are type hints?

    Type hints are annotations that specify the expected data types of variables, function arguments, and return values. They were introduced in Python 3.5 and are used to improve code readability and prevent type-related errors.
  • How do I handle exceptions in my docstrings?

    Use the Raises: section to document any exceptions that your function might raise. Include the exception type and a brief description of the conditions under which it is raised.
  • Is there a standard format for documenting exceptions?

    While there's no strict standard, it's common to include the exception type and a brief description of the conditions under which it's raised. The following format is widely used: Raises: ExceptionType: Description of when the exception is raised.