Skip to content

Commit

Permalink
Fix warning on __qiskit_version__ (Qiskit#10686)
Browse files Browse the repository at this point in the history
* Fix warning on `__qiskit_version__`

The warning emitted by the `QiskitVersion` class that backs the
`__qiskit_version__` variable was previously set to warn on
instantiation, not usage.  This caused the warning to trigger during
library import, rather than at the site of usage.

* Add warning assertion to test
  • Loading branch information
jakelishman authored Aug 22, 2023
1 parent b039687 commit 4ab2ef8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
16 changes: 9 additions & 7 deletions qiskit/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,21 +93,23 @@ class QiskitVersion(Mapping):
__slots__ = ["_version_dict", "_loaded"]

def __init__(self):
self._version_dict = {
"qiskit-terra": __version__,
"qiskit": None,
}
self._loaded = False

def _load_versions(self):
warnings.warn(
"qiskit.__qiskit_version__ is deprecated since "
"Qiskit Terra 0.25.0, and will be removed 3 months or more later. "
"Instead, you should use qiskit.__version__. The other packages listed in "
"Instead, you should use qiskit.__version__. The other packages listed in the"
"former qiskit.__qiskit_version__ have their own __version__ module level dunder, "
"as standard in PEP 8.",
category=DeprecationWarning,
stacklevel=3,
)
self._version_dict = {
"qiskit-terra": __version__,
"qiskit": None,
}
self._loaded = False

def _load_versions(self):
from importlib.metadata import version

try:
Expand Down
3 changes: 2 additions & 1 deletion test/python/test_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ class TestVersion(QiskitTestCase):

def test_qiskit_version(self):
"""Test qiskit-version sets the correct version for terra."""
self.assertEqual(__version__, __qiskit_version__["qiskit-terra"])
with self.assertWarnsRegex(DeprecationWarning, "__qiskit_version__"):
self.assertEqual(__version__, __qiskit_version__["qiskit-terra"])

0 comments on commit 4ab2ef8

Please sign in to comment.