Skip to content

Commit de15bec

Browse files
authored
Use autosummary for sphinx docs (#194)
2 parents b0dabb0 + 3a3c7f6 commit de15bec

File tree

12 files changed

+147
-21
lines changed

12 files changed

+147
-21
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ cov.xml
5555

5656
# Sphinx documentation
5757
docs/_build/
58+
docs/_api
5859

5960
# PyBuilder
6061
target/
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# 19. Use autosummary to create API docs
2+
3+
Date: 2024-09-10
4+
5+
## Status
6+
7+
Accepted
8+
9+
## Context
10+
11+
The current sphinx API documentation requires you to paste automodule directives in for each subpackage. This is counter intuitive and also places all the docs on one page. People saw a blank page and assumed API generation was broken.
12+
13+
Using autosummary would give a nicer summary, but requires pasting custom templates in which makes sphinx-autobuild keep reloading forever.
14+
15+
Using autodoc2 would allow md docstrings, but doesn't work with pydantic, and doesn't support google or numpy docstrings.
16+
17+
## Decision
18+
19+
Decided to use autosummary, with one page per subpackage. This means at most 2 reloads of sphinx-autobuild before it settles, which is reasonable.
20+
21+
## Consequences
22+
23+
Try to push a bit of the custom template (generating public_members in member order) up to sphinx.

template/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ cov.xml
5555

5656
# Sphinx documentation
5757
docs/_build/
58+
docs/_api
5859

5960
# PyBuilder
6061
target/

template/README.md.jinja

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ Or if it is a commandline tool then you might put some example commands here:
3131

3232
```
3333
python -m {{package_name}} --version
34-
```
35-
{% if sphinx %}
34+
```{% if sphinx %}
35+
3636
<!-- README only content. Anything below this line won't be included in index.md -->
3737

3838
See {{docs_url}} for more detailed documentation.{% endif %}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
"""Top level API.
2+
3+
.. data:: __version__
4+
:type: str
5+
6+
Version number as calculated by https://github.com/pypa/setuptools_scm
7+
"""
8+
19
from ._version import __version__
210

311
__all__ = ["__version__"]

template/src/{{ package_name }}/__main__.py.jinja

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Interface for ``python -m {{package_name}}``."""
2+
23
from argparse import ArgumentParser
34
from collections.abc import Sequence
45

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
:orphan:
2+
3+
..
4+
This page is not included in the TOC tree, but must exist so that the
5+
autosummary pages are generated for {{ package_name }} and all its
6+
subpackages
7+
8+
API
9+
===
10+
11+
.. autosummary::
12+
:toctree: _api
13+
:template: custom-module-template.rst
14+
:recursive:
15+
16+
{{ package_name }}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{{ ('``' + fullname + '``') | underline }}
2+
3+
{%- set filtered_members = [] %}
4+
{%- for item in members %}
5+
{%- if item in functions + classes + exceptions + attributes %}
6+
{% set _ = filtered_members.append(item) %}
7+
{%- endif %}
8+
{%- endfor %}
9+
10+
.. automodule:: {{ fullname }}
11+
:members:
12+
13+
{% block modules %}
14+
{% if modules %}
15+
.. rubric:: Submodules
16+
17+
.. autosummary::
18+
:toctree:
19+
:template: custom-module-template.rst
20+
:recursive:
21+
{% for item in modules %}
22+
{{ item }}
23+
{%- endfor %}
24+
{% endif %}
25+
{% endblock %}
26+
27+
{% block members %}
28+
{% if filtered_members %}
29+
.. rubric:: Members
30+
31+
.. autosummary::
32+
:nosignatures:
33+
{% for item in filtered_members %}
34+
{{ item }}
35+
{%- endfor %}
36+
{% endif %}
37+
{% endblock %}

template/{% if sphinx %}docs{% endif %}/conf.py.jinja

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ else:
3333
extensions = [
3434
# Use this for generating API docs
3535
"sphinx.ext.autodoc",
36+
# and making summary tables at the top of API docs
37+
"sphinx.ext.autosummary",
3638
# This can parse google style docstrings
3739
"sphinx.ext.napoleon",
3840
# For linking to external sphinx documentation
@@ -81,6 +83,12 @@ autodoc_member_order = "bysource"
8183
# Don't inherit docstrings from baseclasses
8284
autodoc_inherit_docstrings = False
8385

86+
# Document only what is in __all__
87+
autosummary_ignore_module_all = False
88+
89+
# Add any paths that contain templates here, relative to this directory.
90+
templates_path = ["_templates"]
91+
8492
# Output graphviz directive produced images in a scalable format
8593
graphviz_output_format = "svg"
8694

template/{% if sphinx %}docs{% endif %}/reference.md.jinja

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Technical reference material including APIs and release notes.
66
:maxdepth: 1
77
:glob:
88

9-
reference/*
9+
API <_api/{{ package_name }}>
1010
genindex
1111
Release Notes <{{ repo_url }}/releases>
1212
```

0 commit comments

Comments
 (0)