Skip to content
Open
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
3 changes: 3 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
-----------------
Expand Down
21 changes: 17 additions & 4 deletions src/grimp/application/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
"""
Expand Down
18 changes: 17 additions & 1 deletion tests/unit/application/graph/test_direct_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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",
(
Expand Down
Loading