diff --git a/sphinx/ext/autosummary/generate.py b/sphinx/ext/autosummary/generate.py index 3ce57159d..b125d0c6a 100644 --- a/sphinx/ext/autosummary/generate.py +++ b/sphinx/ext/autosummary/generate.py @@ -156,22 +156,19 @@ class AutosummaryRenderer: except TemplateNotFound: return False - def render(self, objtype: str, context: Dict) -> str: + def render(self, template_name: str, context: Dict) -> str: """Render a template file.""" - if objtype.endswith('.rst'): - # old styled: template_name is given - warnings.warn('AutosummaryRenderer.render() takes an object type as an argument.', - RemovedInSphinx50Warning, stacklevel=2) - return self.env.get_template(objtype).render(context) + if template_name.endswith('.rst'): + template = self.env.get_template(template_name) else: - # objtype is given + # objtype is given as template_name try: - template = self.env.get_template('autosummary/%s.rst' % objtype) + template = self.env.get_template('autosummary/%s.rst' % template_name) except TemplateNotFound: # fallback to base.rst template = self.env.get_template('autosummary/base.rst') - return template.render(context) + return template.render(context) # -- Generating output --------------------------------------------------------- @@ -268,7 +265,10 @@ def generate_autosummary_content(name: str, obj: Any, parent: Any, ns['objtype'] = doc.objtype ns['underline'] = len(name) * '=' - return template.render(doc.objtype, ns) + if template_name: + return template.render(template_name, ns) + else: + return template.render(doc.objtype, ns) def generate_autosummary_docs(sources: List[str], output_dir: str = None, diff --git a/tests/roots/test-ext-autosummary-template/_templates/empty.rst b/tests/roots/test-ext-autosummary-template/_templates/empty.rst new file mode 100644 index 000000000..7f7204cf5 --- /dev/null +++ b/tests/roots/test-ext-autosummary-template/_templates/empty.rst @@ -0,0 +1 @@ +EMPTY diff --git a/tests/roots/test-ext-autosummary-template/conf.py b/tests/roots/test-ext-autosummary-template/conf.py new file mode 100644 index 000000000..cc2363527 --- /dev/null +++ b/tests/roots/test-ext-autosummary-template/conf.py @@ -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'] diff --git a/tests/roots/test-ext-autosummary-template/index.rst b/tests/roots/test-ext-autosummary-template/index.rst new file mode 100644 index 000000000..c9f28b0fe --- /dev/null +++ b/tests/roots/test-ext-autosummary-template/index.rst @@ -0,0 +1,5 @@ +.. autosummary:: + :toctree: generate + :template: empty.rst + + target.Foo diff --git a/tests/roots/test-ext-autosummary-template/target.py b/tests/roots/test-ext-autosummary-template/target.py new file mode 100644 index 000000000..c607b5989 --- /dev/null +++ b/tests/roots/test-ext-autosummary-template/target.py @@ -0,0 +1,2 @@ +class Foo: + """docstring of Foo.""" diff --git a/tests/test_ext_autosummary.py b/tests/test_ext_autosummary.py index 114694166..63fafe860 100644 --- a/tests/test_ext_autosummary.py +++ b/tests/test_ext_autosummary.py @@ -378,6 +378,14 @@ def test_autosummary_skip_member(app): 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', confoverrides={'autosummary_generate': []}) def test_empty_autosummary_generate(app, status, warning):