Skip to content

Conversation

NFUChen
Copy link
Contributor

@NFUChen NFUChen commented Jul 20, 2025

Improve Error Handling in Module Importer

Summary

This PR improves error handling in the module importer by making import failures more explicit and removing silent error suppression. This change ensures that import errors are properly propagated rather than being silently ignored, which helps with debugging and provides better error visibility.

Changes Made

1. Version Bump

  • Updated version from 0.0.22 to 0.0.23 in py_spring_core/__init__.py

2. Module Importer Error Handling (py_spring_core/commons/module_importer.py)

  • Breaking Change: Modified import_module_from_path() to raise exceptions instead of returning None on import failures
  • Breaking Change: Removed ignore_errors parameter from import_classes_from_paths() method
  • Updated method signature and documentation to reflect the removal of error suppression

3. Core Utils Update (py_spring_core/core/utils.py)

  • Breaking Change: Removed is_ignore_error parameter from dynamically_import_modules() function
  • Updated function signature to align with the module importer changes

4. Test Updates (tests/test_module_importer.py)

  • Updated tests to expect exceptions instead of None returns for import failures
  • Added proper exception testing for non-existent files (FileNotFoundError)
  • Added proper exception testing for invalid Python syntax (SyntaxError)

Breaking Changes

⚠️ This is a breaking change that affects how import errors are handled:

  • Previously: Import failures would return None or be silently ignored
  • Now: Import failures will raise appropriate exceptions (FileNotFoundError, SyntaxError, etc.)

Benefits

  1. Better Error Visibility: Import failures are now immediately apparent rather than being silently ignored
  2. Improved Debugging: Developers can catch and handle specific import errors appropriately
  3. Consistent Behavior: Aligns with Python's standard exception handling patterns
  4. Fail-Fast Principle: Issues are discovered early rather than causing problems later in the application lifecycle

Migration Guide

If you're currently using the module importer and relying on silent error handling, you'll need to:

  1. Wrap import calls in try-catch blocks if you want to handle import failures gracefully
  2. Remove any ignore_errors or is_ignore_error parameters from your function calls
  3. Update any code that checks for None returns from import functions

Testing

  • All existing tests have been updated to reflect the new error handling behavior
  • New tests verify that appropriate exceptions are raised for different failure scenarios
  • Test coverage maintains the same level while ensuring proper error propagation

Related Issues

  • Addresses concerns about silent import failures
  • Improves error handling consistency across the framework

@NFUChen NFUChen self-assigned this Jul 20, 2025
@NFUChen NFUChen added the bug Something isn't working label Jul 20, 2025
@NFUChen NFUChen changed the title Fix/silent import errors Improve Error Handling in Module Importer Jul 20, 2025
@NFUChen NFUChen merged commit b987d2a into main Jul 20, 2025
1 check passed
@NFUChen NFUChen deleted the fix/silent-import-errors branch July 20, 2025 15:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants