hojichar.utils.warn_deprecation
1import inspect 2import logging 3import warnings 4from functools import wraps 5from typing import Any, Callable, Optional 6 7logger = logging.getLogger(__name__) 8 9 10def deprecated_since(version: str, alternative: Optional[str] = None) -> Callable[[Any], Any]: 11 """ 12 Decorator to mark functions or classes as deprecated since a given version, 13 emitting both a DeprecationWarning and a logging.warning(). 14 """ 15 16 def decorator(obj: Any) -> Any: 17 name = obj.__name__ 18 msg = f"'{name}' is deprecated since version {version} and will be removed in a future release." 19 if alternative: 20 msg += f" Use '{alternative}' instead." 21 22 def _emit_warning() -> None: 23 warnings.warn(msg, category=DeprecationWarning, stacklevel=3) 24 25 if inspect.isclass(obj): 26 orig_init = obj.__init__ 27 28 @wraps(orig_init) 29 def new_init(self, *args, **kwargs): # type: ignore 30 _emit_warning() 31 return orig_init(self, *args, **kwargs) 32 33 obj.__init__ = new_init 34 return obj 35 else: 36 37 @wraps(obj) 38 def new_func(*args, **kwargs): # type: ignore 39 _emit_warning() 40 return obj(*args, **kwargs) 41 42 return new_func 43 44 return decorator
def
deprecated_since(version: str, alternative: Optional[str] = None) -> Callable[[Any], Any]:
11def deprecated_since(version: str, alternative: Optional[str] = None) -> Callable[[Any], Any]: 12 """ 13 Decorator to mark functions or classes as deprecated since a given version, 14 emitting both a DeprecationWarning and a logging.warning(). 15 """ 16 17 def decorator(obj: Any) -> Any: 18 name = obj.__name__ 19 msg = f"'{name}' is deprecated since version {version} and will be removed in a future release." 20 if alternative: 21 msg += f" Use '{alternative}' instead." 22 23 def _emit_warning() -> None: 24 warnings.warn(msg, category=DeprecationWarning, stacklevel=3) 25 26 if inspect.isclass(obj): 27 orig_init = obj.__init__ 28 29 @wraps(orig_init) 30 def new_init(self, *args, **kwargs): # type: ignore 31 _emit_warning() 32 return orig_init(self, *args, **kwargs) 33 34 obj.__init__ = new_init 35 return obj 36 else: 37 38 @wraps(obj) 39 def new_func(*args, **kwargs): # type: ignore 40 _emit_warning() 41 return obj(*args, **kwargs) 42 43 return new_func 44 45 return decorator
Decorator to mark functions or classes as deprecated since a given version, emitting both a DeprecationWarning and a logging.warning().