Docstrings and Doctest#
Proper documentation is essential for code readability and maintainability. This guide explains how to write docstrings using the Google style and test them using doctest
.
1. Writing Docstrings in Google Style#
Google style docstrings are a clean and readable way to document your code. Here is a breakdown of the format:
Example Docstring#
def add_numbers(a: int, b: int) -> int:
"""
Add two numbers.
Args:
a (int): The first number.
b (int): The second number.
Returns:
int: The sum of the two numbers.
Examples:
>>> add_numbers(1, 2)
3
>>> add_numbers(-1, 5)
4
"""
return a + b
Key Sections of a Google Style Docstring#
- Summary: A short description of the function.
- Args: A list of all parameters with their types and descriptions.
- Returns: A description of the return value(s).
- Raises (optional): A list of exceptions the function may raise.
- Examples: Code examples demonstrating how to use the function.
Additional Notes#
- Use
"""
for multi-line docstrings. - Align descriptions for readability.
- Keep the summary concise and to the point.
2. Testing Docstrings with Doctest#
doctest
allows you to test the examples provided in your docstrings.
Running Doctests#
- Save your code with properly formatted docstrings.
- Use the following command to run
doctest
:
python -m doctest -v your_script.py
The -v
flag provides verbose output, showing which tests passed or failed.
Example Script with Doctest#
def multiply_numbers(a: int, b: int) -> int:
"""
Multiply two numbers.
Args:
a (int): The first number.
b (int): The second number.
Returns:
int: The product of the two numbers.
Examples:
>>> multiply_numbers(2, 3)
6
>>> multiply_numbers(-1, 5)
-5
>>> multiply_numbers(0, 10)
0
"""
return a * b
Common Commands#
- Run all tests: Run the script with doctest as shown above.
- Check specific failures: Look at the detailed output to understand why a test failed.
3. Debugging and Troubleshooting#
- Ensure your examples in the docstring match the actual output exactly (including whitespace).
- Use the
# doctest: +SKIP
directive to skip examples that should not be tested:
>>> some_function() # doctest: +SKIP
- If you encounter issues with floating-point numbers, use the
# doctest: +ELLIPSIS
directive to allow partial matching:
>>> divide_numbers(1, 3)
0.333... # doctest: +ELLIPSIS
4. Best Practices#
- Write examples for edge cases (e.g., zero, negative numbers, large inputs).
- Ensure all public functions, methods, and classes have docstrings.
- Regularly run
doctest
to ensure your examples remain up to date.
5. Additional Resources#
By following these guidelines, you can ensure your code is both well-documented and well-tested. Thank you for contributing!