Skip to content

Commit 829e9d8

Browse files
scharlottej13keewispre-commit-ci[bot]
authored
only override autosummary for accessor-related templates (#67)
* only override autosummary for accessor-related templates * create a list of known templates * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * import from the new `templates` module * compare against the list of known templates * add a entry to demonstrate that custom templates still work * actually find all templates * include all directory names in the main templates directory Co-authored-by: Keewis <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent b44965f commit 829e9d8

File tree

5 files changed

+56
-5
lines changed

5 files changed

+56
-5
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{{ fullname }}
2+
{{ underline }}
3+
4+
.. autoclass:: {{ [module, objname] | join('.') }}
5+
6+
{% block methods %}
7+
.. automethod:: __init__
8+
9+
{% if methods %}
10+
.. rubric:: {{ _('Methods') }}
11+
12+
.. autosummary::
13+
:toctree: generated/
14+
{% for item in methods %}
15+
{% if item != "__init__" %}
16+
~{{ [module, name] | join('.') }}.{{ item }}
17+
{% endif %}
18+
{%- endfor %}
19+
{% endif %}
20+
{% endblock %}
21+
22+
{% block attributes %}
23+
{% if attributes %}
24+
.. rubric:: {{ _('Attributes') }}
25+
26+
.. autosummary::
27+
:toctree: generated/
28+
{% for item in attributes %}
29+
~{{ [module, name] | join('.') }}.{{ item }}
30+
{%- endfor %}
31+
{% endif %}
32+
{% endblock %}

docs/examples.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,17 @@ become:
7575
:template: autosummary/accessor_method.rst
7676

7777
Example.test2.sub.func
78+
79+
Different templates still work:
80+
81+
.. literalinclude:: examples.rst
82+
:language: rst
83+
:lines: 87-91
84+
85+
becomes:
86+
87+
.. autosummary::
88+
:toctree: generated/
89+
:template: custom-template.rst
90+
91+
TestAccessor

sphinx_autosummary_accessors/__init__.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,20 @@
33
except ImportError:
44
from importlib_metadata import version
55

6-
import pathlib
7-
86
from . import autosummary
97
from .documenters import (
108
AccessorAttributeDocumenter,
119
AccessorCallableDocumenter,
1210
AccessorDocumenter,
1311
AccessorMethodDocumenter,
1412
)
13+
from .templates import templates_path # noqa: F401
1514

1615
try:
1716
__version__ = version("sphinx-autosummary-accessors")
1817
except Exception:
1918
__version__ = "999"
2019

21-
templates_path = str(pathlib.Path(__file__).parent / "templates")
22-
2320

2421
def add_autosummary_create_documenter(func):
2522
import sphinx.ext.autosummary

sphinx_autosummary_accessors/autosummary.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
from sphinx.ext.autosummary import Autosummary, generate
44

5+
from .templates import known_templates
6+
57
directives_re = re.compile(r"^\.\. ([^:]+):: (.+)$", re.MULTILINE)
68

79
original_create_documenter = Autosummary.create_documenter
@@ -22,7 +24,7 @@ def create_documenter_from_template(autosummary, app, obj, parent, full_name):
2224

2325
options = autosummary.options
2426
template_name = options.get("template", None)
25-
if template_name is None:
27+
if template_name is None or template_name not in known_templates:
2628
return original_create_documenter(autosummary, app, obj, parent, full_name)
2729

2830
imported_members = options.get("imported_members", False)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import pathlib
2+
3+
root = pathlib.Path(__file__).parent / "templates"
4+
5+
known_templates = [str(path.relative_to(root)) for path in root.glob("**/*.rst")]
6+
templates_path = str(root)

0 commit comments

Comments
 (0)