Article 8: Documentation Best Practices
Introduction
Why Documentation Matters
Docstrings
PEP 257 Standards
def calculate_discount(price: Decimal, discount_rate: float) -> Decimal:
"""
Calculate the discounted price.
Applies a percentage discount to the original price and returns
the final amount after discount.
Args:
price: The original price before discount.
discount_rate: The discount as a decimal (e.g., 0.10 for 10%).
Returns:
The price after applying the discount.
Raises:
ValueError: If discount_rate is negative or greater than 1.
Examples:
>>> calculate_discount(Decimal("100.00"), 0.10)
Decimal('90.00')
>>> calculate_discount(Decimal("50.00"), 0.25)
Decimal('37.50')
"""
if not 0 <= discount_rate <= 1:
raise ValueError(f"discount_rate must be between 0 and 1, got {discount_rate}")
discount = price * Decimal(str(discount_rate))
return price - discountGoogle Style Docstrings
Class Docstrings
Module Docstrings
README Files
README Structure
Installation
Usage
Configuration
API Reference
Contributing
License
Installation
Requirements
Install via pip
Install via pipx (recommended)
Install from source
Usage
Creating Tasks
Listing Tasks
Managing Tasks
Configuration
Environment Variables
Variable
Description
Default
Development
Setup
Running Tests
Code Quality
Contributing
License
Acknowledgments
MkDocs with mkdocstrings
Inline API Documentation
Keeping Documentation Updated
Documentation as Code
Doctest for Verified Examples
Pre-commit Documentation Checks
Documentation Patterns
Architecture Decision Records (ADRs)
Changelog
Contributing Guide
Code Style
Testing
Documentation
Exercise 2: Create a README
Documentation Checklist
Key Takeaways
What's Next?
Last updated