Coverage for src/sensai/util/deprecation.py: 88%

16 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-08-13 22:17 +0000

1import warnings 

2import logging 

3from functools import wraps 

4 

5log = logging.getLogger(__name__) 

6 

7 

8def deprecated(message): 

9 """ 

10 This is a decorator for functions to mark them as deprecated, issuing a warning when the function is called 

11 

12 :param message: message, which can indicate the new recommended approach or reason for deprecation 

13 :return: decorated function 

14 """ 

15 def deprecated_decorator(func): 

16 @wraps(func) 

17 def deprecated_func(*args, **kwargs): 

18 msg = "{} is a deprecated function. {}".format(func.__name__, message) 

19 if logging.Logger.root.hasHandlers(): 

20 log.warning(msg) 

21 else: 

22 warnings.warn(msg, category=DeprecationWarning, stacklevel=2) 

23 warnings.simplefilter('default', DeprecationWarning) 

24 return func(*args, **kwargs) 

25 return deprecated_func 

26 return deprecated_decorator