Skip to content

Commit e61f569

Browse files
authored
Do not add the module prefix when generating autosummary documents (#12609)
1 parent 47757c4 commit e61f569

File tree

11 files changed

+65
-33
lines changed

11 files changed

+65
-33
lines changed

CHANGES.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ Release 7.4.6 (in development)
44
Bugs fixed
55
----------
66

7+
* #12859, #9743, #12609: autosummary: Do not add the package prefix when
8+
generating autosummary directives for modules within a package.
9+
Patch by Adam Turner.
710

811
Release 7.4.5 (released Jul 16, 2024)
912
=====================================

sphinx/ext/autosummary/generate.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -330,10 +330,6 @@ def generate_autosummary_content(
330330
doc, app, obj, {'module'}, imported=True
331331
)
332332
skip += all_imported_modules
333-
imported_modules = [name + '.' + modname for modname in imported_modules]
334-
all_imported_modules = [
335-
name + '.' + modname for modname in all_imported_modules
336-
]
337333
public_members = getall(obj)
338334
else:
339335
imported_modules, all_imported_modules = [], []
@@ -476,21 +472,22 @@ def _get_modules(
476472
if modname in skip:
477473
# module was overwritten in __init__.py, so not accessible
478474
continue
479-
fullname = name + '.' + modname
475+
fullname = f'{name}.{modname}'
480476
try:
481477
module = import_module(fullname)
482-
if module and hasattr(module, '__sphinx_mock__'):
483-
continue
484478
except ImportError:
485479
pass
480+
else:
481+
if module and hasattr(module, '__sphinx_mock__'):
482+
continue
486483

487-
items.append(fullname)
484+
items.append(modname)
488485
if public_members is not None:
489486
if modname in public_members:
490-
public.append(fullname)
487+
public.append(modname)
491488
else:
492489
if not modname.startswith('_'):
493-
public.append(fullname)
490+
public.append(modname)
494491
return public, items
495492

496493

sphinx/ext/autosummary/templates/autosummary/module.rst

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,51 +3,51 @@
33
.. automodule:: {{ fullname }}
44

55
{% block attributes %}
6-
{% if attributes %}
6+
{%- if attributes %}
77
.. rubric:: {{ _('Module Attributes') }}
88

99
.. autosummary::
1010
{% for item in attributes %}
1111
{{ item }}
1212
{%- endfor %}
1313
{% endif %}
14-
{% endblock %}
14+
{%- endblock %}
1515

16-
{% block functions %}
17-
{% if functions %}
16+
{%- block functions %}
17+
{%- if functions %}
1818
.. rubric:: {{ _('Functions') }}
1919

2020
.. autosummary::
2121
{% for item in functions %}
2222
{{ item }}
2323
{%- endfor %}
2424
{% endif %}
25-
{% endblock %}
25+
{%- endblock %}
2626

27-
{% block classes %}
28-
{% if classes %}
27+
{%- block classes %}
28+
{%- if classes %}
2929
.. rubric:: {{ _('Classes') }}
3030

3131
.. autosummary::
3232
{% for item in classes %}
3333
{{ item }}
3434
{%- endfor %}
3535
{% endif %}
36-
{% endblock %}
36+
{%- endblock %}
3737

38-
{% block exceptions %}
39-
{% if exceptions %}
38+
{%- block exceptions %}
39+
{%- if exceptions %}
4040
.. rubric:: {{ _('Exceptions') }}
4141

4242
.. autosummary::
4343
{% for item in exceptions %}
4444
{{ item }}
4545
{%- endfor %}
4646
{% endif %}
47-
{% endblock %}
47+
{%- endblock %}
4848

49-
{% block modules %}
50-
{% if modules %}
49+
{%- block modules %}
50+
{%- if modules %}
5151
.. rubric:: Modules
5252

5353
.. autosummary::
@@ -57,4 +57,4 @@
5757
{{ item }}
5858
{%- endfor %}
5959
{% endif %}
60-
{% endblock %}
60+
{%- endblock %}

sphinx/project.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
import contextlib
66
import os
77
from glob import glob
8+
from pathlib import Path
89
from typing import TYPE_CHECKING
910

1011
from sphinx.locale import __
1112
from sphinx.util import logging
1213
from sphinx.util.matching import get_matching_files
13-
from sphinx.util.osutil import path_stabilize, relpath
14+
from sphinx.util.osutil import os_path, path_stabilize, relpath
1415

1516
if TYPE_CHECKING:
1617
from collections.abc import Iterable
@@ -24,7 +25,7 @@ class Project:
2425

2526
def __init__(self, srcdir: str | os.PathLike[str], source_suffix: Iterable[str]) -> None:
2627
#: Source directory.
27-
self.srcdir = srcdir
28+
self.srcdir = Path(srcdir)
2829

2930
#: source_suffix. Same as :confval:`source_suffix`.
3031
self.source_suffix = tuple(source_suffix)
@@ -114,6 +115,7 @@ def doc2path(self, docname: str, absolute: bool) -> str:
114115
# Backwards compatibility: the document does not exist
115116
filename = docname + self._first_source_suffix
116117

118+
filename = os_path(filename)
117119
if absolute:
118120
return os.path.join(self.srcdir, filename)
119121
return filename
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import os
2+
import sys
3+
4+
sys.path.insert(0, os.path.abspath('.'))
5+
6+
extensions = [
7+
'sphinx.ext.autosummary',
8+
]
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.. autosummary::
2+
:toctree: docs/pkg
3+
:recursive:
4+
5+
pkg

tests/roots/test-ext-autosummary-module_prefix/pkg/__init__.py

Whitespace-only changes.

tests/roots/test-ext-autosummary-module_prefix/pkg/mod0/__init__.py

Whitespace-only changes.

tests/roots/test-ext-autosummary-module_prefix/pkg/mod1/__init__.py

Whitespace-only changes.

tests/test_extensions/test_ext_autosummary.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -506,12 +506,20 @@ def test_autosummary_recursive(app, status, warning):
506506

507507
# Check content of recursively generated stub-files
508508
content = (app.srcdir / 'generated' / 'package.rst').read_text(encoding='utf8')
509-
assert 'package.module' in content
510-
assert 'package.package' in content
511-
assert 'package.module_importfail' in content
509+
assert 'module' in content
510+
assert 'package' in content
511+
assert 'module_importfail' in content
512+
# we no longer generate fully-qualified module names.
513+
assert 'package.module' not in content
514+
assert 'package.package' not in content
515+
assert 'package.module_importfail' not in content
512516

513517
content = (app.srcdir / 'generated' / 'package.package.rst').read_text(encoding='utf8')
514-
assert 'package.package.module' in content
518+
assert 'module' in content
519+
assert 'package.package.module' not in content
520+
521+
warnings = app.warning.getvalue()
522+
assert 'Summarised items should not include the current module.' not in warnings
515523

516524

517525
@pytest.mark.sphinx('dummy', testroot='ext-autosummary-recursive',
@@ -599,11 +607,11 @@ def test_autosummary_imported_members(app, status, warning):
599607
assert (' .. autosummary::\n'
600608
' \n'
601609
' Bar\n'
602-
' \n' in module)
610+
' ' in module)
603611
assert (' .. autosummary::\n'
604612
' \n'
605613
' foo\n'
606-
' \n' in module)
614+
' ' in module)
607615
finally:
608616
sys.modules.pop('autosummary_dummy_package', None)
609617

@@ -627,7 +635,7 @@ def test_autosummary_module_all(app, status, warning):
627635
assert ('.. autosummary::\n'
628636
' :toctree:\n'
629637
' :recursive:\n\n'
630-
' autosummary_dummy_package_all.extra_dummy_module\n\n' in module)
638+
' extra_dummy_module\n' in module)
631639
finally:
632640
sys.modules.pop('autosummary_dummy_package_all', None)
633641

tests/test_extensions/test_ext_autosummary_imports.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,12 @@ def test_autosummary_import_cycle(app, warning):
3838
"Replace 'spam.eggs.Ham' with 'Ham'."
3939
)
4040
assert expected in app.warning.getvalue()
41+
42+
43+
@pytest.mark.sphinx('dummy', testroot='ext-autosummary-module_prefix')
44+
@pytest.mark.usefixtures("rollback_sysmodules")
45+
def test_autosummary_generate_prefixes(app, warning):
46+
app.build()
47+
warnings = app.warning.getvalue()
48+
assert 'Summarised items should not include the current module.' not in warnings
49+
assert warnings == ''

0 commit comments

Comments
 (0)