Napoleon Sphinx Extension
repository·master·Indexed 41 days ago
https://github.com/sphinx-contrib/napoleonA Sphinx extension that acts as a pre-processor to convert Google and NumPy style Python docstrings into standard reStructuredText. It supports various section headers such as Args, Returns, and Raises, and integrates with PEP 484 type annotations to allow cleaner docstrings.
What's inside Napoleon
- Napoleon is a Sphinx extension that allows you to use Google and NumPy style docstrings in your Python documentation. It parses these styles and converts them into standard reStructuredText (reST) so that Sphinx can render them correctly. This enables you to write more legible, human-friendly docstrings without sacrificing the power of Sphinx's documentation engine.
How Napoleon works
masterNapoleon is a Sphinx extension that acts as a pre-processor. It parses
GoogleandNumPystyle docstrings and converts them into standard reStructuredText (rST) before Sphinx processes them.This conversion happens during the Sphinx documentation build process and does not modify your actual source code files. Napoleon can interpret docstrings for
modules,classes,attributes,methods,functions, andvariablesthat are discovered bySphinx autodoc.Google vs NumPy docstring styles
masterNapoleon supports two distinct docstring styles. You should choose one and remain consistent throughout your project; do not mix them.
- Google Style: Uses indentation to separate sections. It is more horizontally compact and is recommended for short, simple docstrings.
- NumPy Style: Uses underlines (dashes) to separate sections. It requires more vertical space and is better suited for long, in-depth documentation.
# Google style def func(arg1, arg2): """Summary line. Extended description of function. Args: arg1 (int): Description of arg1 arg2 (str): Description of arg2 Returns: bool: Description of return value """ return True # NumPy style def func(arg1, arg2): """Summary line. Extended description of function. Parameters ---------- arg1 : int Description of arg1 arg2 : str Description of arg2 Returns ------- bool Description of return value """ return TrueInstall and set up Napoleon
masterTo use Napoleon to parse Google or NumPy style docstrings in your Sphinx documentation, follow these steps:
Install the extension via pip:
pip install sphinxcontrib-napoleonEnable the extension in your Sphinx
conf.pyfile by adding'sphinxcontrib.napoleon'to theextensionslist.Use
sphinx-apidocto generate your API documentation files from your source code.
$ pip install sphinxcontrib-napoleon # In conf.py extensions = ['sphinxcontrib.napoleon'] $ sphinx-apidoc -f -o docs/source projectdirCompare Google vs NumPy docstring styles
masterNapoleon supports two primary docstring styles. You should choose one and remain consistent throughout your project; do not mix them.
Google Style
Uses indentation to separate sections. It is more horizontally compact and is often preferred for short, simple docstrings.
def func(arg1, arg2): """Summary line. Args: arg1 (int): Description of arg1 arg2 (str): Description of arg2 Returns: bool: Description of return value """ return TrueNumPy Style
Uses underlines to separate sections. It requires more vertical space but is often easier to read for long, in-depth docstrings.
def func(arg1, arg2): """Summary line. Parameters ---------- arg1 : int Description of arg1 arg2 : str Description of arg2 Returns ------- bool Description of return value """ return Truedef func(arg1, arg2): """Summary line. Args: arg1 (int): Description of arg1 arg2 (str): Description of arg2 Returns: bool: Description of return value """ return TrueInstall and enable Napoleon
masterTo use Napoleon in your Sphinx documentation project, install the extension via pip and add it to your
conf.pyextensions list.Note: If you are using Sphinx 1.3 or newer, Napoleon is already included in the Sphinx core as
sphinx.ext.napoleon. Usesphinxcontrib-napoleonspecifically for Sphinx versions <= 1.2.$ pip install sphinxcontrib-napoleon# conf.py # Add napoleon to the extensions list extensions = ['sphinxcontrib.napoleon']Example of NumPy style Python docstrings
masterThis example demonstrates how to write Python docstrings using the NumPy style, which Napoleon can then parse into legible Sphinx documentation. The NumPy style typically uses underlined headers for sections like
Parameters,Returns, andRaises.def function_with_numpy_style(arg1, arg2): """ A brief description of the function. Parameters ---------- arg1 : int Description of arg1. arg2 : str Description of arg2. Returns ------- bool Description of the return value. """ return TrueUsing Python type annotations with Napoleon
masterNapoleon supports using PEP 484 type annotations directly in your Python function signatures. When using type annotations, you can omit the types from the docstring sections (like
ArgsorReturns), which helps keep docstrings clean and allows IDEs/type-checkers to perform static analysis.Example (Google style with annotations):
def func(arg1: int, arg2: str) -> bool: """Summary line. Extended description of function. Args: arg1: Description of arg1 arg2: Description of arg2 Returns: Description of return value """ return Truedef func(arg1: int, arg2: str) -> bool: """Summary line. Args: arg1: Description of arg1 arg2: Description of arg2 Returns: Description of return value """ return TrueUse Google Style Python docstrings
masterNapoleon allows you to parse Python docstrings written in the Google style. This style uses indentation and specific section headers (like
Args:,Returns:, etc.) to define documentation components, making them more readable in source code than standard reStructuredText.To use this feature, ensure
sphinxcontrib.napoleonis added to your Sphinxconf.pyextensions list. You can then write docstrings following the Google format, and Napoleon will convert them into standard Sphinx documentation.def function_with_google_style(arg1, arg2): """Example Google Style docstring. Args: arg1 (int): The first argument. arg2 (str): The second argument. Returns: bool: True if successful, False otherwise. """ return TrueSupported docstring section headers
masterNapoleon recognizes several section headers within docstrings to convert them into structured documentation. The following headers are supported (including aliases):
Args,Arguments,Parameters,Other ParametersAttributesExample,ExamplesKeyword Args,Keyword ArgumentsMethodsNote,NotesReturn,ReturnsRaisesReferencesSee AlsoTodoWarning,Warnings,WarnsYield,YieldsOther(implied by general parsing)