From b7262542001540a7e26712c9595d3579dbedc817 Mon Sep 17 00:00:00 2001 From: Jake Lishman Date: Tue, 22 Aug 2023 15:06:49 +0100 Subject: [PATCH] Fix warning on `__qiskit_version__` (#10686) * 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 --- qiskit/version.py | 16 +++++++++------- test/python/test_version.py | 3 ++- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/qiskit/version.py b/qiskit/version.py index 1f38b39a0e29..7be28ae51bcd 100644 --- a/qiskit/version.py +++ b/qiskit/version.py @@ -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: diff --git a/test/python/test_version.py b/test/python/test_version.py index 1fdb541074f8..c64788b34b3f 100644 --- a/test/python/test_version.py +++ b/test/python/test_version.py @@ -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"])