Skip to content

Add tests for detecting import errors when importing micro simulatiob objects#265

Merged
IshaanDesai merged 3 commits into
precice:developfrom
Geethapranay1:fix-missing-dependency
Jun 3, 2026
Merged

Add tests for detecting import errors when importing micro simulatiob objects#265
IshaanDesai merged 3 commits into
precice:developfrom
Geethapranay1:fix-missing-dependency

Conversation

@Geethapranay1

Copy link
Copy Markdown
Contributor

Fix #258

Previously, load_backend_class swallowed ImportError inside the try_load block. This hid the real error (like a missing dependency such as foamlib) and just threw a generic "Could not load micro simulation" error.
I moved the import_module call out of the try...except block so that ModuleNotFoundError bubbles up properly. I also added some tests to verify that missing direct and transitive dependencies report the correct error, while making sure the old fallback logic still works if the file loads but the class is missing

Checklist:

  • I made sure that the CI passed before I ask for a review.
  • I added a summary of the changes (compared to the last release) in the CHANGELOG.md.
  • If necessary, I made changes to the documentation and/or added new content.
  • I will remember to squash-and-merge, providing a useful summary of the changes of this PR.

@Geethapranay1

Copy link
Copy Markdown
Contributor Author

@IshaanDesai PTAL!

@IshaanDesai IshaanDesai requested a review from fsimonis May 26, 2026 07:01
@IshaanDesai

Copy link
Copy Markdown
Member

@fsimonis can you please test this with the foamlib case?

@fsimonis

Copy link
Copy Markdown
Member

This works, but doesn't give additional context.

@IshaanDesai

Copy link
Copy Markdown
Member

This works, but doesn't give additional context.

Okay. So the ImportError is now visible? If yes, is that sufficient to debug?

@Geethapranay1

Copy link
Copy Markdown
Contributor Author

@fsimonis could you clarify what context you'd expect? I can catch the ModuleNotFoundError and re-raise it with the micro simulation path included, ex: Missing dependency 'foamlib' required by micro simulation 'micro' is that what you are expecting?

@fsimonis

Copy link
Copy Markdown
Member

This is the output:

05/26/2026 11:06:04 AM - micro_manager.micro_manager - INFO - Total number of micro simulations: 100
Traceback (most recent call last):
  File ".../venv/bin/micro-manager-precice", line 8, in <module>
    sys.exit(main())
             ~~~~^^
  File ".../micro-manager/micro_manager/__init__.py", line 123, in main
    manager.initialize()
    ~~~~~~~~~~~~~~~~~~^^
  File ".../micro-manager/micro_manager/micro_manager.py", line 601, in initialize
    micro_problem_base = load_backend_class(self._config.get_micro_file_name())
  File ".../micro_manager/micro_simulation.py", line 602, in load_backend_class
    module = ipl.import_module(path_to_micro_file)
  File "/usr/lib/python3.14/importlib/__init__.py", line 88, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1406, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1371, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1342, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 938, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 759, in exec_module
  File "<frozen importlib._bootstrap>", line 491, in _call_with_frames_removed
  File ".../micro/python/micro.py", line 5, in <module>
    import foamlib
ModuleNotFoundError: No module named 'foamlib'

My propsed fix in #262 results in:

05/26/2026 11:08:51 AM - micro_manager.micro_manager - INFO - Total number of micro simulations: 100
Traceback (most recent call last):
  File "/.../micro_manager/micro_simulation.py", line 605, in load_backend_class
    mod = ipl.import_module(path_to_micro_file)
  File "/usr/lib/python3.14/importlib/__init__.py", line 88, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1406, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1371, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1342, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 938, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 759, in exec_module
  File "<frozen importlib._bootstrap>", line 491, in _call_with_frames_removed
  File "/.../micro/python/micro.py", line 5, in <module>
    import foamlib
ModuleNotFoundError: No module named 'foamlib'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/.../venv/bin/micro-manager-precice", line 8, in <module>
    sys.exit(main())
             ~~~~^^
  File "/.../micro-manager/micro_manager/__init__.py", line 123, in main
    manager.initialize()
    ~~~~~~~~~~~~~~~~~~^^
  File "/.../micro-manager/micro_manager/micro_manager.py", line 601, in initialize
    micro_problem_base = load_backend_class(self._config.get_micro_file_name())
  File "/.../micro_manager/micro_simulation.py", line 612, in load_backend_class
    raise RuntimeError(
        f"Counld not load a dependency of the python module '{path_to_micro_file}' containing the micro simulation: {ie}"
    )
RuntimeError: Counld not load a dependency of the python module 'python.micro' containing the micro simulation: No module named 'foamlib'

I'm fine with both.

@Geethapranay1

Copy link
Copy Markdown
Contributor Author

@fsimonis thanks for testing this and sharing the comparison with #262, really helpful
The contextual messages are a good idea. I'll update my pr to add the wrapping and I'll keep it as ModuleNotFoundError with from chaining so we preserve the exception type and get a clean traceback. I already have tests covering the missing dependency cases too.

@IshaanDesai IshaanDesai added the enhancement Enchance existing functionality label May 26, 2026
@IshaanDesai

Copy link
Copy Markdown
Member

@Geethapranay1 I merged the solution proposed by @fsimonis. From your contribution, the tests could still be valuable. Can you please merge the upstream develop branch into your branch and then reformulate the tests?

@Geethapranay1

Copy link
Copy Markdown
Contributor Author

@IshaanDesai Before I rebase the tests, i noted that on #262 error handling wrapping ModuleNotFoundError in RuntimeError drops the .name attribute and exception type, which means callers can't programmatically distinguish a missing dependency from other runtime failures may be re-raising as ModuleNotFoundError(...) from e preserves both the type and the causal chain while still adding the contextual message and also there is a minor typo error "Counld" -> "Could" typo on l623. i will include this fix alongside the tests and happy to take a opinion on the RuntimeError

@IshaanDesai IshaanDesai left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests are valuable, so I would still like to merge this PR. Please take a look at my comments. Thanks for the effort!

Comment thread micro_manager/micro_simulation.py
Comment thread micro_manager/micro_simulation.py Outdated
@Geethapranay1

Copy link
Copy Markdown
Contributor Author

@IshaanDesai reverted micro_simulation.py, only the tests remain now. PTAL!

@Geethapranay1 Geethapranay1 force-pushed the fix-missing-dependency branch from e4884c5 to 386267f Compare June 3, 2026 09:01
@IshaanDesai IshaanDesai changed the title Fix: Stop swallowing ImportError when loading micro simulation Add tests for detecting import errors when importing micro simulatiob objects Jun 3, 2026
@IshaanDesai IshaanDesai merged commit 7565e30 into precice:develop Jun 3, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Enchance existing functionality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error loading missing dependency of a micro simulation are insufficient

3 participants