Sphinx Napoleon Extension
Sphinxcontrib-napoleon is a Sphinx extension that acts as a pre-processor, enabling Sphinx to parse and convert both NumPy and Google style docstrings into reStructuredText. This allows developers to write more legible and less verbose docstrings in their Python code. The standalone `sphinxcontrib-napoleon` package is currently at version 0.7, with its last release in 2018. While this package is maintained for compatibility with older Sphinx versions (<=1.2), its core functionality has been integrated directly into Sphinx as `sphinx.ext.napoleon` since Sphinx 1.3, making the `sphinx.ext.napoleon` path the modern standard for newer Sphinx projects.
Warnings
- breaking The `sphinxcontrib-napoleon` package is intended for Sphinx versions up to 1.2. For Sphinx 1.3 and newer, Napoleon's functionality is included directly within Sphinx as `sphinx.ext.napoleon`. While `sphinxcontrib-napoleon` might still function with newer Sphinx versions, using `sphinx.ext.napoleon` is the official and recommended path.
- breaking Version 0.7 of `sphinxcontrib-napoleon` (last updated 2018) is incompatible with Python 3.10+ due to a breaking change in Python 3.10 that moved `Callable` from the `collections` module to `collections.abc`. This results in an `ImportError`.
- gotcha Napoleon supports both Google and NumPy style docstrings, but mixing them within a single project is strongly discouraged. While both styles are parsed, inconsistent styling can lead to less readable documentation and potential formatting quirks.
- gotcha Older versions of Napoleon (prior to fixes in Sphinx) had issues correctly parsing `*args` and `**kwargs` in Google-style docstrings, leading to these parameters not being properly included in the generated documentation.
Install
-
pip install sphinxcontrib-napoleon
Imports
- napoleon
# conf.py extensions = ['sphinxcontrib.napoleon']
- napoleon
# conf.py extensions = ['sphinx.ext.napoleon']
Quickstart
# 1. Install sphinxcontrib-napoleon (if using Sphinx <= 1.2, or for older projects)
# pip install sphinxcontrib-napoleon
# 2. In your Sphinx project's conf.py file:
import os
import sys
sys.path.insert(0, os.path.abspath('.'))
# Add 'sphinx.ext.autodoc' to ensure docstrings are processed
extensions = [
'sphinx.ext.autodoc',
'sphinx.ext.napoleon' # Use 'sphinxcontrib.napoleon' for Sphinx <= 1.2
]
# Optional: Configure napoleon settings (defaults shown)
napoleon_google_docstring = True
napoleon_numpy_docstring = True
napoleon_include_init_with_doc = False
napoleon_include_private_with_doc = False
napoleon_include_special_with_doc = False
napoleon_use_admonition_for_examples = False
napoleon_use_admonition_for_notes = False
napoleon_use_admonition_for_references = False
napoleon_use_ivar = False
napoleon_use_param = True
napoleon_use_rtype = True
napoleon_use_keyword = True
napoleon_custom_sections = None
# 3. Generate API documentation (run from your project root or docs directory):
# sphinx-apidoc -f -o docs/source your_project_module_path
# make html (or sphinx-build -b html docs/source docs/build/html)