|
6 | 6 | import traceback |
7 | 7 | import warnings |
8 | 8 | import functools |
9 | | -from typing import Any, List, Dict, Union |
| 9 | +from typing import Any, Callable, List, Dict, Optional, Union |
10 | 10 |
|
11 | 11 | import queue as Queue |
12 | 12 | import logging |
@@ -420,17 +420,22 @@ def generate_random_token(): |
420 | 420 | return ''.join(random.sample(string.ascii_letters, 16)) |
421 | 421 |
|
422 | 422 |
|
423 | | -def deprecated(func): |
424 | | - """This is a decorator which can be used to mark functions |
425 | | - as deprecated. It will result in a warning being emitted |
426 | | - when the function is used.""" |
427 | | - # https://stackoverflow.com/a/30253848/441814 |
428 | | - @functools.wraps(func) |
429 | | - def new_func(*args, **kwargs): |
430 | | - warnings.simplefilter('always', DeprecationWarning) # turn off filter |
431 | | - warnings.warn("Call to deprecated function {}.".format(func.__name__), |
432 | | - category=DeprecationWarning, |
433 | | - stacklevel=2) |
434 | | - warnings.simplefilter('default', DeprecationWarning) # reset filter |
435 | | - return func(*args, **kwargs) |
436 | | - return new_func |
| 423 | +def deprecated(warn: bool=False, alternative: Optional[Callable]=None): |
| 424 | + """ |
| 425 | + Use this decorator to mark functions as deprecated. |
| 426 | + When the function is used, an info (or warning if `warn` is True) is logged. |
| 427 | + :param warn: If True a warning is logged else an info |
| 428 | + :param alternative: The new function to use instead |
| 429 | + """ |
| 430 | + def decorator(function): |
| 431 | + def wrapper(*args, **kwargs): |
| 432 | + if not warn: |
| 433 | + logger.info(f"`{function.__name__}` is deprecated." |
| 434 | + + (f" Use `{alternative.__name__}` instead" if alternative else "")) |
| 435 | + else: |
| 436 | + logger.warn(f"`{function.__name__}` is deprecated." |
| 437 | + + (f" Use `{alternative.__name__}` instead" if alternative else "")) |
| 438 | + return function(*args, **kwargs) |
| 439 | + return wrapper |
| 440 | + return decorator |
| 441 | + |
0 commit comments