-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathdeprecation.py
69 lines (51 loc) · 2 KB
/
deprecation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# This code is part of Qiskit.
#
# (C) Copyright IBM 2017.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.
"""Deprecation utilities"""
import functools
import warnings
def deprecate_arguments(kwarg_map):
"""Decorator to automatically alias deprecated argument names and warn upon use."""
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
if kwargs:
_rename_kwargs(func.__name__, kwargs, kwarg_map)
return func(*args, **kwargs)
return wrapper
return decorator
def deprecate_function(msg, stacklevel=2):
"""Emit a warning prior to calling decorated function.
Args:
msg (str): Warning message to emit.
stacklevel (int): The warning stackevel to use, defaults to 2.
Returns:
Callable: The decorated, deprecated callable.
"""
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
warnings.warn(msg, DeprecationWarning, stacklevel=stacklevel)
return func(*args, **kwargs)
return wrapper
return decorator
def _rename_kwargs(func_name, kwargs, kwarg_map):
for old_arg, new_arg in kwarg_map.items():
if old_arg in kwargs:
if new_arg in kwargs:
raise TypeError(f"{func_name} received both {new_arg} and {old_arg} (deprecated).")
warnings.warn(
"{} keyword argument {} is deprecated and "
"replaced with {}.".format(func_name, old_arg, new_arg),
DeprecationWarning,
stacklevel=3,
)
kwargs[new_arg] = kwargs.pop(old_arg)