• This repository has been archived on 26/Apr/2023
  • Stars
    star
    149
  • Rank 248,619 (Top 5 %)
  • Language
    Python
  • License
    Other
  • Created about 7 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Napoleon - Marching toward legible docstrings

Note

As of Sphinx 1.3, the napoleon extension will come packaged with Sphinx under sphinx.ext.napoleon. The sphinxcontrib.napoleon extension will continue to work with Sphinx <= 1.2.

Are you tired of writing docstrings that look like this:

:param path: The path of the file to wrap
:type path: str
:param field_storage: The :class:`FileStorage` instance to wrap
:type field_storage: FileStorage
:param temporary: Whether or not to delete the file when the File
   instance is destructed
:type temporary: bool
:returns: A buffered writable file descriptor
:rtype: BufferedFileStorage

ReStructuredText is great, but it creates visually dense, hard to read docstrings. Compare the jumble above to the same thing rewritten according to the Google Python Style Guide:

Args:
    path (str): The path of the file to wrap
    field_storage (FileStorage): The :class:`FileStorage` instance to wrap
    temporary (bool): Whether or not to delete the file when the File
       instance is destructed

Returns:
    BufferedFileStorage: A buffered writable file descriptor

Much more legible, no?

Napoleon is a Sphinx extension that enables Sphinx to parse both NumPy and Google style docstrings - the style recommended by Khan Academy.

Napoleon is a pre-processor that parses NumPy and Google style docstrings and converts them to reStructuredText before Sphinx attempts to parse them. This happens in an intermediate step while Sphinx is processing the documentation, so it doesn't modify any of the docstrings in your actual source code files.

Getting Started

  1. Install the napoleon extension:

    $ pip install sphinxcontrib-napoleon
    
  2. After setting up Sphinx to build your docs, enable napoleon in the Sphinx conf.py file:

    # conf.py
    
    # Add napoleon to the extensions list
    extensions = ['sphinxcontrib.napoleon']
    
  1. Use sphinx-apidoc to build your API documentation:

    $ sphinx-apidoc -f -o docs/source projectdir
    

Docstrings

Napoleon interprets every docstring that Sphinx autodoc can find, including docstrings on: modules, classes, attributes, methods, functions, and variables. Inside each docstring, specially formatted Sections are parsed and converted to reStructuredText.

All standard reStructuredText formatting still works as expected.

Docstring Sections

All of the following section headers are supported:

  • Args (alias of Parameters)
  • Arguments (alias of Parameters)
  • Attributes
  • Example
  • Examples
  • Keyword Args (alias of Keyword Arguments)
  • Keyword Arguments
  • Methods
  • Note
  • Notes
  • Other Parameters
  • Parameters
  • Return (alias of Returns)
  • Returns
  • Raises
  • References
  • See Also
  • Warning
  • Warnings (alias of Warning)
  • Warns
  • Yield (alias of Yields)
  • Yields

Google vs NumPy

Napoleon supports two styles of docstrings: Google and NumPy. The main difference between the two styles is that Google uses indention to separate sections, whereas NumPy uses underlines.

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 True

NumPy style tends to require more vertical space, whereas Google style tends to use more horizontal space. Google style tends to be easier to read for short and simple docstrings, whereas NumPy style tends be easier to read for long and in-depth docstrings.

The Khan Academy recommends using Google style.

The choice between styles is largely aesthetic, but the two styles should not be mixed. Choose one style for your project and be consistent with it.

For full documentation see https://sphinxcontrib-napoleon.readthedocs.io

More Repositories

1

confluencebuilder

Confluence Markup Builder Plugin for Sphinx
Python
312
star
2

sphinxcontrib-versioning

Sphinx extension that allows building versioned docs for self-hosting.
Python
125
star
3

openapi

OpenAPI (fka Swagger) spec renderer for Sphinx.
Python
109
star
4

plantuml

Python
108
star
5

spelling

A spelling checker for Sphinx-based documentation
Python
81
star
6

sphinx-lint

Check for stylistic and formal issues in .rst and .py files included in the documentation
Python
68
star
7

matlabdomain

A Sphinx extension for documenting Matlab code
Python
61
star
8

httpdomain

Documenting RESTful HTTP APIs
Python
59
star
9

apidoc

A Sphinx extension for running sphinx-apidoc on each build
Python
53
star
10

youtube

A Sphinx extension to embed videos from YouTube
Python
51
star
11

emojicodes

An extension to use emoji codes in your Sphinx documentation! 😍
Python
48
star
12

video

A sphinx plugin that enables embedding of HTML5 videos
Python
45
star
13

autoprogram

Documenting CLI programs
Python
44
star
14

datatemplates

Sphinx plugin for generating RST on the fly from data files and templates.
Python
44
star
15

fulltoc

Extension for Sphinx to make the sidebar show a full table of contents instead of just the local headings
Python
38
star
16

kroki

Embed PlantUML, DOT, etc. diagrams in your documentation using Kroki.
Python
36
star
17

redoc

ReDoc powered OpenAPI (fka Swagger) spec renderer for Sphinx.
Python
33
star
18

restbuilder

A Sphinx builder/writer to output reStructuredText (rst) files
Python
31
star
19

cookiecutter

