Skip to content

Commit 6ce5be4

Browse files
1ucian0ElePT
authored andcommitted
Remove deprecate_function and deprecate_arguments decorators (deprecated in 0.24) (#13448)
* remove deprecated_function and deprecated_argument decorators * reno * Update releasenotes/notes/deprecate_arguments_and_deprecate_function-5e19f6f049fa489c.yaml Co-authored-by: Elena Peña Tapia <[email protected]> --------- Co-authored-by: Elena Peña Tapia <[email protected]> (cherry picked from commit 1ae287b)
1 parent 896d1c8 commit 6ce5be4

File tree

4 files changed

+7
-208
lines changed

4 files changed

+7
-208
lines changed

qiskit/utils/__init__.py

-6
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@
2323
2424
.. autofunction:: add_deprecation_to_docstring
2525
.. autofunction:: deprecate_arg
26-
.. autofunction:: deprecate_arguments
2726
.. autofunction:: deprecate_func
28-
.. autofunction:: deprecate_function
2927
3028
SI unit conversion
3129
==================
@@ -58,9 +56,7 @@
5856
from .deprecation import (
5957
add_deprecation_to_docstring,
6058
deprecate_arg,
61-
deprecate_arguments,
6259
deprecate_func,
63-
deprecate_function,
6460
)
6561
from .multiprocessing import local_hardware_info
6662
from .multiprocessing import is_main_process
@@ -78,9 +74,7 @@
7874
"LazySubprocessTester",
7975
"add_deprecation_to_docstring",
8076
"deprecate_arg",
81-
"deprecate_arguments",
8277
"deprecate_func",
83-
"deprecate_function",
8478
"local_hardware_info",
8579
"is_main_process",
8680
"apply_prefix",

qiskit/utils/deprecation.py

-108
Original file line numberDiff line numberDiff line change
@@ -205,114 +205,6 @@ def wrapper(*args, **kwargs):
205205
return decorator
206206

207207

208-
def deprecate_arguments(
209-
kwarg_map: dict[str, str | None],
210-
category: Type[Warning] = DeprecationWarning,
211-
*,
212-
since: str | None = None,
213-
):
214-
"""Deprecated. Instead, use `@deprecate_arg`.
215-
216-
Args:
217-
kwarg_map: A dictionary of the old argument name to the new name.
218-
category: Usually either DeprecationWarning or PendingDeprecationWarning.
219-
since: The version the deprecation started at. Only Optional for backwards
220-
compatibility - this should always be set. If the deprecation is pending, set
221-
the version to when that started; but later, when switching from pending to
222-
deprecated, update `since` to the new version.
223-
224-
Returns:
225-
Callable: The decorated callable.
226-
"""
227-
228-
def decorator(func):
229-
func_name = func.__qualname__
230-
old_kwarg_to_msg = {}
231-
for old_arg, new_arg in kwarg_map.items():
232-
msg_suffix = (
233-
"will in the future be removed." if new_arg is None else f"replaced with {new_arg}."
234-
)
235-
old_kwarg_to_msg[old_arg] = (
236-
f"{func_name} keyword argument {old_arg} is deprecated and {msg_suffix}"
237-
)
238-
239-
@functools.wraps(func)
240-
def wrapper(*args, **kwargs):
241-
for old, new in kwarg_map.items():
242-
_maybe_warn_and_rename_kwarg(
243-
args,
244-
kwargs,
245-
func_name=func_name,
246-
original_func_co_varnames=wrapper.__original_func_co_varnames,
247-
old_arg_name=old,
248-
new_alias=new,
249-
warning_msg=old_kwarg_to_msg[old],
250-
category=category,
251-
predicate=None,
252-
)
253-
return func(*args, **kwargs)
254-
255-
# When decorators get called repeatedly, `func` refers to the result of the prior
256-
# decorator, not the original underlying function. This trick allows us to record the
257-
# original function's variable names regardless of how many decorators are used.
258-
#
259-
# If it's the very first decorator call, we also check that *args and **kwargs are not used.
260-
if hasattr(func, "__original_func_co_varnames"):
261-
wrapper.__original_func_co_varnames = func.__original_func_co_varnames
262-
else:
263-
wrapper.__original_func_co_varnames = func.__code__.co_varnames
264-
param_kinds = {param.kind for param in inspect.signature(func).parameters.values()}
265-
if inspect.Parameter.VAR_POSITIONAL in param_kinds:
266-
raise ValueError(
267-
"@deprecate_arg cannot be used with functions that take variable *args. Use "
268-
"warnings.warn() directly instead."
269-
)
270-
271-
for msg in old_kwarg_to_msg.values():
272-
add_deprecation_to_docstring(
273-
wrapper, msg, since=since, pending=issubclass(category, PendingDeprecationWarning)
274-
)
275-
return wrapper
276-
277-
return decorator
278-
279-
280-
def deprecate_function(
281-
msg: str,
282-
stacklevel: int = 2,
283-
category: Type[Warning] = DeprecationWarning,
284-
*,
285-
since: str | None = None,
286-
):
287-
"""Deprecated. Instead, use `@deprecate_func`.
288-
289-
Args:
290-
msg: Warning message to emit.
291-
stacklevel: The warning stacklevel to use, defaults to 2.
292-
category: Usually either DeprecationWarning or PendingDeprecationWarning.
293-
since: The version the deprecation started at. Only Optional for backwards
294-
compatibility - this should always be set. If the deprecation is pending, set
295-
the version to when that started; but later, when switching from pending to
296-
deprecated, update `since` to the new version.
297-
298-
Returns:
299-
Callable: The decorated, deprecated callable.
300-
"""
301-
302-
def decorator(func):
303-
@functools.wraps(func)
304-
def wrapper(*args, **kwargs):
305-
warnings.warn(msg, category=category, stacklevel=stacklevel)
306-
return func(*args, **kwargs)
307-
308-
add_deprecation_to_docstring(
309-
wrapper, msg, since=since, pending=issubclass(category, PendingDeprecationWarning)
310-
)
311-
return wrapper
312-
313-
return decorator
314-
315-
316208
def _maybe_warn_and_rename_kwarg(
317209
args: tuple[Any, ...],
318210
kwargs: dict[str, Any],
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
upgrade_misc:
3+
- |
4+
The ``deprecate_function`` and ``deprecate_arguments`` decorators had been deprecated since 0.24, released
5+
on May 2023, and have been removed in 2.0.
6+
Current :func:`deprecate_func`` replaces ``@deprecate_function`` and current
7+
:func:`deprecate_arg` replaces ``@deprecate_arguments``.

test/python/utils/test_deprecation.py

-94
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@
2020
from qiskit.utils.deprecation import (
2121
add_deprecation_to_docstring,
2222
deprecate_arg,
23-
deprecate_arguments,
2423
deprecate_func,
25-
deprecate_function,
2624
)
2725
from test import QiskitTestCase # pylint: disable=wrong-import-order
2826

@@ -164,51 +162,6 @@ def my_func() -> None:
164162
),
165163
)
166164

167-
def test_deprecate_arguments_docstring(self) -> None:
168-
"""Test that `@deprecate_arguments` adds the correct message to the docstring."""
169-
170-
@deprecate_arguments(
171-
{"old_arg1": "new_arg1", "old_arg2": None},
172-
category=PendingDeprecationWarning,
173-
since="9.99",
174-
)
175-
def my_func() -> None:
176-
pass
177-
178-
self.assertEqual(
179-
my_func.__doc__,
180-
dedent(
181-
f"""\
182-
183-
.. deprecated:: 9.99_pending
184-
{my_func.__qualname__} keyword argument old_arg1 is deprecated and replaced with \
185-
new_arg1.
186-
187-
.. deprecated:: 9.99_pending
188-
{my_func.__qualname__} keyword argument old_arg2 is deprecated and will in the \
189-
future be removed.
190-
"""
191-
),
192-
)
193-
194-
def test_deprecate_function_docstring(self) -> None:
195-
"""Test that `@deprecate_function` adds the correct message to the docstring."""
196-
197-
@deprecate_function("Stop using my_func!", since="9.99")
198-
def my_func() -> None:
199-
pass
200-
201-
self.assertEqual(
202-
my_func.__doc__,
203-
dedent(
204-
"""\
205-
206-
.. deprecated:: 9.99
207-
Stop using my_func!
208-
"""
209-
),
210-
)
211-
212165
def test_deprecate_func_runtime_warning(self) -> None:
213166
"""Test that `@deprecate_func` warns whenever the function is used."""
214167

@@ -313,53 +266,6 @@ def my_func2(my_kwarg: int | None = None, **kwargs) -> None:
313266
with self.assertWarnsRegex(DeprecationWarning, "my_kwarg"):
314267
my_func2(my_kwarg=5, another_arg=0, yet_another=0)
315268

316-
def test_deprecate_arguments_runtime_warning(self) -> None:
317-
"""Test that `@deprecate_arguments` warns whenever the arguments are used.
318-
319-
Also check that old arguments are passed in as their new alias.
320-
"""
321-
322-
@deprecate_arguments({"arg1": "new_arg1", "arg2": None}, since="9.99")
323-
def my_func(arg1: str = "a", arg2: str = "a", new_arg1: str | None = None) -> None:
324-
del arg2
325-
# If the old arg was set, we should set its `new_alias` to that value.
326-
if arg1 != "a":
327-
self.assertEqual(new_arg1, "z")
328-
if new_arg1 is not None:
329-
self.assertEqual(new_arg1, "z")
330-
331-
# No warnings if no deprecated args used.
332-
my_func()
333-
my_func(new_arg1="z")
334-
335-
# Warn if argument is specified, regardless of positional vs kwarg.
336-
with self.assertWarnsRegex(DeprecationWarning, "arg1"):
337-
my_func("z")
338-
with self.assertWarnsRegex(DeprecationWarning, "arg1"):
339-
my_func(arg1="z")
340-
with self.assertWarnsRegex(DeprecationWarning, "arg2"):
341-
my_func("z", "z")
342-
with self.assertWarnsRegex(DeprecationWarning, "arg2"):
343-
my_func(arg2="z")
344-
345-
# Error if new_alias specified at the same time as old argument name.
346-
with self.assertRaises(TypeError):
347-
my_func("a", new_arg1="z")
348-
with self.assertRaises(TypeError):
349-
my_func(arg1="a", new_arg1="z")
350-
with self.assertRaises(TypeError):
351-
my_func("a", "a", "z")
352-
353-
def test_deprecate_function_runtime_warning(self) -> None:
354-
"""Test that `@deprecate_function` warns whenever the function is used."""
355-
356-
@deprecate_function("Stop using my_func!", since="9.99")
357-
def my_func() -> None:
358-
pass
359-
360-
with self.assertWarnsRegex(DeprecationWarning, "Stop using my_func!"):
361-
my_func()
362-
363269

364270
class AddDeprecationDocstringTest(QiskitTestCase):
365271
"""Test that we correctly insert the deprecation directive at the right location.

0 commit comments

Comments
 (0)