015ReviewmidPython / Sphinx真实来源改编 Sphinx
Sphinx autodoc:让显式声明的空 __all__ 生效
审查一个 Sphinx autodoc 补丁:作者称模块声明 __all__ = [] 时成员仍会被全部文档化,补丁把空 __all__ 当作显式的空导出列表处理。
@@ -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())new file mode 100644@@ -0,0 +1,16 @@+"""+docsting of empty_all module.+"""+__all__ = []+++def foo():+ """docstring"""+++def bar():+ """docstring"""+++def baz():+ """docstring"""new file mode 100644@@ -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.',+ '',+ ]