A cookiecutter template for a Sphinx extension
Python
24
star
20

sphinx-pretty-searchresults

Sphinx: pretty search results is an extension for the Sphinx documentation tool. To display search results, Sphinx is fetching the source files of search hits and rendering excerpts in raw markup. This extension removes the markup from these source files (during build time), so the search results look decent.
Python
22
star
21

googleanalytics

Python
18
star
22

sphinxcontrib-towncrier

An RST directive for injecting a Towncrier-generated changelog draft containing fragments for the unreleased (next) project version. Demo: https://ansible-pylibssh.rtfd.io/changelog. Docs: https://sphinxcontrib-towncrier.rtfd.io
Python
17
star
23

tikz

A Sphinx extension to draw pictures with the TikZ/PGF LaTeX package
Python
16
star
24

doxylink

A Sphinx extension to link to external Doxygen API documentation.
Python
15
star
25

prefectviz

An extension to add Prefect flow visualizations into you Sphinx documentation.
Python
14
star
26

github-administration

This repository is used for managing the sphinx-contrib GitHub organization, including requesting importing new repositories or granting permissions.
13
star
27

svgbob

A Sphinx extension to convert ASCII diagrams to SVGs with Svgbob.
Python
12
star
28

jquery

Extension to include jQuery on newer Sphinx releases
Python
10
star
29

redirects

A Sphinx extension for generating JavaScript-driven redirects for moved pages
Python
9
star
30

typer

Auto generate docs for typer and click commands using typer's rich console formatting.
Python
9
star
31

sqltable

Python
7
star
32

scm

Sphinx SCM interface
Python
7
star
33

email

Python
6
star
34

sphinxcontrib-details-directive

Python
5
star
35

slide

A Sphinx extension to embed presentation slides on Slideshare and other sites
Python
5
star
36

prefecttask

An extension to autodoc Prefect Tasks
Python
5
star
37

icon

custom sphinx role to embed inline icons
Python
5
star
38

sadisplay

A Sphinx extension for displaying SqlAlchemy model
Python
5
star
39

prettyspecialmethods

Shows special/magic methods in sphinx docs as the python syntax that invokes them
Python
5
star
40

pecanwsme

Extension to Sphinx for documenting APIs built with Pecan and WSME
Python
5
star
41

ogp

Sphinx OGP feature for html builder
Python
4
star
42

examplecode

Python
4
star
43

proof

∎ A Sphinx extension to provide directives to typeset theorems, properties, proofs, etc.
Python
4
star
44

exceltable

Python
4
star
45

documentedlist

DocumentedList Sphinx Extension
Python
4
star
46

stuffcounter

A sphinx extension to illustrate the `add_enumerable_node()` method.
Python
4
star
47

cheader

Sphinx extension that implements a c:header directive.
Python
4
star
48

inlinesyntaxhighlight

Python
3
star
49

mscgen

Python
2
star
50

sqlalchemyviz

SqlAlchemyViz Sphinx Extension
Python
2
star
51

feed

Python
2
star
52

cmakedomain

Python
2
star
53

gherkin

Sphinx parser for Gherkin BDD
Shell
2
star
54

googlechart

Python
2
star
55

ansi

💖 Project is looking for extra maintainers, check issue tracker.
Python
2
star
56

requirements

A Sphinx extension that lets you declare requirements wherever you need (e.g. in test docstrings), mark statuses and collect them in a single list
Python
2
star
57

sdedit

A Sphinx extension for inserting sequence diagram by using Quick Sequence Diagram Editor
Python
2
star
58

sword

A Sphinx extension for inserting Bible verses from Sword
Python
2
star
59

autofile

A sphinx extension to generate module for many files from a glob expression. maintainers: @Freed-Wu
Python
2
star
60

packages

📦 Provide directives to display packages installed on the host machine.
Python
2
star
61

findanything

JavaScript
1
star
62

cheeseshop

Python
1
star
63

gnuplot

Python
1
star
64

autoanysrc

Python
1
star
65

gravizo

Embed PlantUML, DOT, etc. diagrams in your documentation using Gravizo.
Python
1
star
66

eval

Evaluate shell command or python code in sphinx and myst.
Python
1
star
67

lilypond

Python
1
star
68

clearquest

Python
1
star
69

whooshindex

A Sphinx whoosh indexer extension
Python
1
star
70

imagesvg

Python
1
star
71

aafig

Sphinx extension to add ASCII-art diagrams using aafigure
Python
1
star
72

zopeext

A Sphinx extension that provides an 'autointerface' directive for using Zope interfaces
Python
1
star
73

cacoo

Python
1
star
74

googlemaps

Python
1
star
75

autorun

Python
1
star
76

rubydomain

A Sphinx extension for Ruby support (Sphinx 1.0 needed)
Python
1
star
77

domaintools

Python
1
star
78

btn

A sphinx custom role to embed inline btn
Python
1
star
79

sphinxcontrib-skeleton

The automatic build of the @sphinx-contrib/copier-sphinxcontrib template
Python
1
star
80

copier-sphinxcontrib

The skeleton of a sphinxcontrib extention
Jinja
1
star
81

sphinxcontrib-termynal

Sphinx extension to include termynal windows in html documentation build.
Python
1
star