Skip to content

Fix mpi4py import in test_optProb - #488

Merged
A-CGray merged 1 commit into
mainfrom
bugfix-mpi4py-mpi-submodule-import
Aug 24, 2026
Merged

Fix mpi4py import in test_optProb#488
A-CGray merged 1 commit into
mainfrom
bugfix-mpi4py-mpi-submodule-import

Conversation

@eirikurj

Copy link
Copy Markdown
Collaborator

Purpose

The recently added tests are all failing with the following error in my development environment.

The following tests failed:
test_optProb.py:TestScaling.test_finalize_scale
test_optProb.py:TestScaling.test_infinite_bounds_not_scaled
test_optProb.py:TestScaling.test_mapConJac_roundtrip
test_optProb.py:TestScaling.test_mapCon_roundtrip
test_optProb.py:TestScaling.test_mapObjGrad
test_optProb.py:TestScaling.test_mapObj_roundtrip
test_optProb.py:TestScaling.test_mapX_roundtrip
test_optProb.py:TestScaling.test_scale_and_offset

  File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 999, in exec_module
  File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
  File "/home/***/repos/pyoptsparse/tests/test_optProb.py", line 15, in <module>
    comm = mpi4py.MPI.COMM_WORLD
           ^^^^^^^^^^
AttributeError: module 'mpi4py' has no attribute 'MPI'

This is because import mpi4py imports the package and does not bind the MPI extension submodule as an attribute. Accessing mpi4py.MPI therefore raises AttributeError, which the except ImportError does not catch, so the module fails to import instead of falling back to comm = 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. Under testflo -i, tests in a class with N_PROCS are launched via testflo/mpirun.py, which imports the submodule at module level before importing the test module, so this did not come up. Test classes without N_PROCS like this one are launched via testflo/isolatedrun.py, which does not, so the eight TestScaling tests added in #486 fail at import in any environment with mpi4py installed.

This PR switches to from mpi4py import MPI which makes the guard behave as intended regardless of import order.

Expected time until merged

ASAP

Type of change

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (non-backwards-compatible fix or feature)
  • Code style update (formatting, renaming)
  • Refactoring (no functional changes, no API changes)
  • Documentation update
  • Maintenance update
  • Other (please describe)

Testing

Checklist

  • I have run ruff check and ruff format to make sure the Python code adheres to PEP-8 and is consistently formatted
  • I have formatted the Fortran code with fprettify or C/C++ code with clang-format as applicable
  • I have run unit and regression tests which pass locally with my changes
  • I have added new tests that prove my fix is effective or that my feature works
  • I have added necessary documentation

@eirikurj
eirikurj requested a review from marcomangano as a code owner August 24, 2026 15:50
@eirikurj
eirikurj requested a review from A-CGray August 24, 2026 15:50
@codecov

codecov Bot commented Aug 24, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 54.22%. Comparing base (f0393e2) to head (7eb5eae).

Files with missing lines Patch % Lines
tests/test_optProb.py 0.00% 2 Missing ⚠️
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ewu63 ewu63 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks Eirikur

@A-CGray
A-CGray merged commit 9ee4f75 into main Aug 24, 2026
13 of 14 checks passed
@A-CGray
A-CGray deleted the bugfix-mpi4py-mpi-submodule-import branch August 24, 2026 18:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants