Fix mpi4py import in test_optProb - #488
Merged
Merged
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #488 +/- ##
=======================================
Coverage 54.22% 54.22%
=======================================
Files 1 1
Lines 225 225
=======================================
Hits 122 122
Misses 103 103 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
A-CGray
approved these changes
Aug 24, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Purpose
The recently added tests are all failing with the following error in my development environment.
This is because
import mpi4pyimports the package and does not bind theMPIextension submodule as an attribute. Accessingmpi4py.MPItherefore raisesAttributeError, which theexcept ImportErrordoes not catch, so the module fails to import instead of falling back tocomm = None.Why this is passing on CI is not entirely clear, its probably because something earlier in the same process had already run
from mpi4py import MPI. Undertestflo -i, tests in a class withN_PROCSare launched viatestflo/mpirun.py, which imports the submodule at module level before importing the test module, so this did not come up. Test classes withoutN_PROCSlike this one are launched viatestflo/isolatedrun.py, which does not, so the eightTestScalingtests added in #486 fail at import in any environment with mpi4py installed.This PR switches to
from mpi4py import MPIwhich makes the guard behave as intended regardless of import order.Expected time until merged
ASAP
Type of change
Testing
Checklist
ruff checkandruff formatto make sure the Python code adheres to PEP-8 and is consistently formattedfprettifyor C/C++ code withclang-formatas applicable