Skip to content

Warn on invalid module __all__ during initial load #13802

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 10 additions & 16 deletions sphinx/ext/autodoc/_documenters.py
Original file line number Diff line number Diff line change
Expand Up @@ -1045,18 +1045,8 @@ def can_document_member(
return False

def _module_all(self) -> Sequence[str] | None:
if self.object is not None and self.__all__ is None:
try:
if not self.options.ignore_module_all:
self.__all__ = inspect.getall(self.object)
except ValueError as exc:
# invalid __all__ found.
msg = __(
'__all__ should be a list of strings, not %r '
'(in module %s) -- ignoring __all__'
)
logger.warning(msg, exc.args[0], self.props.full_name, type='autodoc')

if self.__all__ is None and not self.options.ignore_module_all:
self.__all__ = self.props.all
return self.__all__

def add_directive_header(self, sig: str) -> None:
Expand Down Expand Up @@ -1107,8 +1097,8 @@ def get_module_members(self) -> dict[str, ObjectMember]:
def get_object_members(self, want_all: bool) -> tuple[bool, list[ObjectMember]]:
members = self.get_module_members()
if want_all:
module_all = self._module_all()
if module_all is None:
module_all = self.props.all
if self.options.ignore_module_all or module_all is None:
# for implicit module members, check __module__ to avoid
# documenting imported objects
return True, list(members.values())
Expand Down Expand Up @@ -1141,8 +1131,12 @@ def get_object_members(self, want_all: bool) -> tuple[bool, list[ObjectMember]]:
def sort_members(
self, documenters: list[tuple[Documenter, bool]], order: str
) -> list[tuple[Documenter, bool]]:
module_all = self._module_all()
if order == 'bysource' and module_all:
module_all = self.props.all
if (
order == 'bysource'
and not self.options.ignore_module_all
and module_all is not None
):
assert module_all is not None
module_all_set = frozenset(module_all)
module_all_len = len(module_all)
Expand Down
14 changes: 10 additions & 4 deletions sphinx/ext/autodoc/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -812,17 +812,23 @@ def _load_object_by_name(
obj_properties: set[_AutodocFuncProperty] = set()
if objtype == 'module':
file_path = getattr(module, '__file__', None)
try:
mod_all = inspect.getall(module)
except ValueError:
mod_all = safe_getattr(obj, '__all__', None)
if isinstance(mod_all, (list, tuple)) and all(
isinstance(e, str) for e in mod_all
):
mod_all = tuple(mod_all)
elif mod_all is not None:
# Invalid __all__ found.
msg = __('Ignoring invalid __all__ in module %s: %r')
logger.warning(msg, module_name, mod_all, type='autodoc')
mod_all = None

props = _ModuleProperties(
obj_type=objtype,
module_name=module_name,
docstring_lines=(),
file_path=Path(file_path) if file_path is not None else None,
all=tuple(mod_all) if mod_all is not None else None,
all=mod_all,
_obj=obj,
_obj___module__=obj.__name__,
)
Expand Down
Loading