diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 12dc6070..781d404a 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -10,6 +10,9 @@ latest * Tweak Just commands for running version-specific Python tests. * Remove `typing-extensions` as a dependency. * Make package FIPS compatible by marking blake2 hashing with `usedforsecurity=False`. +* Raise `ModuleNotPresent` instead of an unclear error when `find_modules_that_directly_import` or + `find_modules_directly_imported_by` is called with a module that is not in the graph. + (https://github.com/python-grimp/grimp/issues/113) 3.14 (2025-12-10) ----------------- diff --git a/src/grimp/application/graph.py b/src/grimp/application/graph.py index 21533f18..165eecb3 100644 --- a/src/grimp/application/graph.py +++ b/src/grimp/application/graph.py @@ -217,13 +217,26 @@ def direct_import_exists( ) def find_modules_directly_imported_by(self, module: str) -> set[str]: + """ + Find all modules that are directly imported by the supplied module. + + If the module is not present in the graph, grimp.exceptions.ModuleNotPresent will be raised. + """ + if not self._rustgraph.contains_module(module): + msg = f'"{module}" not present in the graph.' + raise ModuleNotPresent(msg) return self._rustgraph.find_modules_directly_imported_by(module) def find_modules_that_directly_import(self, module: str) -> set[str]: - if self._rustgraph.contains_module(module): - # TODO panics if module isn't in modules. - return self._rustgraph.find_modules_that_directly_import(module) - return set() + """ + Find all modules that directly import the supplied module. + + If the module is not present in the graph, grimp.exceptions.ModuleNotPresent will be raised. + """ + if not self._rustgraph.contains_module(module): + msg = f'"{module}" not present in the graph.' + raise ModuleNotPresent(msg) + return self._rustgraph.find_modules_that_directly_import(module) def get_import_details(self, *, importer: str, imported: str) -> list[DetailedImport]: """ diff --git a/tests/unit/application/graph/test_direct_imports.py b/tests/unit/application/graph/test_direct_imports.py index c70112ac..dcf58f4e 100644 --- a/tests/unit/application/graph/test_direct_imports.py +++ b/tests/unit/application/graph/test_direct_imports.py @@ -3,7 +3,7 @@ import pytest from grimp.application.graph import ImportGraph -from grimp.exceptions import InvalidImportExpression +from grimp.exceptions import InvalidImportExpression, ModuleNotPresent def test_find_modules_directly_imported_by(): @@ -34,6 +34,22 @@ def test_find_modules_that_directly_import(): assert {a, f} == graph.find_modules_that_directly_import("bar") +def test_find_modules_directly_imported_by_raises_module_not_present(): + graph = ImportGraph() + graph.add_module("foo") + + with pytest.raises(ModuleNotPresent, match=re.escape('"bar" not present in the graph.')): + graph.find_modules_directly_imported_by("bar") + + +def test_find_modules_that_directly_import_raises_module_not_present(): + graph = ImportGraph() + graph.add_module("foo") + + with pytest.raises(ModuleNotPresent, match=re.escape('"bar" not present in the graph.')): + graph.find_modules_that_directly_import("bar") + + @pytest.mark.parametrize( "importer, imported, as_packages, expected_result", (