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
10 changes: 7 additions & 3 deletions tests/utils_/test_import_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,15 @@ def test_returns_false_when_find_spec_raises(self):
"""``find_spec`` itself can raise for dotted names whose parent package
fails to import. This should be treated as the module being unavailable.
"""
with patch(
"vllm.utils.import_utils.importlib.util.find_spec",
side_effect=ModuleNotFoundError("No module named 'fake_parent'"),
with (
patch(
"vllm.utils.import_utils.importlib.util.find_spec",
side_effect=ModuleNotFoundError("No module named 'fake_parent'"),
),
patch("vllm.utils.import_utils.logger.warning") as mock_warning,
):
assert _has_module("fake_parent.child") is False
mock_warning.assert_not_called()

def test_result_is_cached(self):
"""Verify the @cache decorator prevents repeated imports."""
Expand Down
10 changes: 8 additions & 2 deletions vllm/utils/import_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,8 +401,14 @@ def _has_module(module_name: str) -> bool:
for the same module incur no additional overhead.
"""
try:
if importlib.util.find_spec(module_name) is None:
return False
module_spec = importlib.util.find_spec(module_name)
except ModuleNotFoundError:
return False

if module_spec is None:
return False

try:
importlib.import_module(module_name)
except Exception:
logger.warning(
Expand Down
Loading