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.
pip install sphinx-autodoc2Verified import paths — ran on the pinned version, not inferred.
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.
Review `sphinx-autodoc2` documentation, especially the 'Differences from `sphinx.ext.autodoc`' section. Configure `autodoc2_packages` with file system paths, not importable module names.
This typically means Sphinx cannot resolve cross-references. Ensure external packages are correctly added to your `intersphinx_mapping` in `conf.py`. For renamed or complex types, consider using `autodoc2_replace_annotations` or `autodoc2_replace_bases` options to map them correctly.
Ensure `autodoc2_packages` in your `conf.py` is set to a list containing at least one path to a Python package or module. E.g., `autodoc2_packages = ['../src/my_package']`.
Check for updates to `sphinx-autodoc2`. If on 0.5.0, try using a list of dictionaries if a fix is not available, or downgrade if possible. Refer to the official GitHub issues for the latest status and workaround.