mirror of
https://github.com/sanjeed5/awesome-cursor-rules-mdc.git
synced 2026-11-30 23:04:59 +08:00
Regenerate all MDC rules with fresh Exa data and December 2025 best practices
- Refreshed all 241 library rules with current best practices - Updated Exa search results for all libraries with 2025 content - Fixed --regenerate-all flag logic to properly process all libraries - Removed duplicate astro entry in rules.json (merged tags) - Added missing docker.mdc and vim.mdc files - All rules now include modern patterns and anti-patterns - Updated with framework-specific recommendations for 2025 Technical improvements: - Fixed retry_failed_only logic in generate_mdc_files.py - Improved progress tracking for regeneration scenarios - Ensured proper handling of --refresh-exa flag
This commit is contained in:
+199
-221
@@ -1,243 +1,221 @@
|
||||
---
|
||||
description: Comprehensive best practices for using pdoc to generate and maintain Python project documentation. Covers code structure, performance, security, testing, and tooling to ensure high-quality documentation and maintainable projects.
|
||||
globs: *.py
|
||||
description: This guide provides definitive rules for writing Python docstrings and type hints to maximize clarity and utility with pdoc, ensuring accurate and maintainable API documentation.
|
||||
globs: **/*.py
|
||||
---
|
||||
# pdoc Best Practices: A Comprehensive Guide
|
||||
# pdoc Best Practices
|
||||
|
||||
This document outlines best practices for using `pdoc` to generate API documentation for Python projects. It covers code organization, performance considerations, security, testing, and common pitfalls, all tailored for effective documentation generation and maintainability.
|
||||
`pdoc` is our definitive tool for generating API documentation from Python docstrings and type annotations. This guide outlines the mandatory practices to ensure our generated documentation is consistent, accurate, and immediately useful.
|
||||
|
||||
## Library Information:
|
||||
## 1. Docstrings: The Foundation (Google Style)
|
||||
|
||||
- Name: pdoc
|
||||
- Tags: development, documentation, python, auto-generation
|
||||
Always use **Google Style docstrings** for all public modules, classes, functions, and methods. This provides a clear, structured format that `pdoc` parses effectively.
|
||||
|
||||
## 1. Code Organization and Structure
|
||||
### Mandatory Docstring Sections:
|
||||
* **Summary Line**: A concise, one-line summary of the object's purpose.
|
||||
* **Detailed Description**: Elaborate on behavior, context, and important considerations.
|
||||
* **`Args`**: List each parameter with its type and description.
|
||||
* **`Returns`**: Describe the return value and its type.
|
||||
* **`Raises`**: Document any exceptions that can be raised.
|
||||
* **`Example`**: Provide a short, copy-pasteable usage example.
|
||||
|
||||
### 1.1 Directory Structure Best Practices
|
||||
❌ **BAD: Incomplete and inconsistent docstring**
|
||||
```python
|
||||
def calculate_discount(price, percentage):
|
||||
"""Applies a discount to a price.
|
||||
Returns the discounted price.
|
||||
"""
|
||||
return price * (1 - percentage / 100)
|
||||
```
|
||||
|
||||
- **Clear Module Grouping:** Organize your code into logical modules and sub-packages. This makes it easier for `pdoc` to generate a navigable API.
|
||||
- **`__init__.py` Files:** Ensure that each package directory contains an `__init__.py` file. This file can be empty, or it can contain initialization code and, importantly, the package-level docstring which `pdoc` will use.
|
||||
- **Separate Concerns:** Isolate different functionalities into separate modules. For example, separate data models, business logic, and utility functions into distinct files.
|
||||
✅ **GOOD: Comprehensive Google Style docstring**
|
||||
```python
|
||||
def calculate_discount(price: float, percentage: float) -> float:
|
||||
"""Applies a percentage-based discount to a given price.
|
||||
|
||||
Example:
|
||||
This function calculates the final price after applying a specified
|
||||
discount percentage. The percentage should be a value between 0 and 100.
|
||||
|
||||
Args:
|
||||
price (float): The original price of the item.
|
||||
percentage (float): The discount percentage to apply (0-100).
|
||||
|
||||
my_project/
|
||||
├── my_package/
|
||||
│ ├── __init__.py # Package-level docstring here
|
||||
│ ├── module_a.py # Contains functions and classes
|
||||
│ ├── module_b.py # Contains more functions and classes
|
||||
│ └── subpackage/
|
||||
│ ├── __init__.py # Subpackage-level docstring here
|
||||
│ └── module_c.py # Contains functions and classes
|
||||
└── setup.py # Installation script
|
||||
Returns:
|
||||
float: The discounted price.
|
||||
|
||||
Raises:
|
||||
ValueError: If the percentage is not within the 0-100 range.
|
||||
|
||||
### 1.2 File Naming Conventions
|
||||
Example:
|
||||
>>> calculate_discount(100.0, 10.0)
|
||||
90.0
|
||||
>>> calculate_discount(50.0, 0.0)
|
||||
50.0
|
||||
"""
|
||||
if not (0 <= percentage <= 100):
|
||||
raise ValueError("Discount percentage must be between 0 and 100.")
|
||||
return price * (1 - percentage / 100)
|
||||
```
|
||||
|
||||
- **Descriptive Names:** Use descriptive names for modules and packages. For example, `database_utils.py` is much better than `db.py`.
|
||||
- **Lowercase with Underscores:** Follow the standard Python convention of using lowercase letters with underscores for module names (e.g., `my_module.py`).
|
||||
## 2. Type Hints: pdoc's Superpower
|
||||
|
||||
### 1.3 Module Organization
|
||||
**Always use explicit type hints** for all function arguments, return values, and class attributes in public APIs. `pdoc` leverages type hints for accurate cross-referencing and improved readability.
|
||||
|
||||
- **Top-Level Docstrings:** Every module should have a top-level docstring that describes its purpose and lists the main classes, functions, and exceptions it defines.
|
||||
- **`__all__` Variable:** Use the `__all__` variable to explicitly define the public API of a module. This tells `pdoc` (and other tools) which names should be included in the generated documentation.
|
||||
❌ **BAD: Missing type hints**
|
||||
```python
|
||||
def process_data(data, config):
|
||||
"""Processes raw data using provided configuration."""
|
||||
# ... logic ...
|
||||
return processed_data
|
||||
```
|
||||
|
||||
Example:
|
||||
✅ **GOOD: Complete type hints**
|
||||
```python
|
||||
from typing import Any, Dict
|
||||
|
||||
python
|
||||
def process_data(data: list[Any], config: Dict[str, Any]) -> Dict[str, Any]:
|
||||
"""Processes raw data using provided configuration.
|
||||
|
||||
Args:
|
||||
data (list[Any]): A list of raw data items to process.
|
||||
config (Dict[str, Any]): Configuration parameters for processing.
|
||||
|
||||
Returns:
|
||||
Dict[str, Any]: The processed data as a dictionary.
|
||||
"""
|
||||
# ... logic ...
|
||||
return {"result": "processed"}
|
||||
```
|
||||
|
||||
## 3. Module and Package Docstrings
|
||||
|
||||
Every Python module (`.py` file) and package (`__init__.py`) must start with a docstring providing a high-level overview.
|
||||
|
||||
* **Module Docstring**: Explain the module's primary purpose and key contents.
|
||||
* **Package Docstring (`__init__.py`)**: Describe the package's overall functionality and main sub-modules.
|
||||
|
||||
```python
|
||||
# my_module.py
|
||||
"""This module provides utility functions for data processing.
|
||||
"""This module provides utility functions for data manipulation.
|
||||
|
||||
Classes:
|
||||
DataProcessor: Processes data.
|
||||
|
||||
Functions:
|
||||
clean_data: Cleans the data.
|
||||
It includes functions for filtering, sorting, and transforming lists of data.
|
||||
"""
|
||||
|
||||
__all__ = ['DataProcessor', 'clean_data']
|
||||
|
||||
class DataProcessor:
|
||||
"""Processes data from various sources."""
|
||||
...
|
||||
|
||||
def clean_data(data):
|
||||
"""Cleans the input data."""
|
||||
...
|
||||
|
||||
|
||||
### 1.4 Component Architecture
|
||||
|
||||
- **Loose Coupling:** Design your components to be loosely coupled. This means that components should have minimal dependencies on each other.
|
||||
- **Clear Interfaces:** Define clear interfaces for your components. This makes it easier to test and document your code.
|
||||
- **Abstraction:** Use abstraction to hide the implementation details of your components. This makes your code more flexible and easier to maintain.
|
||||
|
||||
### 1.5 Code Splitting
|
||||
|
||||
- **Divide and Conquer:** Split large modules into smaller, more manageable files. This makes your code easier to understand and maintain.
|
||||
- **By Functionality:** Split code based on functionality. For example, if you have a module that contains both database access code and business logic, split it into two separate modules.
|
||||
|
||||
## 2. Common Patterns and Anti-patterns
|
||||
|
||||
### 2.1 Design Patterns
|
||||
|
||||
- **Factory Pattern:** Use the factory pattern to create objects. This makes it easier to switch between different implementations of a class.
|
||||
- **Strategy Pattern:** Use the strategy pattern to encapsulate different algorithms. This makes it easier to switch between different algorithms at runtime.
|
||||
- **Decorator Pattern:** Use the decorator pattern to add functionality to objects dynamically.
|
||||
|
||||
### 2.2 Recommended Approaches
|
||||
|
||||
- **Use Docstrings:** All modules, classes, functions, and methods must include docstrings. Docstrings are used by `pdoc` to create the API documentation.
|
||||
- **Follow PEP 257:** Adhere to PEP 257 for docstring conventions. This ensures consistency and readability.
|
||||
- **Choose a Style:** Consistently use a docstring style (Google, NumPy, reStructuredText) throughout your project.
|
||||
- **Include Examples:** Provide simple, executable examples in your docstrings. This helps users understand how to use your code. These examples can be embedded directly within docstrings using the `doctest` module.
|
||||
|
||||
### 2.3 Anti-patterns
|
||||
|
||||
- **Missing Docstrings:** Failing to document modules, classes, functions, or methods.
|
||||
- **Incomplete Docstrings:** Providing docstrings that are too brief or lack essential information.
|
||||
- **Inconsistent Style:** Mixing different docstring styles within the same project.
|
||||
- **Redundant Documentation:** Repeating information that is already clear from the code itself. Focus on the *why* rather than the *how*.
|
||||
- **Ignoring `__all__`:** Not defining `__all__` or defining it incorrectly can lead to undocumented or wrongly documented API surfaces.
|
||||
|
||||
### 2.4 State Management
|
||||
|
||||
- **Minimize Global State:** Avoid using global variables as much as possible. If you must use global variables, document them clearly.
|
||||
- **Use Classes to Encapsulate State:** Use classes to encapsulate state. This makes it easier to manage and reason about your code.
|
||||
|
||||
### 2.5 Error Handling
|
||||
|
||||
- **Raise Exceptions:** Raise exceptions when errors occur. This makes it easier to handle errors gracefully.
|
||||
- **Use Try-Except Blocks:** Use try-except blocks to catch exceptions. This prevents your program from crashing when errors occur.
|
||||
- **Document Exceptions:** Document the exceptions that your functions and methods can raise.
|
||||
|
||||
## 3. Performance Considerations
|
||||
|
||||
### 3.1 Optimization Techniques
|
||||
|
||||
- **Profiling:** Use a profiler to identify performance bottlenecks in your code.
|
||||
- **Caching:** Use caching to store frequently accessed data. Consider using `functools.lru_cache` for memoization.
|
||||
- **Vectorization:** Use vectorized operations when working with numerical data. This is often faster than using loops.
|
||||
|
||||
### 3.2 Memory Management
|
||||
|
||||
- **Avoid Memory Leaks:** Be careful to avoid memory leaks. Free up memory when it is no longer needed.
|
||||
- **Use Generators:** Use generators to process large amounts of data. This can reduce memory usage.
|
||||
- **Use Data Structures Efficiently:** Use the right data structures for the job. For example, use sets for membership testing and dictionaries for lookups.
|
||||
|
||||
### 3.3 Rendering Optimization
|
||||
|
||||
- Not generally applicable to `pdoc` itself, as it mainly generates static HTML. However, the design of your code *can* affect how quickly `pdoc` can process it. Well-structured, easily-introspected code will be processed faster.
|
||||
|
||||
### 3.4 Bundle Size
|
||||
|
||||
- Not applicable to `pdoc` itself, as it is a documentation generator and not a web application framework.
|
||||
|
||||
### 3.5 Lazy Loading
|
||||
|
||||
- Not directly applicable to `pdoc`, but consider lazy loading modules within your own code to improve startup time if you have a large project that `pdoc` needs to process. This is done using `importlib.import_module`.
|
||||
|
||||
## 4. Security Best Practices
|
||||
|
||||
### 4.1 Common Vulnerabilities
|
||||
|
||||
- **Not Directly Applicable:** `pdoc` generates static documentation and does not execute arbitrary user-supplied code. Therefore, it is less vulnerable than many other types of applications. However, be mindful of the dependencies you use and keep them up to date.
|
||||
|
||||
### 4.2 Input Validation
|
||||
|
||||
- **Not Directly Applicable:** `pdoc` does not take direct user input.
|
||||
|
||||
### 4.3 Authentication and Authorization
|
||||
|
||||
- **Not Applicable:** `pdoc` is a documentation generation tool and does not require authentication or authorization.
|
||||
|
||||
### 4.4 Data Protection
|
||||
|
||||
- **Not Applicable:** `pdoc` does not handle sensitive data.
|
||||
|
||||
### 4.5 Secure API Communication
|
||||
|
||||
- **Not Applicable:** `pdoc` does not communicate with external APIs.
|
||||
|
||||
## 5. Testing Approaches
|
||||
|
||||
### 5.1 Unit Testing
|
||||
|
||||
- **Test Docstrings:** Use `doctest` to test examples embedded in your docstrings. This ensures that your examples are up-to-date and correct. `pdoc` renders these examples, making them executable directly from the documentation.
|
||||
- **Focus on Public API:** Write unit tests for the public API of your modules, classes, and functions.
|
||||
|
||||
### 5.2 Integration Testing
|
||||
|
||||
- **Test Interactions:** Test the interactions between different components of your system.
|
||||
|
||||
### 5.3 End-to-End Testing
|
||||
|
||||
- **Test Complete Workflows:** Test complete workflows to ensure that your system is working correctly.
|
||||
|
||||
### 5.4 Test Organization
|
||||
|
||||
- **Separate Test Files:** Keep your tests in separate files from your code. This makes it easier to find and run your tests.
|
||||
- **Organize by Module:** Organize your tests by module. This makes it easier to find the tests for a specific module.
|
||||
|
||||
### 5.5 Mocking and Stubbing
|
||||
|
||||
- **Use Mock Objects:** Use mock objects to isolate the code that you are testing from its dependencies. This makes it easier to test your code in isolation.
|
||||
|
||||
## 6. Common Pitfalls and Gotchas
|
||||
|
||||
### 6.1 Frequent Mistakes
|
||||
|
||||
- **Incorrect Docstring Formatting:** Using the wrong docstring format or inconsistent formatting.
|
||||
- **Omitting Parameters:** Failing to document all parameters of a function or method.
|
||||
- **Not Updating Documentation:** Failing to update the documentation when the code changes.
|
||||
- **Circular Imports:** Circular imports can confuse `pdoc`. Refactor your code to avoid them.
|
||||
|
||||
### 6.2 Edge Cases
|
||||
|
||||
- **Dynamic Code Generation:** If your code heavily uses dynamic code generation (`eval`, `exec`), `pdoc` may have difficulty introspecting it.
|
||||
- **Metaclasses:** Complex metaclasses can also make it harder for `pdoc` to generate accurate documentation.
|
||||
|
||||
### 6.3 Version-Specific Issues
|
||||
|
||||
- **Check Compatibility:** Always check the compatibility of `pdoc` with your Python version. Refer to the official `pdoc` documentation for version-specific information.
|
||||
|
||||
### 6.4 Compatibility Concerns
|
||||
|
||||
- **Third-Party Libraries:** Ensure that `pdoc` is compatible with any third-party libraries that you are using.
|
||||
|
||||
### 6.5 Debugging Strategies
|
||||
|
||||
- **Verbose Mode:** Run `pdoc` in verbose mode to see more detailed output. This can help you identify errors.
|
||||
- **Check for Errors:** Check the output of `pdoc` for any errors or warnings.
|
||||
- **Inspect Generated HTML:** Inspect the generated HTML to see if the documentation looks correct.
|
||||
|
||||
## 7. Tooling and Environment
|
||||
|
||||
### 7.1 Recommended Tools
|
||||
|
||||
- **IDE:** Use a good IDE (e.g., VS Code, PyCharm) with Python support.
|
||||
- **Linting:** Use a linter (e.g., pylint, flake8) to catch errors and enforce coding standards.
|
||||
- **Formatting:** Use a code formatter (e.g., black, autopep8) to format your code.
|
||||
|
||||
### 7.2 Build Configuration
|
||||
|
||||
- **`setup.py` or `pyproject.toml`:** Use a `setup.py` file or `pyproject.toml` to define your project's dependencies and metadata.
|
||||
- **Include Documentation Files:** Make sure to include documentation files in your build configuration.
|
||||
|
||||
### 7.3 Linting and Formatting
|
||||
|
||||
- **Consistent Style:** Enforce a consistent coding style using a linter and code formatter.
|
||||
- **Follow PEP 8:** Follow the PEP 8 style guide for Python code.
|
||||
- **Use Docstring Checks:** Configure your linter to check for missing or incomplete docstrings.
|
||||
|
||||
### 7.4 Deployment Best Practices
|
||||
|
||||
- **Generate Documentation Automatically:** Automate the generation of documentation as part of your build process.
|
||||
- **Host Documentation:** Host the generated documentation on a web server or documentation hosting service (e.g., Read the Docs).
|
||||
|
||||
### 7.5 CI/CD Integration
|
||||
|
||||
- **Integrate with CI/CD:** Integrate `pdoc` with your CI/CD pipeline to automatically generate and deploy documentation on every commit.
|
||||
|
||||
By following these best practices, you can ensure that your Python projects have high-quality, well-maintained documentation that is easy for users to understand and use.
|
||||
def filter_list(data: list[Any], condition: Callable[[Any], bool]) -> list[Any]:
|
||||
# ...
|
||||
pass
|
||||
```
|
||||
|
||||
## 4. `__all__` for Explicit API Exposure
|
||||
|
||||
Define `__all__` in `__init__.py` files and modules to explicitly declare the public API. This guides `pdoc` on what to include in the generated documentation and prevents accidental exposure of internal components.
|
||||
|
||||
```python
|
||||
# my_package/__init__.py
|
||||
"""A package for advanced data processing utilities."""
|
||||
|
||||
__all__ = ["data_processor", "config_manager"] # Only these modules are public
|
||||
```
|
||||
|
||||
```python
|
||||
# my_package/data_processor.py
|
||||
"""Core data processing functions."""
|
||||
|
||||
__all__ = ["process_records", "clean_entries"] # Only these functions are public
|
||||
|
||||
def process_records(...): ...
|
||||
def clean_entries(...): ...
|
||||
def _internal_helper(...): ... # Not in __all__, not part of public API
|
||||
```
|
||||
|
||||
## 5. Inline Comments vs. Docstrings
|
||||
|
||||
**Use inline comments for *why*, docstrings for *what***.
|
||||
* **Docstrings**: Describe the *public interface*—what a function/class does, its inputs, outputs, and how to use it.
|
||||
* **Inline Comments**: Explain *complex implementation details* or *non-obvious logic* within a function body.
|
||||
|
||||
❌ **BAD: Explaining obvious code with comments, or using comments for API details**
|
||||
```python
|
||||
def add(a: int, b: int) -> int:
|
||||
# This function adds two numbers
|
||||
# a: first number
|
||||
# b: second number
|
||||
# returns: sum of a and b
|
||||
return a + b # Return the sum
|
||||
```
|
||||
|
||||
✅ **GOOD: Docstring for API, comments for internal logic**
|
||||
```python
|
||||
def process_financial_transaction(amount: float, user_id: str) -> bool:
|
||||
"""Processes a financial transaction for a user.
|
||||
|
||||
Applies specific business rules for fraud detection before committing.
|
||||
|
||||
Args:
|
||||
amount (float): The transaction amount.
|
||||
user_id (str): The ID of the user initiating the transaction.
|
||||
|
||||
Returns:
|
||||
bool: True if the transaction was successful, False otherwise.
|
||||
"""
|
||||
# Check for known fraud patterns before proceeding.
|
||||
# This specific rule (amount > 1000 for new users) is mandated by compliance.
|
||||
if amount > 1000 and _is_new_user(user_id):
|
||||
_log_fraud_attempt(user_id, amount)
|
||||
return False
|
||||
|
||||
# Proceed with transaction
|
||||
return _commit_transaction(amount, user_id)
|
||||
```
|
||||
|
||||
## 6. Automate Documentation Generation (CI/CD)
|
||||
|
||||
Integrate `pdoc` into your CI/CD pipeline. Documentation must be generated and deployed automatically on every successful merge to `main`. This ensures docs are always up-to-date.
|
||||
|
||||
```yaml
|
||||
# .github/workflows/docs.yml (GitHub Actions example)
|
||||
name: Generate and Deploy Docs
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
|
||||
jobs:
|
||||
build-and-deploy-docs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Set up Python
|
||||
uses: actions/setup-python@v5
|
||||
with:
|
||||
python-version: '3.11'
|
||||
- name: Install dependencies
|
||||
run: |
|
||||
pip install pdoc
|
||||
- name: Generate pdoc documentation
|
||||
run: |
|
||||
pdoc --html --output-dir docs my_package
|
||||
- name: Deploy to GitHub Pages
|
||||
uses: peaceiris/actions-gh-pages@v3
|
||||
with:
|
||||
github_token: ${{ secrets.GITHUB_TOKEN }}
|
||||
publish_dir: ./docs
|
||||
```
|
||||
|
||||
## 7. Test Docstring Examples with `doctest`
|
||||
|
||||
Ensure that `Example` sections in your docstrings remain accurate and executable by running `doctest` as part of your test suite. This prevents documentation drift.
|
||||
|
||||
```python
|
||||
# tests/test_my_module.py
|
||||
import doctest
|
||||
import unittest
|
||||
from my_package import my_module # Assuming my_module has doctests
|
||||
|
||||
def load_tests(loader, tests, ignore):
|
||||
tests.addTests(doctest.DocTestSuite(my_module))
|
||||
return tests
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
```
|
||||
Reference in New Issue
Block a user