1
0
mirror of https://github.com/eternnoir/pyTelegramBotAPI.git synced 2023-08-10 21:12:57 +03:00

Merge pull request #1214 from SwissCorePy/master

new deprecated decorator
This commit is contained in:
Badiboy 2021-06-30 15:01:46 +03:00 committed by GitHub
commit 484e7fccbd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 15 deletions

View File

@ -6,7 +6,7 @@ import threading
import traceback import traceback
import warnings import warnings
import functools import functools
from typing import Any, List, Dict, Union from typing import Any, Callable, List, Dict, Optional, Union
import queue as Queue import queue as Queue
import logging import logging
@ -420,17 +420,22 @@ def generate_random_token():
return ''.join(random.sample(string.ascii_letters, 16)) return ''.join(random.sample(string.ascii_letters, 16))
def deprecated(func): def deprecated(warn: bool=False, alternative: Optional[Callable]=None):
"""This is a decorator which can be used to mark functions """
as deprecated. It will result in a warning being emitted Use this decorator to mark functions as deprecated.
when the function is used.""" When the function is used, an info (or warning if `warn` is True) is logged.
# https://stackoverflow.com/a/30253848/441814 :param warn: If True a warning is logged else an info
@functools.wraps(func) :param alternative: The new function to use instead
def new_func(*args, **kwargs): """
warnings.simplefilter('always', DeprecationWarning) # turn off filter def decorator(function):
warnings.warn("Call to deprecated function {}.".format(func.__name__), def wrapper(*args, **kwargs):
category=DeprecationWarning, if not warn:
stacklevel=2) logger.info(f"`{function.__name__}` is deprecated."
warnings.simplefilter('default', DeprecationWarning) # reset filter + (f" Use `{alternative.__name__}` instead" if alternative else ""))
return func(*args, **kwargs) else:
return new_func logger.warn(f"`{function.__name__}` is deprecated."
+ (f" Use `{alternative.__name__}` instead" if alternative else ""))
return function(*args, **kwargs)
return wrapper
return decorator

View File

@ -19,6 +19,14 @@ if not should_skip:
CHAT_ID = os.environ['CHAT_ID'] CHAT_ID = os.environ['CHAT_ID']
GROUP_ID = os.environ['GROUP_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") @pytest.mark.skipif(should_skip, reason="No environment variables configured")
class TestTeleBot: class TestTeleBot:
@ -616,6 +624,9 @@ class TestTeleBot:
time.sleep(1) time.sleep(1)
assert update.message.text == 'got' * 2 assert update.message.text == 'got' * 2
def test_deprecated_dec(self):
_test()
def test_chat_permissions(self): def test_chat_permissions(self):
return # CHAT_ID is private chat, no permissions can be set return # CHAT_ID is private chat, no permissions can be set
#tb = telebot.TeleBot(TOKEN) #tb = telebot.TeleBot(TOKEN)