Skip to content

Commit b987d2a

Browse files
authored
Improve Error Handling in Module Importer (#18)
1 parent 40bd4d3 commit b987d2a

File tree

4 files changed

+9
-15
lines changed

4 files changed

+9
-15
lines changed

py_spring_core/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from py_spring_core.event.application_event_publisher import ApplicationEventPublisher
2323
from py_spring_core.event.commons import ApplicationEvent
2424

25-
__version__ = "0.0.22"
25+
__version__ = "0.0.23"
2626

2727
__all__ = [
2828
"PySpringApplication",

py_spring_core/commons/module_importer.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def import_module_from_path(self, file_path: str) -> Optional[Any]:
5959
return module
6060
except Exception as error:
6161
logger.warning(f"[MODULE IMPORT] Failed to import {module_name}: {error}")
62-
return None
62+
raise error
6363

6464
def extract_classes_from_module(self, module: Any) -> list[Type[object]]:
6565
"""
@@ -84,16 +84,14 @@ def extract_classes_from_module(self, module: Any) -> list[Type[object]]:
8484
def import_classes_from_paths(
8585
self,
8686
file_paths: Iterable[str],
87-
target_subclasses: Iterable[Type[object]] = [],
88-
ignore_errors: bool = True
87+
target_subclasses: Iterable[Type[object]] = []
8988
) -> set[Type[object]]:
9089
"""
9190
Import classes from multiple file paths with optional filtering.
9291
9392
Args:
9493
file_paths (Iterable[str]): The file paths of the modules to import.
9594
target_subclasses (Iterable[Type[object]], optional): Target subclasses to filter. Defaults to [].
96-
ignore_errors (bool, optional): Whether to ignore import errors. Defaults to True.
9795
9896
Returns:
9997
set[Type[object]]: Set of imported classes.
@@ -103,9 +101,7 @@ def import_classes_from_paths(
103101
for file_path in file_paths:
104102
module = self.import_module_from_path(file_path)
105103
if module is None:
106-
if not ignore_errors:
107-
raise ImportError(f"Failed to import module from {file_path}")
108-
continue
104+
raise ImportError(f"Failed to import module from {file_path}")
109105

110106
loaded_classes = self.extract_classes_from_module(module)
111107
all_loaded_classes.extend(loaded_classes)

py_spring_core/core/utils.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
def dynamically_import_modules(
1414
module_paths: Iterable[str],
15-
is_ignore_error: bool = True,
1615
target_subclasses: Iterable[Type[object]] = [],
1716
) -> set[Type[object]]:
1817
"""
@@ -29,7 +28,6 @@ def dynamically_import_modules(
2928
return _module_importer.import_classes_from_paths(
3029
file_paths=module_paths,
3130
target_subclasses=target_subclasses,
32-
ignore_errors=is_ignore_error
3331
)
3432

3533

tests/test_module_importer.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -204,24 +204,24 @@ class TestClass:
204204

205205
def test_import_nonexistent_file(self):
206206
"""Test importing a non-existent file."""
207-
result = self.importer.import_module_from_path("/nonexistent/file.py")
208-
assert result is None
207+
with pytest.raises(FileNotFoundError):
208+
self.importer.import_module_from_path("/nonexistent/file.py")
209209

210210
def test_import_invalid_python_file(self):
211211
"""Test importing a file with invalid Python syntax."""
212212
with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False) as f:
213213
f.write("""
214214
class TestClass:
215215
def __init__(self):
216-
# Invalid syntax
216+
# Invalid syntax - missing colon
217217
if True
218218
pass
219219
""")
220220
temp_file_path = f.name
221221

222222
try:
223-
result = self.importer.import_module_from_path(temp_file_path)
224-
assert result is None
223+
with pytest.raises(SyntaxError):
224+
self.importer.import_module_from_path(temp_file_path)
225225

226226
finally:
227227
os.unlink(temp_file_path)

0 commit comments

Comments
 (0)