Skip to content

Commit 1791c3b

Browse files
authored
Use sphinx-accessors-autosummary (#4323)
* silence flake8 and mypy * use sphinx_autosummary_accessors instead of the custom code in conf.py * mention the sphinx extension in the internals documentation
1 parent df7b2ea commit 1791c3b

File tree

3 files changed

+20
-126
lines changed

3 files changed

+20
-126
lines changed

ci/requirements/doc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,5 @@ dependencies:
2626
- sphinx=3.1
2727
- sphinx_rtd_theme>=0.4
2828
- zarr>=2.4
29+
- pip:
30+
- sphinx-autosummary-accessors

doc/conf.py

Lines changed: 13 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,7 @@
2020
import sys
2121
from contextlib import suppress
2222

23-
# --------- autosummary templates ------------------
24-
# TODO: eventually replace this with a sphinx.ext.auto_accessor module
25-
import sphinx
26-
from sphinx.ext.autodoc import AttributeDocumenter, Documenter, MethodDocumenter
27-
from sphinx.util import rpartition
23+
import sphinx_autosummary_accessors
2824

2925
# make sure the source version is preferred (#3567)
3026
root = pathlib.Path(__file__).absolute().parent.parent
@@ -53,14 +49,14 @@
5349
matplotlib.use("Agg")
5450

5551
try:
56-
import rasterio
52+
import rasterio # noqa: F401
5753
except ImportError:
5854
allowed_failures.update(
5955
["gallery/plot_rasterio_rgb.py", "gallery/plot_rasterio.py"]
6056
)
6157

6258
try:
63-
import cartopy
59+
import cartopy # noqa: F401
6460
except ImportError:
6561
allowed_failures.update(
6662
[
@@ -88,6 +84,7 @@
8884
"IPython.sphinxext.ipython_directive",
8985
"IPython.sphinxext.ipython_console_highlighting",
9086
"nbsphinx",
87+
"sphinx_autosummary_accessors",
9188
]
9289

9390
extlinks = {
@@ -116,7 +113,7 @@
116113
numpydoc_show_class_members = False
117114

118115
# Add any paths that contain templates here, relative to this directory.
119-
templates_path = ["_templates"]
116+
templates_path = ["_templates", sphinx_autosummary_accessors.templates_path]
120117

121118
# The suffix of source filenames.
122119
source_suffix = ".rst"
@@ -275,14 +272,14 @@
275272

276273
# -- Options for LaTeX output ---------------------------------------------
277274

278-
latex_elements = {
279-
# The paper size ('letterpaper' or 'a4paper').
280-
# 'papersize': 'letterpaper',
281-
# The font size ('10pt', '11pt' or '12pt').
282-
# 'pointsize': '10pt',
283-
# Additional stuff for the LaTeX preamble.
284-
# 'preamble': '',
285-
}
275+
# latex_elements = {
276+
# # The paper size ('letterpaper' or 'a4paper').
277+
# # 'papersize': 'letterpaper',
278+
# # The font size ('10pt', '11pt' or '12pt').
279+
# # 'pointsize': '10pt',
280+
# # Additional stuff for the LaTeX preamble.
281+
# # 'preamble': '',
282+
# }
286283

287284
# Grouping the document tree into LaTeX files. List of tuples
288285
# (source start file, target name, title,
@@ -364,113 +361,3 @@
364361
"dask": ("https://docs.dask.org/en/latest", None),
365362
"cftime": ("https://unidata.github.io/cftime", None),
366363
}
367-
368-
369-
# --------- autosummary templates ------------------
370-
# TODO: eventually replace this with a sphinx.ext.auto_accessor module
371-
class AccessorDocumenter(MethodDocumenter):
372-
"""
373-
Specialized Documenter subclass for accessors.
374-
"""
375-
376-
objtype = "accessor"
377-
directivetype = "method"
378-
379-
# lower than MethodDocumenter so this is not chosen for normal methods
380-
priority = 0.6
381-
382-
def format_signature(self):
383-
# this method gives an error/warning for the accessors, therefore
384-
# overriding it (accessor has no arguments)
385-
return ""
386-
387-
388-
class AccessorLevelDocumenter(Documenter):
389-
"""
390-
Specialized Documenter subclass for objects on accessor level (methods,
391-
attributes).
392-
"""
393-
394-
# This is the simple straightforward version
395-
# modname is None, base the last elements (eg 'hour')
396-
# and path the part before (eg 'Series.dt')
397-
# def resolve_name(self, modname, parents, path, base):
398-
# modname = 'pandas'
399-
# mod_cls = path.rstrip('.')
400-
# mod_cls = mod_cls.split('.')
401-
#
402-
# return modname, mod_cls + [base]
403-
404-
def resolve_name(self, modname, parents, path, base):
405-
if modname is None:
406-
if path:
407-
mod_cls = path.rstrip(".")
408-
else:
409-
mod_cls = None
410-
# if documenting a class-level object without path,
411-
# there must be a current class, either from a parent
412-
# auto directive ...
413-
mod_cls = self.env.temp_data.get("autodoc:class")
414-
# ... or from a class directive
415-
if mod_cls is None:
416-
mod_cls = self.env.temp_data.get("py:class")
417-
# ... if still None, there's no way to know
418-
if mod_cls is None:
419-
return None, []
420-
# HACK: this is added in comparison to ClassLevelDocumenter
421-
# mod_cls still exists of class.accessor, so an extra
422-
# rpartition is needed
423-
modname, accessor = rpartition(mod_cls, ".")
424-
modname, cls = rpartition(modname, ".")
425-
parents = [cls, accessor]
426-
# if the module name is still missing, get it like above
427-
if not modname:
428-
modname = self.env.temp_data.get("autodoc:module")
429-
if not modname:
430-
if sphinx.__version__ > "1.3":
431-
modname = self.env.ref_context.get("py:module")
432-
else:
433-
modname = self.env.temp_data.get("py:module")
434-
# ... else, it stays None, which means invalid
435-
return modname, parents + [base]
436-
437-
438-
class AccessorAttributeDocumenter(AccessorLevelDocumenter, AttributeDocumenter):
439-
440-
objtype = "accessorattribute"
441-
directivetype = "attribute"
442-
443-
# lower than AttributeDocumenter so this is not chosen for normal attributes
444-
priority = 0.6
445-
446-
447-
class AccessorMethodDocumenter(AccessorLevelDocumenter, MethodDocumenter):
448-
449-
objtype = "accessormethod"
450-
directivetype = "method"
451-
452-
# lower than MethodDocumenter so this is not chosen for normal methods
453-
priority = 0.6
454-
455-
456-
class AccessorCallableDocumenter(AccessorLevelDocumenter, MethodDocumenter):
457-
"""
458-
This documenter lets us removes .__call__ from the method signature for
459-
callable accessors like Series.plot
460-
"""
461-
462-
objtype = "accessorcallable"
463-
directivetype = "method"
464-
465-
# lower than MethodDocumenter; otherwise the doc build prints warnings
466-
priority = 0.5
467-
468-
def format_name(self):
469-
return MethodDocumenter.format_name(self).rstrip(".__call__")
470-
471-
472-
def setup(app):
473-
app.add_autodocumenter(AccessorDocumenter)
474-
app.add_autodocumenter(AccessorAttributeDocumenter)
475-
app.add_autodocumenter(AccessorMethodDocumenter)
476-
app.add_autodocumenter(AccessorCallableDocumenter)

doc/internals.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,11 @@ To help users keep things straight, please `let us know
171171
for an open source library. In the future, we will maintain a list of accessors
172172
and the libraries that implement them on this page.
173173

174+
To make documenting accessors with ``sphinx`` and ``sphinx.ext.autosummary``
175+
easier, you can use `sphinx-ext-autosummary`_.
176+
177+
.. _sphinx-ext-autosummary: https://sphinx-autosummary-accessors.readthedocs.io/
178+
174179
.. _zarr_encoding:
175180

176181
Zarr Encoding Specification

0 commit comments

Comments
 (0)