-
-
Notifications
You must be signed in to change notification settings - Fork 573
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8b33589
commit 5024850
Showing
8 changed files
with
272 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .decorators import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import functools | ||
import warnings | ||
from .exceptions import DeprecatedFunctionWarning | ||
|
||
|
||
def deprecate_function(func=None, version=None, msg=None): | ||
""" | ||
A decorator to mark a function as deprecated. | ||
Parameters: | ||
- func: The function to decorate. If no function is provided, the decorator will be used as a factory. | ||
- version: The version in which the function was deprecated. | ||
- msg: Custom message to display alongside the deprecation warning. | ||
If no message is provided, a default message is used. | ||
""" | ||
|
||
def decorator(func): | ||
@functools.wraps(func) | ||
def wrapper(*args, **kwargs): | ||
# Construct the warning message | ||
message = ( | ||
msg | ||
or f"Function '{func.__name__}' is deprecated and will be removed in future versions." | ||
) | ||
if version: | ||
message += f" Deprecated since version {version}." | ||
|
||
# Raise the deprecation warning | ||
warnings.warn(message, category=DeprecatedFunctionWarning, stacklevel=2) | ||
|
||
return func(*args, **kwargs) | ||
|
||
return wrapper | ||
|
||
# If no function is passed (when used as a decorator with parameters) | ||
if func is None: | ||
return decorator | ||
else: | ||
return decorator(func) | ||
|
||
|
||
def deprecate_multiple_renamedParameters(param_dict: dict): | ||
""" | ||
Decorator to handle deprecated parameter names dynamically, issuing warnings when old | ||
parameter names are found and replacing them with the new ones provided in the param_dict. | ||
param_dict is a dictionary mapping old parameter names to new parameter names. | ||
""" | ||
|
||
def decorator(func): | ||
@functools.wraps(func) | ||
def wrapper(self, values, *args, **kwargs): | ||
# Iterate over the old-to-new parameter mapping | ||
for old_param, new_param in param_dict.items(): | ||
if old_param in values: | ||
# Issue a deprecation warning and update the parameter | ||
warnings.warn( | ||
f"The parameter '{old_param}' has been renamed to '{new_param}'", | ||
DeprecationWarning, | ||
stacklevel=2, | ||
) | ||
values[new_param] = values.pop(old_param) | ||
# Call the original function with the updated values | ||
return func(self, values, *args, **kwargs) | ||
|
||
return wrapper | ||
|
||
return decorator | ||
|
||
|
||
def deprecated_params(param_map): | ||
""" | ||
A decorator to handle deprecated parameters in a function. | ||
Parameters | ||
---------- | ||
param_map : dict | ||
A dictionary mapping deprecated parameter names to a tuple containing the | ||
new parameter name (or None if it's removed) and a message explaining the deprecation. | ||
Example | ||
------- | ||
@handle_deprecated_params({ | ||
'npts': ('t_eval', "The 'npts' parameter is deprecated, use 't_eval' instead.") | ||
}) | ||
def step(...): | ||
... | ||
""" | ||
|
||
def decorator(func): | ||
@functools.wraps(func) | ||
def wrapper(*args, **kwargs): | ||
for deprecated_param, (new_param, message) in param_map.items(): | ||
if deprecated_param in kwargs: | ||
warnings.warn(message, DeprecationWarning, stacklevel=2) | ||
if new_param: | ||
kwargs[new_param] = kwargs.pop(deprecated_param) | ||
return func(*args, **kwargs) | ||
|
||
return wrapper | ||
|
||
return decorator |
Oops, something went wrong.