diff --git a/sphinx/ext/autodoc/__init__.py b/sphinx/ext/autodoc/__init__.py index d85d796171d..c9f90accab0 100644 --- a/sphinx/ext/autodoc/__init__.py +++ b/sphinx/ext/autodoc/__init__.py @@ -1074,7 +1074,7 @@ def get_module_members(self) -> Dict[str, ObjectMember]: def get_object_members(self, want_all: bool) -> Tuple[bool, ObjectMembers]: members = self.get_module_members() if want_all: - if not self.__all__: + if self.__all__ is None: # for implicit module members, check __module__ to avoid # documenting imported objects return True, list(members.values()) diff --git a/tests/roots/test-ext-autodoc/target/empty_all.py b/tests/roots/test-ext-autodoc/target/empty_all.py new file mode 100644 index 00000000000..2baefa8bbc9 --- /dev/null +++ b/tests/roots/test-ext-autodoc/target/empty_all.py @@ -0,0 +1,15 @@ +"""Module with explicit empty __all__ for autodoc tests.""" + +__all__ = [] + + +def foo() -> None: + """Foo function for autodoc.""" + + +def bar() -> None: + """Bar function for autodoc.""" + + +def baz() -> None: + """Baz function for autodoc.""" diff --git a/tests/test_ext_autodoc.py b/tests/test_ext_autodoc.py index 39897eb7da1..c2d505876ac 100644 --- a/tests/test_ext_autodoc.py +++ b/tests/test_ext_autodoc.py @@ -658,6 +658,35 @@ def test_autodoc_imported_members(app): assert '.. py:function:: save_traceback(app: Sphinx) -> str' in actual +@pytest.mark.sphinx('html', testroot='ext-autodoc') +def test_autodoc_module_with_explicit_empty_all(app): + options = {"members": None} + actual = do_autodoc(app, 'module', 'target.empty_all', options) + assert list(filter(lambda l: 'function::' in l, actual)) == [] + + +@pytest.mark.sphinx('html', testroot='ext-autodoc') +def test_autodoc_module_explicit_members_with_empty_all(app): + options = {"members": "foo,baz"} + actual = do_autodoc(app, 'module', 'target.empty_all', options) + assert list(filter(lambda l: 'function::' in l, actual)) == [ + '.. py:function:: baz() -> None', + '.. py:function:: foo() -> None', + ] + + +@pytest.mark.sphinx('html', testroot='ext-autodoc') +def test_autodoc_module_ignore_empty_all(app): + options = {"members": None, + "ignore-module-all": None} + actual = do_autodoc(app, 'module', 'target.empty_all', options) + assert list(filter(lambda l: 'function::' in l, actual)) == [ + '.. py:function:: bar() -> None', + '.. py:function:: baz() -> None', + '.. py:function:: foo() -> None', + ] + + @pytest.mark.sphinx('html', testroot='ext-autodoc') def test_autodoc_special_members(app): # specific special methods