test: import qiskit eagerly in conftest to fix test_simulation isolation failures#49
Merged
Merged
Conversation
test_simulation.py triggers qiskit's first import inside a
patch.dict("sys.modules", ...) block; on exit patch.dict deletes the
freshly-added qiskit submodule entries, leaving qiskit half-initialized
and failing its own tests when the file is run in isolation. Importing
qiskit once at collection time makes that first import a clean no-op.
Refs #47.
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.
Summary
Fixes the qiskit half of #47:
tests/test_simulation.pyfails 2 tests when the file is run in isolation (e.g.pytest tests/test_simulation.py), while passing in full-suite runs. (The full-suite segfault part of #47 was a separate native-reload bug, already fixed by #46.)Root cause
TestSimulationExecutorrunsexecute()insidepatch.dict("sys.modules", {"qristal": ..., "qristal.core": ...}). Inside that block,circuit.to_openqasm()triggers qiskit's first import (from qiskit import qasm2, qasm3). qiskit loads its standard-gate library lazily, so a batch ofqiskit.*submodules get added tosys.modulesduring the block — andpatch.dictdeletes them on exit because they weren't in the entry snapshot. qiskit is left half-initialized, so the next gate-class construction fails:In full-suite runs an earlier file imports qiskit cleanly first, so the in-block import is a no-op and the failure is masked — which is why it only shows up in isolation.
Fix
Import qiskit once at collection time in
tests/conftest.py, before any test can enter a patchedsys.modulescontext. Guarded withtry/except ImportErrorsince qiskit is an optional dependency.Verification
pytest tests/test_simulation.py(isolation): 2 failed → 39 passedpytest tests/(full suite): 414 passed, 16 skipped, exit 0 (unchanged; no regression)Closes #47.
Note
Low Risk
Test-only collection-time import with optional-dependency guard; no production or security impact.
Overview
Fixes isolated failures in
tests/test_simulation.py(issue #47) by eagerly importingqiskitandqasm2/qasm3at pytest collection time intests/conftest.py.When
SimulationExecutortests run insidepatch.dict("sys.modules", ...), a first-time lazy qiskit import can add submodules thatpatch.dictremoves on exit, leaving qiskit half-initialized and causing gate/Instructionerrors on later use. Full-suite runs often mask this because another test imports qiskit first.The new imports are wrapped in
try/except ImportErrorso optional qiskit absence does not break collection.Reviewed by Cursor Bugbot for commit 382ce45. Bugbot is set up for automated code reviews on this repo. Configure here.