Skip to content

Commit

Permalink
autosummary: Respect empty module __all__ (#13187)
Browse files Browse the repository at this point in the history
Co-authored-by: Adam Turner <[email protected]>
  • Loading branch information
vpratz and AA-Turner authored Jan 16, 2025
1 parent a0cd666 commit cfb4786
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ Features added
Bugs fixed
----------

* #12463: autosummary: Respect an empty module ``__all__``.
Patch by Valentin Pratz
* #13060: HTML Search: use ``Map`` to store per-file term scores.
Patch by James Addison
* #13130: LaTeX docs: ``pdflatex`` index creation may fail for index entries
Expand Down
6 changes: 5 additions & 1 deletion sphinx/ext/autosummary/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,11 @@ def members_of(obj: Any, conf: Config) -> Sequence[str]:
if conf.autosummary_ignore_module_all:
return dir(obj)
else:
return getall(obj) or dir(obj)
if (obj___all__ := getall(obj)) is not None:
# return __all__, even if empty.
return obj___all__
# if __all__ is not set, return dir(obj)
return dir(obj)


def generate_autosummary_content(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__all__ = ()
11 changes: 11 additions & 0 deletions tests/roots/test-ext-autosummary-module_empty_all/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import sys
from pathlib import Path

sys.path.insert(0, str(Path.cwd().resolve()))

extensions = ['sphinx.ext.autodoc', 'sphinx.ext.autosummary']
autodoc_default_options = {'members': True}
autosummary_ignore_module_all = False
autosummary_imorted_members = False

templates_path = ['templates']
8 changes: 8 additions & 0 deletions tests/roots/test-ext-autosummary-module_empty_all/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
test-ext-autosummary-module_all
===============================

.. autosummary::
:toctree: generated
:recursive:

autosummary_dummy_package_empty_all
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{{ fullname | escape | underline}}

.. automodule:: {{ fullname }}

{% block members %}
Summary
-------
.. autosummary::

{% for item in members %}
{{ item }}
{%- endfor %}
{% endblock %}
29 changes: 26 additions & 3 deletions tests/test_extensions/test_ext_autosummary.py
Original file line number Diff line number Diff line change
Expand Up @@ -826,9 +826,8 @@ def test_autosummary_module_all(app):
app.build()
# generated/foo is generated successfully
assert app.env.get_doctree('generated/autosummary_dummy_package_all')
module = (
app.srcdir / 'generated' / 'autosummary_dummy_package_all.rst'
).read_text(encoding='utf8')
path = app.srcdir / 'generated' / 'autosummary_dummy_package_all.rst'
module = path.read_text(encoding='utf8')
assert ' .. autosummary::\n \n PublicBar\n \n' in module
assert (
' .. autosummary::\n \n public_foo\n public_baz\n \n'
Expand All @@ -840,6 +839,30 @@ def test_autosummary_module_all(app):
sys.modules.pop('autosummary_dummy_package_all', None)


@pytest.mark.sphinx('dummy', testroot='ext-autosummary-module_empty_all')
def test_autosummary_module_empty_all(app):
try:
app.build()
# generated/foo is generated successfully
assert app.env.get_doctree('generated/autosummary_dummy_package_empty_all')
path = app.srcdir / 'generated' / 'autosummary_dummy_package_empty_all.rst'
module = path.read_text(encoding='utf8')
assert '.. automodule:: autosummary_dummy_package_empty_all' in module
# for __all__ = (), the output should not contain any variables
assert '__all__' not in module
assert '__builtins__' not in module
assert '__cached__' not in module
assert '__doc__' not in module
assert '__file__' not in module
assert '__loader__' not in module
assert '__name__' not in module
assert '__package__' not in module
assert '__path__' not in module
assert '__spec__' not in module
finally:
sys.modules.pop('autosummary_dummy_package_all', None)


@pytest.mark.sphinx(
'html',
testroot='ext-autodoc',
Expand Down

0 comments on commit cfb4786

Please sign in to comment.