From deb850d253720038fa4dfb71d2b010b83c146148 Mon Sep 17 00:00:00 2001 From: Jake Lishman Date: Mon, 18 Apr 2022 22:55:50 -0400 Subject: [PATCH] Deprecate old paths for module-availability testers The canonical location for all of these is `qiskit.utils.optionals`, along with all the other import testers. Historically these objects were available from `qiskit.visualization` just because they were reused in several places within that module, and now a few downstream packages have begun importing from there. The alternate path was made available in Terra 0.20. --- qiskit/visualization/__init__.py | 25 ++++++++++++++++--- ...e-old-optional-paths-982466a6336e4794.yaml | 9 +++++++ 2 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 releasenotes/notes/deprecate-old-optional-paths-982466a6336e4794.yaml diff --git a/qiskit/visualization/__init__.py b/qiskit/visualization/__init__.py index 41385caa1bbb..0ea3e9143828 100644 --- a/qiskit/visualization/__init__.py +++ b/qiskit/visualization/__init__.py @@ -114,6 +114,7 @@ import os import sys +import warnings from qiskit.visualization.counts_visualization import plot_histogram from qiskit.visualization.state_visualization import ( @@ -127,9 +128,6 @@ from qiskit.visualization.transition_visualization import visualize_transition from qiskit.visualization.array import array_to_latex -# NOTE (Jake Lishman, 2022-01-12): for backwards compatibility. Deprecate these paths in Terra 0.21. -from qiskit.utils.optionals import HAS_MATPLOTLIB, HAS_PYLATEX, HAS_PIL, HAS_PDFTOCAIRO - from .circuit_visualization import circuit_drawer from .dag_visualization import dag_drawer from .exceptions import VisualizationError @@ -140,3 +138,24 @@ from .pulse_visualization import pulse_drawer from .pulse_v2 import draw as pulse_drawer_v2 from .timeline import draw as timeline_drawer + +_DEPRECATED_NAMES = { + "HAS_MATPLOTLIB", + "HAS_PYLATEX", + "HAS_PIL", + "HAS_PDFTOCAIRO", +} + + +def __getattr__(name): + if name in _DEPRECATED_NAMES: + from qiskit.utils import optionals + + warnings.warn( + f"Accessing '{name}' from '{__name__}' is deprecated since Qiskit Terra 0.21 " + "and will be removed in a future release. Use 'qiskit.utils.optionals' instead.", + DeprecationWarning, + stacklevel=2, + ) + return getattr(optionals, name) + raise AttributeError(f"module '{__name__}' has no attribute '{name}'") diff --git a/releasenotes/notes/deprecate-old-optional-paths-982466a6336e4794.yaml b/releasenotes/notes/deprecate-old-optional-paths-982466a6336e4794.yaml new file mode 100644 index 000000000000..ee009af6fc9c --- /dev/null +++ b/releasenotes/notes/deprecate-old-optional-paths-982466a6336e4794.yaml @@ -0,0 +1,9 @@ +--- +deprecations: + - | + Accessing several old toggles (``HAS_MATPLOTLIB``, ``HAS_PDFTOCAIRO``, + ``HAS_PYLATEX`` and ``HAS_PIL``) from the :mod:`qiskit.visualization` module + is now deprecated, and these import paths will be removed in a future + version of Qiskit Terra. The same objects should instead be accessed + through :mod:`qiskit.utils.optionals`, which contains testers for almost all + of Terra's optional dependencies.