From 9e5df0aea8e615217554e5204221a35c9df25938 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Mazzucotelli?= Date: Thu, 22 Sep 2022 21:11:03 +0200 Subject: [PATCH] fix: Don't crash on unsupported item in `__all__` (log a warning instead) Issue #92: https://github.com/mkdocstrings/griffe/issues/92 --- src/griffe/loader.py | 5 ++++- tests/test_loader.py | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/griffe/loader.py b/src/griffe/loader.py index 10fc3793..7842473f 100644 --- a/src/griffe/loader.py +++ b/src/griffe/loader.py @@ -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 diff --git a/tests/test_loader.py b/tests/test_loader.py index 2740ea66..565a43a3 100644 --- a/tests/test_loader.py +++ b/tests/test_loader.py @@ -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)