Skip to content

Commit

Permalink
reno
Browse files Browse the repository at this point in the history
  • Loading branch information
1ucian0 committed Nov 15, 2024
1 parent 20c37a4 commit fee0568
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 108 deletions.
108 changes: 0 additions & 108 deletions qiskit/utils/deprecation.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,114 +205,6 @@ def wrapper(*args, **kwargs):
return decorator


def deprecate_arguments(
kwarg_map: dict[str, str | None],
category: Type[Warning] = DeprecationWarning,
*,
since: str | None = None,
):
"""Deprecated. Instead, use `@deprecate_arg`.
Args:
kwarg_map: A dictionary of the old argument name to the new name.
category: Usually either DeprecationWarning or PendingDeprecationWarning.
since: The version the deprecation started at. Only Optional for backwards
compatibility - this should always be set. If the deprecation is pending, set
the version to when that started; but later, when switching from pending to
deprecated, update `since` to the new version.
Returns:
Callable: The decorated callable.
"""

def decorator(func):
func_name = func.__qualname__
old_kwarg_to_msg = {}
for old_arg, new_arg in kwarg_map.items():
msg_suffix = (
"will in the future be removed." if new_arg is None else f"replaced with {new_arg}."
)
old_kwarg_to_msg[old_arg] = (
f"{func_name} keyword argument {old_arg} is deprecated and {msg_suffix}"
)

@functools.wraps(func)
def wrapper(*args, **kwargs):
for old, new in kwarg_map.items():
_maybe_warn_and_rename_kwarg(
args,
kwargs,
func_name=func_name,
original_func_co_varnames=wrapper.__original_func_co_varnames,
old_arg_name=old,
new_alias=new,
warning_msg=old_kwarg_to_msg[old],
category=category,
predicate=None,
)
return func(*args, **kwargs)

# When decorators get called repeatedly, `func` refers to the result of the prior
# decorator, not the original underlying function. This trick allows us to record the
# original function's variable names regardless of how many decorators are used.
#
# If it's the very first decorator call, we also check that *args and **kwargs are not used.
if hasattr(func, "__original_func_co_varnames"):
wrapper.__original_func_co_varnames = func.__original_func_co_varnames
else:
wrapper.__original_func_co_varnames = func.__code__.co_varnames
param_kinds = {param.kind for param in inspect.signature(func).parameters.values()}
if inspect.Parameter.VAR_POSITIONAL in param_kinds:
raise ValueError(
"@deprecate_arg cannot be used with functions that take variable *args. Use "
"warnings.warn() directly instead."
)

for msg in old_kwarg_to_msg.values():
add_deprecation_to_docstring(
wrapper, msg, since=since, pending=issubclass(category, PendingDeprecationWarning)
)
return wrapper

return decorator


def deprecate_function(
msg: str,
stacklevel: int = 2,
category: Type[Warning] = DeprecationWarning,
*,
since: str | None = None,
):
"""Deprecated. Instead, use `@deprecate_func`.
Args:
msg: Warning message to emit.
stacklevel: The warning stacklevel to use, defaults to 2.
category: Usually either DeprecationWarning or PendingDeprecationWarning.
since: The version the deprecation started at. Only Optional for backwards
compatibility - this should always be set. If the deprecation is pending, set
the version to when that started; but later, when switching from pending to
deprecated, update `since` to the new version.
Returns:
Callable: The decorated, deprecated callable.
"""

def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
warnings.warn(msg, category=category, stacklevel=stacklevel)
return func(*args, **kwargs)

add_deprecation_to_docstring(
wrapper, msg, since=since, pending=issubclass(category, PendingDeprecationWarning)
)
return wrapper

return decorator


def _maybe_warn_and_rename_kwarg(
args: tuple[Any, ...],
kwargs: dict[str, Any],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
upgrade_misc:
- |
The ``deprecate_function`` and ``deprecate_arguments`` decorators had been deprecated since 0.24, released
on May 2023.
Current :func:`deprecate_func`` replaces ``@deprecate_function`` and current
:func:`deprecate_arg` replaces ``@deprecate_arguments``.

0 comments on commit fee0568

Please sign in to comment.