Sphinx Autodoc2

0.5.0 · active · verified Thu Apr 16

Sphinx Autodoc2 is a Sphinx extension designed to automatically generate API documentation for Python packages. Unlike the traditional `sphinx.ext.autodoc`, it uses static analysis of the source code, eliminating the need to install or import the package for documentation generation. This approach also correctly handles `TYPE_CHECKING` blocks and avoids import side-effects. It supports both reStructuredText (RST) and MyST (Markdown) docstrings and offers highly configurable analysis and output options. The analysis and rendering are decoupled, allowing it to be used outside of Sphinx via a command-line tool.

Common errors

Warnings

Install

Imports

Quickstart

To quickly set up `sphinx-autodoc2`: 1. **Project Structure**: Assume your `conf.py` is in `docs/` and your Python package is in `src/my_package/`. 2. **Install Dependencies**: `pip install sphinx sphinx-autodoc2 myst-parser` (if using Markdown). 3. **Configure `conf.py`**: Add `'autodoc2'` to your `extensions` list and set `autodoc2_packages` to the relative path of your package(s). You might also need to adjust `sys.path` so Sphinx can find your code. If using MyST, add `'myst_parser'` to extensions and optionally set `autodoc2_render_plugin = 'myst'`. 4. **Create API entry point**: In your `index.rst` or `index.md`, include `apidocs/index` in a `toctree` directive. `autodoc2` will generate files in the `apidocs` directory by default.

# docs/conf.py
import os
import sys

# Adjust this path to point to your Python package root relative to conf.py
# Example: if conf.py is in 'docs/' and your package is in 'src/my_package/'
sys.path.insert(0, os.path.abspath('../src'))

project = 'My Project'
copyright = '2026, Your Name'
author = 'Your Name'
release = '0.1.0'

extensions = [
    'sphinx.ext.autodoc', # Often used alongside for other extensions/features
    'myst_parser',        # Required if using Markdown for docstrings or source files
    'autodoc2',           # Enable the sphinx-autodoc2 extension
]

# --- sphinx-autodoc2 configuration ---
# Path(s) to the package(s) you want to document
autodoc2_packages = [
    '../src/my_package', # Replace 'my_package' with your actual package name
]

# Optional: Set the default docstring parser if using Markdown
# autodoc2_render_plugin = 'myst'

# Add your output directory to .gitignore, e.g., 'apidocs/'
# In your index.rst or index.md, add a toctree entry:
#
# .. toctree::
#    :maxdepth: 2
#
#    apidocs/index

view raw JSON →