|
| 1 | +import functools |
| 2 | +import warnings |
| 3 | +from .exceptions import DeprecatedFunctionWarning |
| 4 | + |
| 5 | + |
| 6 | +def deprecate_function(func=None, version=None, msg=None): |
| 7 | + """ |
| 8 | + A decorator to mark a function as deprecated. |
| 9 | +
|
| 10 | + Parameters: |
| 11 | + - func: The function to decorate. If no function is provided, the decorator will be used as a factory. |
| 12 | + - version: The version in which the function was deprecated. |
| 13 | + - msg: Custom message to display alongside the deprecation warning. |
| 14 | +
|
| 15 | + If no message is provided, a default message is used. |
| 16 | + """ |
| 17 | + |
| 18 | + def decorator(func): |
| 19 | + @functools.wraps(func) |
| 20 | + def wrapper(*args, **kwargs): |
| 21 | + # Construct the warning message |
| 22 | + message = ( |
| 23 | + msg |
| 24 | + or f"Function '{func.__name__}' is deprecated and will be removed in future versions." |
| 25 | + ) |
| 26 | + if version: |
| 27 | + message += f" Deprecated since version {version}." |
| 28 | + |
| 29 | + # Raise the deprecation warning |
| 30 | + warnings.warn(message, category=DeprecatedFunctionWarning, stacklevel=2) |
| 31 | + |
| 32 | + return func(*args, **kwargs) |
| 33 | + |
| 34 | + return wrapper |
| 35 | + |
| 36 | + # If no function is passed (when used as a decorator with parameters) |
| 37 | + if func is None: |
| 38 | + return decorator |
| 39 | + else: |
| 40 | + return decorator(func) |
| 41 | + |
| 42 | + |
| 43 | +def deprecate_multiple_renamedParameters(param_dict: dict): |
| 44 | + """ |
| 45 | + Decorator to handle deprecated parameter names dynamically, issuing warnings when old |
| 46 | + parameter names are found and replacing them with the new ones provided in the param_dict. |
| 47 | + param_dict is a dictionary mapping old parameter names to new parameter names. |
| 48 | + """ |
| 49 | + |
| 50 | + def decorator(func): |
| 51 | + @functools.wraps(func) |
| 52 | + def wrapper(self, values, *args, **kwargs): |
| 53 | + # Iterate over the old-to-new parameter mapping |
| 54 | + for old_param, new_param in param_dict.items(): |
| 55 | + if old_param in values: |
| 56 | + # Issue a deprecation warning and update the parameter |
| 57 | + warnings.warn( |
| 58 | + f"The parameter '{old_param}' has been renamed to '{new_param}'", |
| 59 | + DeprecationWarning, |
| 60 | + stacklevel=2, |
| 61 | + ) |
| 62 | + values[new_param] = values.pop(old_param) |
| 63 | + # Call the original function with the updated values |
| 64 | + return func(self, values, *args, **kwargs) |
| 65 | + |
| 66 | + return wrapper |
| 67 | + |
| 68 | + return decorator |
| 69 | + |
| 70 | + |
| 71 | +def deprecated_params(param_map): |
| 72 | + """ |
| 73 | + A decorator to handle deprecated parameters in a function. |
| 74 | +
|
| 75 | + Parameters |
| 76 | + ---------- |
| 77 | + param_map : dict |
| 78 | + A dictionary mapping deprecated parameter names to a tuple containing the |
| 79 | + new parameter name (or None if it's removed) and a message explaining the deprecation. |
| 80 | +
|
| 81 | + Example |
| 82 | + ------- |
| 83 | + @handle_deprecated_params({ |
| 84 | + 'npts': ('t_eval', "The 'npts' parameter is deprecated, use 't_eval' instead.") |
| 85 | + }) |
| 86 | + def step(...): |
| 87 | + ... |
| 88 | + """ |
| 89 | + |
| 90 | + def decorator(func): |
| 91 | + @functools.wraps(func) |
| 92 | + def wrapper(*args, **kwargs): |
| 93 | + for deprecated_param, (new_param, message) in param_map.items(): |
| 94 | + if deprecated_param in kwargs: |
| 95 | + warnings.warn(message, DeprecationWarning, stacklevel=2) |
| 96 | + if new_param: |
| 97 | + kwargs[new_param] = kwargs.pop(deprecated_param) |
| 98 | + return func(*args, **kwargs) |
| 99 | + |
| 100 | + return wrapper |
| 101 | + |
| 102 | + return decorator |
0 commit comments