Skip to content

Commit

Permalink
fix: Don't crash on unsupported item in __all__ (log a warning inst…
Browse files Browse the repository at this point in the history
…ead)

Issue #92: #92
  • Loading branch information
pawamoy committed Sep 22, 2022
1 parent 68446f7 commit 9e5df0a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/griffe/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,10 @@ def expand_exports(self, module: Module, seen: set | None = None) -> None:
next_module = self.modules_collection[module_path]
if next_module.path not in seen:
self.expand_exports(next_module, seen)
expanded |= next_module.exports
try:
expanded |= next_module.exports
except TypeError:
logger.warning(f"Unsupported item in {module.path}.__all__: {export} (use strings only)")
else:
expanded.add(export)
module.exports = expanded
Expand Down
15 changes: 15 additions & 0 deletions tests/test_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,18 @@ def test_load_single_module_from_both_py_and_pyi_files():
loader = GriffeLoader(search_paths=[tmp_folder.path])
package = loader.load_module("mod")
assert "f" in package.members


def test_unsupported_item_in_all(caplog):
"""Check that unsupported items in `__all__` log a warning.
Parameters:
caplog: Pytest fixture to capture logs.
"""
item_name = "XXX"
with temporary_pypackage("package", ["mod.py"]) as tmp_folder:
tmp_folder.path.joinpath("__init__.py").write_text(f"from .mod import {item_name}\n__all__ = [{item_name}]")
tmp_folder.path.joinpath("mod.py").write_text(f"class {item_name}: ...")
loader = GriffeLoader(search_paths=[tmp_folder.tmpdir])
loader.expand_exports(loader.load_module("package"))
assert any(item_name in record.message and record.levelname == "WARNING" for record in caplog.records)

0 comments on commit 9e5df0a

Please # to comment.