-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide deprecation warnings to the user.
- Loading branch information
Zack Singer
committed
Jul 1, 2024
1 parent
258cd0e
commit d51351f
Showing
2 changed files
with
41 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import os | ||
import functools | ||
|
||
|
||
class DeprecatedFunctionError(Exception): | ||
"""Custom exception for deprecated functions.""" | ||
|
||
pass | ||
|
||
|
||
def deprecated(new_func=None): | ||
""" | ||
Decorator to mark functions as deprecated. Raises an error if | ||
the 'ACK_DEPRECATED' environment variable is not set. Alerts the | ||
user to the replacement function if provided. | ||
""" | ||
|
||
def decorator(func): | ||
@functools.wraps(func) | ||
def wrapper(*args, **kwargs): | ||
if new_func: | ||
new_func_path = f"{new_func.__module__}.{new_func.__name__}" | ||
print( | ||
f"Warning: {func.__name__} is deprecated and is marked for removal. " | ||
f"Please use {new_func_path} instead." | ||
) | ||
if os.getenv('ACK_DEPRECATED') != 'true': | ||
raise DeprecatedFunctionError( | ||
f"Function {func.__name__} is deprecated. " | ||
"Set the environment variable 'ACK_DEPRECATED' to 'true' to acknowledge." | ||
) | ||
return func(*args, **kwargs) | ||
|
||
return wrapper | ||
|
||
return decorator |
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