Skip to content

Commit

Permalink
adding a hard_deprecated decorator for when we want to be draconian
Browse files Browse the repository at this point in the history
  • Loading branch information
brifordwylie committed Nov 27, 2024
1 parent 82d92fb commit bbc3b95
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions src/sageworks/utils/deprecated_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Deprecated Utilities for Sageworks"""

import traceback
import sys
import logging
from functools import wraps
from typing import Callable, Type, Union
Expand Down Expand Up @@ -49,6 +50,39 @@ def wrapper(*args, **kwargs):
return decorator


def hard_deprecated() -> Callable:
"""Decorator to mark classes or functions as immediately deprecated and cause termination."""

def decorator(cls_or_func: Union[Type, Callable]) -> Union[Type, Callable]:
message = f"{cls_or_func.__name__} is immediately deprecated and is being removed."

if isinstance(cls_or_func, type):
# Class decorator
original_init = cls_or_func.__init__

@wraps(original_init)
def new_init(self, *args, **kwargs):
log.critical(message)
trimmed_stack = "".join(traceback.format_stack()[:-1])
log.critical("Call stack:\n%s", trimmed_stack)
sys.exit(1)

cls_or_func.__init__ = new_init
return cls_or_func
else:
# Function/method decorator
@wraps(cls_or_func)
def wrapper(*args, **kwargs):
log.critical(message)
trimmed_stack = "".join(traceback.format_stack()[:-1])
log.critical("Call stack:\n%s", trimmed_stack)
sys.exit(1)

return wrapper

return decorator


if __name__ == "__main__":

@deprecated(version="0.9")
Expand All @@ -65,3 +99,20 @@ def old_method(self):

obj = MyClass()
obj.old_method()

@hard_deprecated()
class ImmediateClass:
def __init__(self):
print("This should not be seen.")

# Uncomment to test (this will exit the script)
# instance = ImmediateClass()

class AnotherClass:
@hard_deprecated()
def immediate_method(self):
print("This should not be seen either.")

# Uncomment to test (this will exit the script)
# obj = AnotherClass()
# obj.immediate_method()

0 comments on commit bbc3b95

Please sign in to comment.