Skip to content

Commit cfb4786

Browse files
vpratzAA-Turner
andauthored
autosummary: Respect empty module __all__ (#13187)
Co-authored-by: Adam Turner <[email protected]>
1 parent a0cd666 commit cfb4786

File tree

7 files changed

+66
-4
lines changed

7 files changed

+66
-4
lines changed

CHANGES.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ Features added
5454
Bugs fixed
5555
----------
5656

57+
* #12463: autosummary: Respect an empty module ``__all__``.
58+
Patch by Valentin Pratz
5759
* #13060: HTML Search: use ``Map`` to store per-file term scores.
5860
Patch by James Addison
5961
* #13130: LaTeX docs: ``pdflatex`` index creation may fail for index entries

sphinx/ext/autosummary/generate.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,11 @@ def members_of(obj: Any, conf: Config) -> Sequence[str]:
275275
if conf.autosummary_ignore_module_all:
276276
return dir(obj)
277277
else:
278-
return getall(obj) or dir(obj)
278+
if (obj___all__ := getall(obj)) is not None:
279+
# return __all__, even if empty.
280+
return obj___all__
281+
# if __all__ is not set, return dir(obj)
282+
return dir(obj)
279283

280284

281285
def generate_autosummary_content(
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__all__ = ()
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import sys
2+
from pathlib import Path
3+
4+
sys.path.insert(0, str(Path.cwd().resolve()))
5+
6+
extensions = ['sphinx.ext.autodoc', 'sphinx.ext.autosummary']
7+
autodoc_default_options = {'members': True}
8+
autosummary_ignore_module_all = False
9+
autosummary_imorted_members = False
10+
11+
templates_path = ['templates']
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
test-ext-autosummary-module_all
2+
===============================
3+
4+
.. autosummary::
5+
:toctree: generated
6+
:recursive:
7+
8+
autosummary_dummy_package_empty_all
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{{ fullname | escape | underline}}
2+
3+
.. automodule:: {{ fullname }}
4+
5+
{% block members %}
6+
Summary
7+
-------
8+
.. autosummary::
9+
10+
{% for item in members %}
11+
{{ item }}
12+
{%- endfor %}
13+
{% endblock %}

tests/test_extensions/test_ext_autosummary.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -826,9 +826,8 @@ def test_autosummary_module_all(app):
826826
app.build()
827827
# generated/foo is generated successfully
828828
assert app.env.get_doctree('generated/autosummary_dummy_package_all')
829-
module = (
830-
app.srcdir / 'generated' / 'autosummary_dummy_package_all.rst'
831-
).read_text(encoding='utf8')
829+
path = app.srcdir / 'generated' / 'autosummary_dummy_package_all.rst'
830+
module = path.read_text(encoding='utf8')
832831
assert ' .. autosummary::\n \n PublicBar\n \n' in module
833832
assert (
834833
' .. autosummary::\n \n public_foo\n public_baz\n \n'
@@ -840,6 +839,30 @@ def test_autosummary_module_all(app):
840839
sys.modules.pop('autosummary_dummy_package_all', None)
841840

842841

842+
@pytest.mark.sphinx('dummy', testroot='ext-autosummary-module_empty_all')
843+
def test_autosummary_module_empty_all(app):
844+
try:
845+
app.build()
846+
# generated/foo is generated successfully
847+
assert app.env.get_doctree('generated/autosummary_dummy_package_empty_all')
848+
path = app.srcdir / 'generated' / 'autosummary_dummy_package_empty_all.rst'
849+
module = path.read_text(encoding='utf8')
850+
assert '.. automodule:: autosummary_dummy_package_empty_all' in module
851+
# for __all__ = (), the output should not contain any variables
852+
assert '__all__' not in module
853+
assert '__builtins__' not in module
854+
assert '__cached__' not in module
855+
assert '__doc__' not in module
856+
assert '__file__' not in module
857+
assert '__loader__' not in module
858+
assert '__name__' not in module
859+
assert '__package__' not in module
860+
assert '__path__' not in module
861+
assert '__spec__' not in module
862+
finally:
863+
sys.modules.pop('autosummary_dummy_package_all', None)
864+
865+
843866
@pytest.mark.sphinx(
844867
'html',
845868
testroot='ext-autodoc',

0 commit comments

Comments
 (0)