-
Notifications
You must be signed in to change notification settings - Fork 214
/
deprecation_decorator.py
29 lines (25 loc) · 1.14 KB
/
deprecation_decorator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import warnings
from typing import Callable, Any
def deprecated(message: str):
def deprecated_decorator(func: Callable) -> Any:
def deprecated_func(*args, **kwargs):
warnings.simplefilter('default', DeprecationWarning)
warnings.warn(f'{func.__name__} is a deprecated function. {message}',
category=DeprecationWarning,
stacklevel=2)
return func(*args, **kwargs)
return deprecated_func
return deprecated_decorator
def deprecated_param(*, name: str, position: int, message: str):
def deprecated_decorator(func: Callable) -> Any:
def deprecated_func(*args, **kwargs):
if len(args) >= position + 1 or name in kwargs:
warnings.simplefilter('default', DeprecationWarning)
warnings.warn(
f'{func.__name__} function parameter `{name}` at position {position} is deprecated. {message}',
category=DeprecationWarning,
stacklevel=2
)
return func(*args, **kwargs)
return deprecated_func
return deprecated_decorator