Merge pull request #7578 from tk0miya/7570_autosummary_template_option

Fix #7570: autosummary: template option is broken
This commit is contained in:
Takeshi KOMIYA 2020-04-30 21:53:42 +09:00 committed by GitHub
commit ca082f6893
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 36 additions and 10 deletions

View File

@ -156,17 +156,14 @@ class AutosummaryRenderer:
except TemplateNotFound: except TemplateNotFound:
return False return False
def render(self, objtype: str, context: Dict) -> str: def render(self, template_name: str, context: Dict) -> str:
"""Render a template file.""" """Render a template file."""
if objtype.endswith('.rst'): if template_name.endswith('.rst'):
# old styled: template_name is given template = self.env.get_template(template_name)
warnings.warn('AutosummaryRenderer.render() takes an object type as an argument.',
RemovedInSphinx50Warning, stacklevel=2)
return self.env.get_template(objtype).render(context)
else: else:
# objtype is given # objtype is given as template_name
try: try:
template = self.env.get_template('autosummary/%s.rst' % objtype) template = self.env.get_template('autosummary/%s.rst' % template_name)
except TemplateNotFound: except TemplateNotFound:
# fallback to base.rst # fallback to base.rst
template = self.env.get_template('autosummary/base.rst') template = self.env.get_template('autosummary/base.rst')
@ -268,6 +265,9 @@ def generate_autosummary_content(name: str, obj: Any, parent: Any,
ns['objtype'] = doc.objtype ns['objtype'] = doc.objtype
ns['underline'] = len(name) * '=' ns['underline'] = len(name) * '='
if template_name:
return template.render(template_name, ns)
else:
return template.render(doc.objtype, ns) return template.render(doc.objtype, ns)

View File

@ -0,0 +1 @@
EMPTY

View File

@ -0,0 +1,10 @@
import os
import sys
sys.path.insert(0, os.path.abspath('.'))
extensions = ['sphinx.ext.autosummary']
autosummary_generate = True
autodoc_default_options = {'members': True}
templates_path = ['_templates']

View File

@ -0,0 +1,5 @@
.. autosummary::
:toctree: generate
:template: empty.rst
target.Foo

View File

@ -0,0 +1,2 @@
class Foo:
"""docstring of Foo."""

View File

@ -378,6 +378,14 @@ def test_autosummary_skip_member(app):
assert 'Foo._privatemeth' in content assert 'Foo._privatemeth' in content
@pytest.mark.sphinx(testroot='ext-autosummary-template')
def test_autosummary_template(app):
app.build()
content = (app.srcdir / 'generate' / 'target.Foo.rst').read_text()
assert 'EMPTY' in content
@pytest.mark.sphinx('dummy', testroot='ext-autosummary', @pytest.mark.sphinx('dummy', testroot='ext-autosummary',
confoverrides={'autosummary_generate': []}) confoverrides={'autosummary_generate': []})
def test_empty_autosummary_generate(app, status, warning): def test_empty_autosummary_generate(app, status, warning):