Skip to content

Commit

Permalink
Added deprecation decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
antoninoLorenzo committed Aug 9, 2024
1 parent fda14a4 commit a7905fd
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
Empty file added src/utils/__init__.py
Empty file.
40 changes: 40 additions & 0 deletions src/utils/deprecated.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""
Deprecated decorator from StackOverflow:
https://stackoverflow.com/questions/2536307/decorators-in-the-python-standard-lib-deprecated-specifically
"""
import warnings
import functools


def deprecated(reason: str = ""):
"""This is a decorator which can be used to mark functions
as deprecated. It will result in a warning being emitted
when the function is used."""

def decorator(func):
@functools.wraps(func)
def new_func(*args, **kwargs):
warnings.simplefilter('always', DeprecationWarning)
msg = f"Call to deprecated function {func.__name__}."

if reason:
msg += f"\nReason: {reason}"

warnings.warn(
msg,
category=DeprecationWarning,
stacklevel=2
)
warnings.simplefilter('default', DeprecationWarning)
return func(*args, **kwargs)

return new_func

# If the decorator is called without arguments
if callable(reason):
f = reason
reason = ""
return decorator(f)

return decorator

0 comments on commit a7905fd

Please sign in to comment.