015ReviewmidPython / Sphinx真实来源改编 Sphinx

Sphinx autodoc:让显式声明的空 __all__ 生效

审查一个 Sphinx autodoc 补丁:作者称模块声明 __all__ = [] 时成员仍会被全部文档化,补丁把空 __all__ 当作显式的空导出列表处理。

ai-pr.diffdiff · 68 lines
diff --git a/sphinx/ext/autodoc/__init__.py b/sphinx/ext/autodoc/__init__.pyindex 76265f5be..d4b8e021f 100644--- a/sphinx/ext/autodoc/__init__.py+++ b/sphinx/ext/autodoc/__init__.py@@ -1074,7 +1074,9 @@ class ModuleDocumenter(Documenter):     def get_object_members(self, want_all: bool) -> Tuple[bool, ObjectMembers]:         members = self.get_module_members()         if want_all:-            if not self.__all__:+            # An empty __all__ is an explicit export list; only fall back to+            # implicit member discovery when the module defines no __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.pynew file mode 100644index 000000000..c094cff33--- /dev/null+++ b/tests/roots/test-ext-autodoc/target/empty_all.py@@ -0,0 +1,16 @@+"""+docsting of empty_all module.+"""+__all__ = []+++def foo():+    """docstring"""+++def bar():+    """docstring"""+++def baz():+    """docstring"""diff --git a/tests/test_ext_autodoc_automodule.py b/tests/test_ext_autodoc_automodule.pynew file mode 100644index 000000000..71b9d1f22--- /dev/null+++ b/tests/test_ext_autodoc_automodule.py@@ -0,0 +1,24 @@+"""+    test_ext_autodoc_automodule+    ~~~~~~~~~~~~~~~~~~~~~~~~~~~++    Test the autodoc extension.  This tests mainly the Documenters; the auto+    directives are tested in a test source file translated by test_build.+"""++import pytest++from test_ext_autodoc import do_autodoc+++@pytest.mark.sphinx('html', testroot='ext-autodoc')+def test_empty_all(app):+    options = {'members': True}+    actual = do_autodoc(app, 'module', 'target.empty_all', options)+    assert list(actual) == [+        '',+        '.. py:module:: target.empty_all',+        '',+        'docsting of empty_all module.',+        '',+    ]