| name | sphinx |
| description | [Applies to: **/*.py] This rule file provides opinionated, practical guidelines for setting up, writing, and maintaining Python project documentation using Sphinx, emphasizing modern best practices, clear structure, and automated validation. |
| source | cursor_mdc |
Sphinx Best Practices
Sphinx is the definitive tool for Python project documentation. This guide outlines the essential workflow and best practices for creating intelligent, beautiful, and maintainable docs. Always prioritize consistency within your project over strict adherence to external guides, but default to these modern standards.
1. Project Setup and Structure
Establish a clean, predictable documentation structure from the start.
1.1. Use a Virtual Environment
Always isolate your documentation build dependencies. This prevents conflicts and ensures reproducible builds.
pip install sphinx
python -m venv .venv/docs
source .venv/docs/bin/activate
pip install sphinx sphinx-autobuild myst-parser sphinx-rtd-theme sphinx.ext.napoleon
1.2. Standard Directory Layout
Place all documentation source files in a dedicated docs/ directory at your project root.
my_project/
├── .venv/
├── my_package/
│ └── __init__.py
│ └── module.py
├── docs/
│ ├── conf.py
│ ├── index.md # Or index.rst
│ ├── tutorial/ # For manual narrative content
│ │ └── getting_started.md
│ ├── api/ # For auto-generated API docs (managed by apidoc)
│ │ └── my_package.rst
│ ├── _static/ # Custom static files (images, CSS)
│ ├── _templates/ # Custom Jinja2 templates
│ └── Makefile # Or make.bat for Windows
├── README.md
├── CHANGELOG.md
├── LICENSE
└── pyproject.toml
1.3. Initialize with sphinx-quickstart
Use the quickstart tool once to scaffold your docs/ directory. Accept the defaults, then customize.
cd docs
sphinx-quickstart
1.4. Prefer MyST Markdown for New Content
For narrative documentation, MyST Markdown (.md) is more approachable than reStructuredText (.rst) while retaining Sphinx's powerful features. Use it for all new manual content.
docs/conf.py:
extensions = [
'myst_parser',
]
source_suffix = {
'.rst': 'restructuredtext',
'.md': 'markdown',
}
2. Docstring Conventions and API Generation
Generate API documentation directly from your Python code's docstrings.
2.1. Adopt Google-Style Docstrings with Type Hints
Write docstrings in Google style for clarity and consistency. Always include type hints in your function/method signatures. The sphinx.ext.napoleon extension will parse these automatically.
docs/conf.py:
extensions = [
'sphinx.ext.napoleon',
]
napoleon_google_docstring = True
napoleon_numpy_docstring = False
napoleon_use_param = True
napoleon_use_rtype = True
my_package/module.py:
def calculate_sum(a, b):
"""Adds two numbers."""
return a + b
def calculate_sum(a: int, b: int) -> int:
"""Calculate the sum of two integers.
Args:
a: The first integer.
b: The second integer.
Returns:
The sum of `a` and `b`.
"""
return a + b
2.2. Automate API Documentation with autodoc and autosummary
Use sphinx.ext.autodoc to pull docstrings and sphinx.ext.autosummary to generate clean API tables.
docs/conf.py:
extensions = [
'sphinx.ext.autodoc',
'sphinx.ext.autosummary',
]
autodoc_member_order = 'bysource'
autodoc_typehints = 'signature'
autosummary_generate = True
docs/api/index.rst (or .md with MyST directives):
.. automodule:: my_package.module
:members:
.. autosummary::
:toctree: _autosummary
:template: module.rst
my_package.module.calculate_sum
my_package.module.MyClass
2.3. Manage sphinx-apidoc Carefully
Run sphinx-apidoc once to scaffold API .rst files. Never let it overwrite your manual documentation.
sphinx-apidoc -o api/ ../my_package
sphinx-apidoc -o api/ ../my_package -f -e -M
3. Content Management and Cross-Referencing
Structure your documentation logically and enable seamless navigation.
3.1. Master toctree for Structure
Organize your documentation using the toctree directive in your main index.md (or index.rst).
docs/index.md:
# My Project Documentation
Welcome to the documentation for My Project!
```{toctree}
:maxdepth: 2
:caption: Contents:
tutorial/getting_started
api/index
3.2. Link External Projects with intersphinx
Easily link to documentation of other Python projects (e.g., Python, NumPy, Django).
docs/conf.py:
extensions = [
'sphinx.ext.intersphinx',
]
intersphinx_mapping = {
'python': ('https://docs.python.org/3/', None),
'numpy': ('https://numpy.org/doc/stable/', None),
'requests': ('https://requests.readthedocs.io/en/latest/', None),
}
docs/tutorial/getting_started.md:
# Getting Started
This project uses [Python's built-in `list` type](:py:class:`python:list`).
For network requests, we rely on the [Requests library](:py:mod:`requests:requests`).
4. Building and Testing Documentation
Integrate documentation builds into your development and CI workflow.
4.1. Use sphinx-autobuild for Live Previews
During development, sphinx-autobuild provides live-reloading previews, accelerating your writing process.
sphinx-autobuild . _build/html --port 8000
4.2. Integrate Docs Build into CI/CD
Ensure your documentation builds successfully and is up-to-date by including a build step in your CI pipeline. Treat documentation errors as build failures.
.github/workflows/ci.yml (Example for GitHub Actions):
name: CI
on: [push, pull_request]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.x'
- name: Install dependencies
run: |
python -m venv .venv/docs
source .venv/docs/bin/activate
pip install -e .[docs] # Install project and docs dependencies
- name: Build Sphinx documentation
run: |
source .venv/docs/bin/activate
cd docs
make html # Or sphinx-build -W -b html . _build/html
4.3. Enforce Docstring Style and Quality
Use linters like pydocstyle to enforce PEP 257 docstring conventions and flake8 for general PEP 8 compliance.
pip install flake8 pydocstyle
flake8 my_package/
pydocstyle my_package/
4.4. Test Docstring Examples with doctest
Embed simple, runnable examples in your docstrings and use sphinx.ext.doctest to verify them.
docs/conf.py:
extensions = [
'sphinx.ext.doctest',
]
my_package/module.py:
def multiply(a: int, b: int) -> int:
"""Multiply two integers.
>>> multiply(2, 3)
6
>>> multiply(0, 5)
0
"""
return a * b
5. Packaging and Distribution
Ensure your documentation is easily accessible and distributable.
5.1. Host on Read the Docs
Read the Docs provides free, automated hosting for Sphinx documentation, integrating directly with your version control system. Configure it to build on every commit.
5.2. Include Docs in Source Distribution
If your documentation is essential for offline use or specific deployment scenarios, ensure it's included in your project's source distribution.
pyproject.toml (or MANIFEST.in for older projects):
[tool.setuptools.package-data]
"my_package" = ["py.typed"]
"my_project" = ["docs/*"]