From a6668397e10ec6203293f2582af567a8903b0a6a Mon Sep 17 00:00:00 2001 From: SwissCorePy <51398037+SwissCorePy@users.noreply.github.com> Date: Wed, 30 Jun 2021 13:08:05 +0200 Subject: [PATCH 1/3] new deprecated decorator added a new deprecated decorator to util --- telebot/util.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/telebot/util.py b/telebot/util.py index 3dd71db..50ca450 100644 --- a/telebot/util.py +++ b/telebot/util.py @@ -6,7 +6,7 @@ import threading import traceback import warnings import functools -from typing import Any, List, Dict, Union +from typing import Any, Callable, List, Dict, Optional, Union import queue as Queue import logging @@ -420,6 +420,26 @@ def generate_random_token(): return ''.join(random.sample(string.ascii_letters, 16)) +def deprecated_dec(warn: Optional[bool]=False, alternative: Optional[Callable]=None): + """ + Use this decorator to mark functions as deprecated. + When the function is used, an info (or warning if `warn` is True) is logged. + :param warn: If True a warning is logged else an info + :param alternative: The new function to use instead + """ + def decorator(function): + def wrapper(*args, **kwargs): + if not warn: + logger.info(f"`{function.__name__}` is deprecated." + + (f" Use `{alternative.__name__}` instead" if alternative else "")) + else: + logger.warn(f"`{function.__name__}` is deprecated." + + (f" Use `{alternative.__name__}` instead" if alternative else "")) + return function(*args, **kwargs) + return wrapper + return decorator + + def deprecated(func): """This is a decorator which can be used to mark functions as deprecated. It will result in a warning being emitted From 073d7fb6a733ee26c1d6985fed422fd28fcbe34c Mon Sep 17 00:00:00 2001 From: SwissCorePy <51398037+SwissCorePy@users.noreply.github.com> Date: Wed, 30 Jun 2021 13:11:48 +0200 Subject: [PATCH 2/3] Update util.py whoops warn is not optional --- telebot/util.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/telebot/util.py b/telebot/util.py index 50ca450..49ea25d 100644 --- a/telebot/util.py +++ b/telebot/util.py @@ -420,7 +420,7 @@ def generate_random_token(): return ''.join(random.sample(string.ascii_letters, 16)) -def deprecated_dec(warn: Optional[bool]=False, alternative: Optional[Callable]=None): +def deprecated_dec(warn: bool=False, alternative: Optional[Callable]=None): """ Use this decorator to mark functions as deprecated. When the function is used, an info (or warning if `warn` is True) is logged. From 791d65e95a9ba60ac8b798c7d865f9d887d9880c Mon Sep 17 00:00:00 2001 From: SwissCorePy <51398037+SwissCorePy@users.noreply.github.com> Date: Wed, 30 Jun 2021 13:47:39 +0200 Subject: [PATCH 3/3] replaced old deprecated decorator --- telebot/util.py | 17 +---------------- tests/test_telebot.py | 11 +++++++++++ 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/telebot/util.py b/telebot/util.py index 49ea25d..711f003 100644 --- a/telebot/util.py +++ b/telebot/util.py @@ -420,7 +420,7 @@ def generate_random_token(): return ''.join(random.sample(string.ascii_letters, 16)) -def deprecated_dec(warn: bool=False, alternative: Optional[Callable]=None): +def deprecated(warn: bool=False, alternative: Optional[Callable]=None): """ Use this decorator to mark functions as deprecated. When the function is used, an info (or warning if `warn` is True) is logged. @@ -439,18 +439,3 @@ def deprecated_dec(warn: bool=False, alternative: Optional[Callable]=None): return wrapper return decorator - -def deprecated(func): - """This is a decorator which can be used to mark functions - as deprecated. It will result in a warning being emitted - when the function is used.""" - # https://stackoverflow.com/a/30253848/441814 - @functools.wraps(func) - def new_func(*args, **kwargs): - warnings.simplefilter('always', DeprecationWarning) # turn off filter - warnings.warn("Call to deprecated function {}.".format(func.__name__), - category=DeprecationWarning, - stacklevel=2) - warnings.simplefilter('default', DeprecationWarning) # reset filter - return func(*args, **kwargs) - return new_func diff --git a/tests/test_telebot.py b/tests/test_telebot.py index a2f3d36..0421033 100644 --- a/tests/test_telebot.py +++ b/tests/test_telebot.py @@ -19,6 +19,14 @@ if not should_skip: CHAT_ID = os.environ['CHAT_ID'] GROUP_ID = os.environ['GROUP_ID'] +def _new_test(): + pass + +@util.deprecated(alternative=_new_test) +def _test(): + pass + + @pytest.mark.skipif(should_skip, reason="No environment variables configured") class TestTeleBot: @@ -605,6 +613,9 @@ class TestTeleBot: tb.process_new_updates([update]) time.sleep(1) assert update.message.text == 'got' * 2 + + def test_deprecated_dec(self): + _test() def test_chat_permissions(self): return # CHAT_ID is private chat, no permissions can be set