Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
fix(utils): narrow check_import exception handling
Catch ImportError instead of bare Exception so present-but-broken modules surface their real error instead of a misleading missing-module hint. Focused tests in tests/utils/test_import_utils.py.
  • Loading branch information
Bruce-Yii committed Sep 8, 2026
commit b117521af777dea35b635960356895b4f8b15b81
2 changes: 1 addition & 1 deletion evalscope/utils/import_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def check_import(
for i, mod_name in enumerate(module_names):
try:
importlib.import_module(mod_name)
except Exception:
except ImportError:
missing_modules.append(mod_name)
if i < len(packages) and packages[i]:
missing_packages.append(packages[i])
Expand Down
30 changes: 30 additions & 0 deletions tests/utils/test_import_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Copyright (c) Alibaba, Inc. and its affiliates.
"""Focused regression tests for evalscope.utils.import_utils.check_import."""
import pytest

from evalscope.utils.import_utils import check_import


class TestCheckImport:
def test_missing_module_returns_false(self) -> None:
assert check_import('definitely_missing_mod_xyz', raise_warning=False) is False

def test_missing_module_raise_error(self) -> None:
with pytest.raises(ImportError, match='not found'):
check_import('definitely_missing_mod_xyz', raise_warning=False, raise_error=True)

def test_broken_module_propagates_runtime_error(self, tmp_path, monkeypatch) -> None:
(tmp_path / 'brokemod_rg.py').write_text('raise RuntimeError(\'boom-on-import\')\n')
monkeypatch.syspath_prepend(str(tmp_path))
with pytest.raises(RuntimeError, match='boom-on-import'):
check_import('brokemod_rg', raise_warning=False)

def test_broken_module_not_reported_as_missing(self, tmp_path, monkeypatch, caplog) -> None:
(tmp_path / 'brokemod_rg2.py').write_text('raise RuntimeError(\'boom-on-import\')\n')
monkeypatch.syspath_prepend(str(tmp_path))
with pytest.raises(RuntimeError):
check_import('brokemod_rg2', raise_warning=True)
assert 'not found' not in caplog.text

def test_existing_module_returns_true(self) -> None:
assert check_import('os', raise_warning=False) is True