From 85a5bc273c3d4c73cec42ecc6eabeca0f66ecce1 Mon Sep 17 00:00:00 2001 From: Jake Lishman Date: Mon, 1 Nov 2021 14:34:33 +0000 Subject: [PATCH 1/2] Remove overrides of standard warning behaviour Previously, qiskit attempted to override the deprecation warning filters for internal qiskit code as soon as it was imported, so that they were shown with the default filters. This isn't something we should be doing; it's a user's choice, and it's unexpected that we would override their settings in order to violate the normal Python behaviour. It also had no effect on the tests, since test runners hook into the warnings control at various times during the execution. If we do want to do something similar, it would have been better to use `warnings.filterwarnings`, instead of a private function. The other change is to remove the "only warn once" filter from `qiskit.utils.deprecate_function`. It's the default behaviour of Python to only show one warning from each `(category, message, module, line)` set, so users already shouldn't be too overwhelmed (and they should change their code if they're seeing them anyway). Within the test suite, it's important that all warnings are raised all the time, so we can assert that deprecated calls warn and nothing else does; if things only warn once, then the execution order of the tests matters for correctness. This should have no effect on the test suite as it stands right now, because even one warning is enough to fail the entire run, and the filtering behaviour was overridden by the runner anyway. --- qiskit/utils/__init__.py | 1 - qiskit/utils/deprecation.py | 41 +------------------------------------ 2 files changed, 1 insertion(+), 41 deletions(-) diff --git a/qiskit/utils/__init__.py b/qiskit/utils/__init__.py index a0992fa2745c..3aa70917c1ac 100644 --- a/qiskit/utils/__init__.py +++ b/qiskit/utils/__init__.py @@ -58,7 +58,6 @@ """ from .quantum_instance import QuantumInstance -from .deprecation import _filter_deprecation_warnings from .deprecation import deprecate_arguments from .deprecation import deprecate_function from .multiprocessing import local_hardware_info diff --git a/qiskit/utils/deprecation.py b/qiskit/utils/deprecation.py index 14938bc88dbf..1a73803ab4f3 100644 --- a/qiskit/utils/deprecation.py +++ b/qiskit/utils/deprecation.py @@ -13,44 +13,9 @@ """Deprecation utilities""" import functools -import re import warnings -def _filter_deprecation_warnings(): - """Apply filters to deprecation warnings. - - Force the `DeprecationWarning` warnings to be displayed for the qiskit - module, overriding the system configuration as they are ignored by default - [1] for end-users. Additionally, silence the `ChangedInMarshmallow3Warning` - messages. - - TODO: on Python 3.7, this might not be needed due to PEP-0565 [2]. - - [1] https://docs.python.org/3/library/warnings.html#default-warning-filters - [2] https://www.python.org/dev/peps/pep-0565/ - """ - deprecation_filter = ( - "default", - None, - DeprecationWarning, - re.compile(r"^qiskit\.*", re.UNICODE), - 0, - ) - - # Instead of using warnings.simple_filter() directly, the internal - # _add_filter() function is used for being able to match against the - # module. - try: - warnings._add_filter(*deprecation_filter, append=False) - except AttributeError: - # ._add_filter is internal and not available in some Python versions. - pass - - -_filter_deprecation_warnings() - - def deprecate_arguments(kwarg_map): """Decorator to automatically alias deprecated argument names and warn upon use.""" @@ -80,13 +45,9 @@ def deprecate_function(msg, stacklevel=2): def decorator(func): @functools.wraps(func) def wrapper(*args, **kwargs): - # warn only once - if not wrapper._warned: - warnings.warn(msg, DeprecationWarning, stacklevel=stacklevel) - wrapper._warned = True + warnings.warn(msg, DeprecationWarning, stacklevel=stacklevel) return func(*args, **kwargs) - wrapper._warned = False return wrapper return decorator From 2d7718f66e852534581af57bb2aca95faaee77e5 Mon Sep 17 00:00:00 2001 From: Jake Lishman Date: Tue, 2 Nov 2021 18:31:11 +0000 Subject: [PATCH 2/2] Add upgrade release notes --- ...emove-manual-warning-filters-028646b73bb86860.yaml | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 releasenotes/notes/remove-manual-warning-filters-028646b73bb86860.yaml diff --git a/releasenotes/notes/remove-manual-warning-filters-028646b73bb86860.yaml b/releasenotes/notes/remove-manual-warning-filters-028646b73bb86860.yaml new file mode 100644 index 000000000000..eb25d1a20336 --- /dev/null +++ b/releasenotes/notes/remove-manual-warning-filters-028646b73bb86860.yaml @@ -0,0 +1,11 @@ +--- +upgrade: + - | + An internal filter override that caused all Qiskit deprecation warnings to be displayed has been removed. + This means that the behaviour will now revert to the standard Python behaviour for deprecations; you should only see a ``DeprecationWarning`` if it was triggered by code in the main script file, interpreter session or Jupyter notebook. + The user will no longer be blamed with a warning if internal Qiskit functions call deprecated behaviour. + If you write libraries, you should occasionally run with the default warning filters disabled, or have tests which always run with them disabled. + - | + Certain warnings used to be only issued once, even if triggered from multiple places. + This behaviour has been removed, so it is possible that if you call deprecated functions, you may see more warnings than you did before. + You should change any deprecated function calls to the suggested versions, because the deprecated forms will be removed in future Qiskit releases.