Merge pull request #960 from roboflow/small_deprecated_parameter_cleanup

update `deprecated_parameter` docs
This commit is contained in:
Piotr Skalski authored and GitHub committed 2024-02-29 10:19:36 +01:00
commit af03010a07
2 files changed
+30 -32

No files matched your search

+3 -4
View File
@@ -33,10 +33,9 @@ class PolygonZone:
old_parameter="triggering_position",
new_parameter="triggering_anchors",
map_function=lambda x: [x],
warning_message="Warning: '{old_parameter}' in '{function_name}' "
"is deprecated and will be remove in "
"'{removal_sv_version}: use '{new_parameter}' instead.",
removal_sv_version="supervision-0.23.0",
warning_message="`{old_parameter}` in `{function_name}` is deprecated and will "
"be remove in `supervision-0.23.0`. Use '{new_parameter}' "
"instead.",
)
def __init__(
self,
+27 -28
View File
@@ -7,54 +7,53 @@ def deprecated_parameter(
old_parameter: str,
new_parameter: str,
map_function: Callable = lambda x: x,
warning_message: str = "Warning: '{old_parameter}' in '{function_name}' "
"is deprecated: use '{new_parameter}' instead.",
warning_message: str = "Warning: '{old_parameter}' in '{function_name}' is "
"deprecated: use '{new_parameter}' instead.",
**message_kwargs,
):
"""
A decorator to mark a function's parameter as deprecated
and issue a warning when used.
A decorator to mark a function's parameter as deprecated and issue a warning when
used.
Parameters:
- old_parameter (str): The name of the deprecated parameter.
- new_parameter (str): The name of the parameter that should be used instead.
- map_function (Callable, optional): A function used to map the value of the old
parameter to the new parameter. Defaults to the identity function.
- warn_message (str, optional): The warning message to be displayed when the
deprecated parameter is used. Defaults to a generic warning message with
placeholders for the old parameter, new parameter, and function name.
- **message_kwargs: Additional keyword arguments that can be used to customize
the warning message.
old_parameter (str): The name of the deprecated parameter.
new_parameter (str): The name of the parameter that should be used instead.
map_function (Callable, optional): A function used to map the value of the old
parameter to the new parameter. Defaults to the identity function.
warning_message (str, optional): The warning message to be displayed when the
deprecated parameter is used. Defaults to a generic warning message with
placeholders for the old parameter, new parameter, and function name.
**message_kwargs: Additional keyword arguments that can be used to customize
the warning message.
Returns:
Callable: A decorator function that can be applied to mark a function's
parameter as deprecated.
Callable: A decorator function that can be applied to mark a function's
parameter as deprecated.
Usage Example:
```python
@deprecated_parameter(old_parameter="old_param", new_parameter="new_param")
def example_function(new_param):
print(f"Function called with new_param: {new_param}")
Examples:
```python
@deprecated_parameter(
old_parameter=<OLD_PARAMETER_NAME>,
new_parameter=<NEW_PARAMETER_NAME>
)
def example_function(<NEW_PARAMETER_NAME>):
pass
# When calling the function with the deprecated parameter:
example_function(old_param="deprecated_value")
```
This will trigger a deprecation warning and execute the function with the mapped
value of the deprecated parameter.
# call function using deprecated parameter
example_function(<OLD_PARAMETER_NAME>=<OLD_PARAMETER_VALUE>)
```
"""
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
if old_parameter in kwargs:
# In case of a method, display also the class name.
if args and hasattr(args[0], "__class__"):
class_name = args[0].__class__.__name__
function_name = f"{class_name}.{func.__name__}"
else:
function_name = func.__name__
# Display deprecation warning
warnings.warn(
message=warning_message.format(
function_name=function_name,
@@ -65,7 +64,7 @@ def deprecated_parameter(
category=DeprecationWarning,
stacklevel=2,
)
# Map old_param to new_param
kwargs[new_parameter] = map_function(kwargs.pop(old_parameter))
return func(*args, **kwargs)