From 422c3b53b26b7984081b4f81efe2aa3f81641555 Mon Sep 17 00:00:00 2001 From: Hinrich Mahler <22366557+Bibo-Joshi@users.noreply.github.com> Date: Fri, 21 Apr 2023 17:57:35 +0200 Subject: [PATCH 01/10] Update a few warning categories --- telegram/_bot.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/telegram/_bot.py b/telegram/_bot.py index 79afc1a11d7..5ed3de893f2 100644 --- a/telegram/_bot.py +++ b/telegram/_bot.py @@ -5619,6 +5619,7 @@ async def upload_sticker_file( "`upload_sticker_file` is deprecated. Please use the new parameters " "`sticker` and `sticker_format` instead.", stacklevel=4, + category=PTBDeprecationWarning, ) data: JSONDict = { @@ -5818,6 +5819,7 @@ async def create_new_sticker_set( "`create_new_sticker_set` are deprecated. Please use the new parameter " "`stickers` and `sticker_format` instead.", stacklevel=4, + category=PTBDeprecationWarning, ) data: JSONDict = { @@ -5989,6 +5991,7 @@ async def add_sticker_to_set( "`add_sticker_to_set` are deprecated. Please use the new parameter `sticker` " "instead.", stacklevel=4, + category=PTBDeprecationWarning, ) data: JSONDict = { From e43b7971b30d11ebca876b402406348085b23df3 Mon Sep 17 00:00:00 2001 From: Hinrich Mahler <22366557+Bibo-Joshi@users.noreply.github.com> Date: Fri, 21 Apr 2023 23:19:12 +0200 Subject: [PATCH 02/10] Get started on updating tests --- telegram/ext/_conversationhandler.py | 3 ++ telegram/ext/_picklepersistence.py | 5 +++- tests/_files/test_sticker.py | 24 +++++++++------- .../_inline/test_inlinequeryresultarticle.py | 2 +- tests/_inline/test_inlinequeryresultgif.py | 4 +-- .../_inline/test_inlinequeryresultmpeg4gif.py | 2 +- tests/_inline/test_inlinequeryresultphoto.py | 2 +- tests/_inline/test_inlinequeryresultvideo.py | 2 +- tests/ext/test_application.py | 4 +++ tests/ext/test_callbackcontext.py | 2 ++ tests/ext/test_conversationhandler.py | 28 +++++++++++++++++++ tests/ext/test_dictpersistence.py | 2 +- tests/ext/test_jobqueue.py | 3 ++ tests/ext/test_picklepersistence.py | 6 ++++ tests/test_bot.py | 11 +++++++- tests/test_chatpermissions.py | 3 ++ tests/test_keyboardbutton.py | 3 ++ tests/test_telegramobject.py | 2 ++ 18 files changed, 89 insertions(+), 19 deletions(-) diff --git a/telegram/ext/_conversationhandler.py b/telegram/ext/_conversationhandler.py index ce979fe58d9..5d7281d0cb2 100644 --- a/telegram/ext/_conversationhandler.py +++ b/telegram/ext/_conversationhandler.py @@ -840,11 +840,13 @@ async def handle_update( # type: ignore[override] if application.job_queue is None: warn( "Ignoring `conversation_timeout` because the Application has no JobQueue.", + stacklevel=1, ) elif not application.job_queue.scheduler.running: warn( "Ignoring `conversation_timeout` because the Applications JobQueue is " "not running.", + stacklevel=1, ) elif isinstance(new_state, asyncio.Task): # Add the new timeout job @@ -931,6 +933,7 @@ async def _trigger_timeout(self, context: CCT) -> None: warn( "ApplicationHandlerStop in TIMEOUT state of " "ConversationHandler has no effect. Ignoring.", + stacklevel=2, ) self._update_state(self.END, ctxt.conversation_key) diff --git a/telegram/ext/_picklepersistence.py b/telegram/ext/_picklepersistence.py index 691e849705a..ee058ec5c17 100644 --- a/telegram/ext/_picklepersistence.py +++ b/telegram/ext/_picklepersistence.py @@ -98,7 +98,10 @@ def persistent_id(self, obj: object) -> Optional[str]: if obj is self._bot: return _REPLACED_KNOWN_BOT if isinstance(obj, Bot): - warn("Unknown bot instance found. Will be replaced by `None` during unpickling") + warn( + "Unknown bot instance found. Will be replaced by `None` during unpickling", + stacklevel=2, + ) return _REPLACED_UNKNOWN_BOT return None # pickles as usual diff --git a/tests/_files/test_sticker.py b/tests/_files/test_sticker.py index 433563a43d0..0bee381e61e 100644 --- a/tests/_files/test_sticker.py +++ b/tests/_files/test_sticker.py @@ -649,6 +649,7 @@ async def make_assertion(*args, **kwargs): await bot.upload_sticker_file(chat_id, "png_sticker_file_id") assert len(recwarn) == 1 assert "Since Bot API 6.6, the parameter" in str(recwarn[0].message) + assert recwarn[0].category is PTBDeprecationWarning assert recwarn[0].filename == __file__ async def test_upload_sticker_file_missing_required_args(self, bot, chat_id): @@ -665,7 +666,11 @@ async def test_upload_sticker_file_mutually_exclusive(self, bot, chat_id): @pytest.mark.parametrize("local_mode", [True, False]) async def test_upload_sticker_file_local_files( - self, monkeypatch, bot, chat_id, local_mode, recwarn + self, + monkeypatch, + bot, + chat_id, + local_mode, ): try: bot._local_mode = local_mode @@ -705,6 +710,7 @@ async def make_assertion(*args, **kwargs): await bot.create_new_sticker_set(chat_id, "name", "title", "some_str_emoji") assert len(recwarn) == 1 assert "Since Bot API 6.6, the parameters" in str(recwarn[0].message) + assert recwarn[0].category is PTBDeprecationWarning assert recwarn[0].filename == __file__ async def test_create_new_sticker_set_missing_required_args(self, bot, chat_id): @@ -720,9 +726,7 @@ async def test_create_new_sticker_set_mutually_exclusive(self, bot, chat_id): ) @pytest.mark.parametrize("local_mode", [True, False]) - async def test_create_new_sticker_set_local_files( - self, monkeypatch, bot, chat_id, local_mode, recwarn - ): + async def test_create_new_sticker_set_local_files(self, monkeypatch, bot, chat_id, local_mode): monkeypatch.setattr(bot, "_local_mode", local_mode) # For just test that the correct paths are passed as we have no local bot API set up test_flag = False @@ -769,9 +773,7 @@ async def make_assertion(_, data, *args, **kwargs): assert test_flag assert len(recwarn) in (1, 2) - async def test_create_new_sticker_all_params( - self, monkeypatch, bot, chat_id, mask_position, recwarn - ): + async def test_create_new_sticker_all_params(self, monkeypatch, bot, chat_id, mask_position): async def make_assertion_old_params(_, data, *args, **kwargs): assert data["user_id"] == chat_id assert data["name"] == "name" @@ -804,6 +806,9 @@ async def make_assertion_new_params(_, data, *args, **kwargs): sticker_type=Sticker.MASK, ) assert len(recwarn) == 1 + assert recwarn[0].filename == __file__, "wrong stacklevel" + assert recwarn[0].category is PTBDeprecationWarning + assert str(recwarn[0]) == "udaitrne" monkeypatch.setattr(bot, "_post", make_assertion_new_params) await bot.create_new_sticker_set( chat_id, @@ -824,6 +829,7 @@ async def make_assertion(*args, **kwargs): await bot.add_sticker_to_set(chat_id, "name", "emoji", "fake_file_id") assert len(recwarn) == 1 assert "Since Bot API 6.6, the parameters" in str(recwarn[0].message) + assert recwarn[0].category is PTBDeprecationWarning assert recwarn[0].filename == __file__ async def test_add_sticker_to_set_missing_required_arg(self, bot, chat_id): @@ -835,9 +841,7 @@ async def test_add_sticker_to_set_mutually_exclusive(self, bot, chat_id): await bot.add_sticker_to_set(chat_id, "name", "emojis", sticker="something") @pytest.mark.parametrize("local_mode", [True, False]) - async def test_add_sticker_to_set_local_files( - self, monkeypatch, bot, chat_id, local_mode, recwarn - ): + async def test_add_sticker_to_set_local_files(self, monkeypatch, bot, chat_id, local_mode): monkeypatch.setattr(bot, "_local_mode", local_mode) # For just test that the correct paths are passed as we have no local bot API set up test_flag = False diff --git a/tests/_inline/test_inlinequeryresultarticle.py b/tests/_inline/test_inlinequeryresultarticle.py index 6b36adcaf3b..c74ffe457e3 100644 --- a/tests/_inline/test_inlinequeryresultarticle.py +++ b/tests/_inline/test_inlinequeryresultarticle.py @@ -61,7 +61,7 @@ class TestInlineQueryResultArticleBase: class TestInlineQueryResultArticleWithoutRequest(TestInlineQueryResultArticleBase): - def test_slot_behaviour(self, inline_query_result_article, recwarn): + def test_slot_behaviour(self, inline_query_result_article): inst = inline_query_result_article for attr in inst.__slots__: assert getattr(inst, attr, "err") != "err", f"got extra slot '{attr}'" diff --git a/tests/_inline/test_inlinequeryresultgif.py b/tests/_inline/test_inlinequeryresultgif.py index 1edd5c353d8..7d3545af705 100644 --- a/tests/_inline/test_inlinequeryresultgif.py +++ b/tests/_inline/test_inlinequeryresultgif.py @@ -170,7 +170,7 @@ def test_thumb_url_issues_warning_and_works_without_positional_arg(self, recwarn new_name="thumbnail_url", ) - def test_init_throws_error_without_thumbnail_url_and_thumb_url(self, recwarn): + def test_init_throws_error_without_thumbnail_url_and_thumb_url(self): with pytest.raises(ValueError, match="You must pass either"): InlineQueryResultGif( TestInlineQueryResultGifBase.id_, @@ -188,7 +188,7 @@ def test_init_throws_error_without_thumbnail_url_and_thumb_url(self, recwarn): thumbnail_mime_type=TestInlineQueryResultGifBase.thumbnail_mime_type, ) - def test_throws_value_error_with_different_deprecated_and_new_arg_thumb_url(self, recwarn): + def test_throws_value_error_with_different_deprecated_and_new_arg_thumb_url(self): with pytest.raises( ValueError, match="different entities as 'thumb_url' and 'thumbnail_url'" ): diff --git a/tests/_inline/test_inlinequeryresultmpeg4gif.py b/tests/_inline/test_inlinequeryresultmpeg4gif.py index eab02169481..7a340a0464d 100644 --- a/tests/_inline/test_inlinequeryresultmpeg4gif.py +++ b/tests/_inline/test_inlinequeryresultmpeg4gif.py @@ -175,7 +175,7 @@ def test_thumb_url_issues_warning_and_works_without_positional_arg(self, recwarn new_name="thumbnail_url", ) - def test_init_throws_error_without_thumbnail_url_and_thumb_url(self, recwarn): + def test_init_throws_error_without_thumbnail_url_and_thumb_url(self): with pytest.raises(ValueError, match="You must pass either"): InlineQueryResultMpeg4Gif( TestInlineQueryResultMpeg4GifBase.id_, diff --git a/tests/_inline/test_inlinequeryresultphoto.py b/tests/_inline/test_inlinequeryresultphoto.py index a0796838f54..d10b15fa374 100644 --- a/tests/_inline/test_inlinequeryresultphoto.py +++ b/tests/_inline/test_inlinequeryresultphoto.py @@ -142,7 +142,7 @@ def test_thumb_url_issues_warning_and_works_without_positional_arg(self, recwarn new_name="thumbnail_url", ) - def test_init_throws_error_without_thumbnail_url_and_thumb_url(self, recwarn): + def test_init_throws_error_without_thumbnail_url_and_thumb_url(self): with pytest.raises(ValueError, match="You must pass either"): InlineQueryResultPhoto( TestInlineQueryResultPhotoBase.id_, diff --git a/tests/_inline/test_inlinequeryresultvideo.py b/tests/_inline/test_inlinequeryresultvideo.py index a751316cecf..e34eb348c46 100644 --- a/tests/_inline/test_inlinequeryresultvideo.py +++ b/tests/_inline/test_inlinequeryresultvideo.py @@ -157,7 +157,7 @@ def test_thumb_url_issues_warning_and_works_without_positional_arg(self, recwarn new_name="thumbnail_url", ) - def test_init_throws_error_without_thumbnail_url_and_thumb_url(self, recwarn): + def test_init_throws_error_without_thumbnail_url_and_thumb_url(self): with pytest.raises(ValueError, match="You must pass either"): InlineQueryResultVideo( TestInlineQueryResultVideoBase.id_, diff --git a/tests/ext/test_application.py b/tests/ext/test_application.py index 8ee4d09ec0f..461bd8af7e6 100644 --- a/tests/ext/test_application.py +++ b/tests/ext/test_application.py @@ -143,6 +143,7 @@ def test_manual_init_warning(self, recwarn, updater): str(recwarn[-1].message) == "`Application` instances should be built via the `ApplicationBuilder`." ) + assert recwarn[0].category is PTBUserWarning assert recwarn[0].filename == __file__, "stacklevel is incorrect!" @pytest.mark.parametrize( @@ -223,6 +224,7 @@ def test_job_queue(self, one_time_bot, app, recwarn): assert application.job_queue is None assert len(recwarn) == 1 assert str(recwarn[0].message) == expected_warning + assert recwarn[0].category is PTBUserWarning assert recwarn[0].filename == __file__, "wrong stacklevel" def test_custom_context_init(self, one_time_bot): @@ -1207,6 +1209,7 @@ async def callback(): assert task.result() == 43 else: assert len(recwarn) == 1 + assert recwarn[0].category is PTBUserWarning assert "won't be automatically awaited" in str(recwarn[0].message) assert recwarn[0].filename == __file__, "wrong stacklevel!" assert not task.done() @@ -2074,6 +2077,7 @@ async def raise_method(*args, **kwargs): for record in recwarn: print(record) if str(record.message).startswith("Could not add signal handlers for the stop"): + assert record.category is PTBUserWarning assert record.filename == __file__, "stacklevel is incorrect!" found = True assert found diff --git a/tests/ext/test_callbackcontext.py b/tests/ext/test_callbackcontext.py index 01aea45d7de..32d518714b9 100644 --- a/tests/ext/test_callbackcontext.py +++ b/tests/ext/test_callbackcontext.py @@ -31,6 +31,7 @@ ) from telegram.error import TelegramError from telegram.ext import ApplicationBuilder, CallbackContext, Job +from telegram.warnings import PTBUserWarning from tests.auxil.slots import mro_slots """ @@ -72,6 +73,7 @@ def test_job_queue(self, bot, app, recwarn): assert callback_context.job_queue is None assert len(recwarn) == 1 assert str(recwarn[0].message) == expected_warning + assert recwarn[0].category is PTBUserWarning assert recwarn[0].filename == __file__, "wrong stacklevel" def test_from_update(self, app): diff --git a/tests/ext/test_conversationhandler.py b/tests/ext/test_conversationhandler.py index c6e3aadd71c..e79d145e66d 100644 --- a/tests/ext/test_conversationhandler.py +++ b/tests/ext/test_conversationhandler.py @@ -19,6 +19,7 @@ """Persistence of conversations is tested in test_basepersistence.py""" import asyncio import logging +from pathlib import Path from warnings import filterwarnings import pytest @@ -58,6 +59,7 @@ ) from telegram.warnings import PTBUserWarning from tests.auxil.build_messages import make_command_message +from tests.auxil.files import PROJECT_ROOT_PATH from tests.auxil.pytest_classes import PytestBot, make_bot from tests.auxil.slots import mro_slots @@ -458,6 +460,7 @@ class NotUpdate: # this for loop checks if the correct stacklevel is used when generating the warning for warning in recwarn: + assert warning.category is PTBUserWarning assert warning.filename == __file__, "incorrect stacklevel!" @pytest.mark.parametrize( @@ -676,6 +679,11 @@ async def callback(_, __): print(exc) raise exc assert len(recwarn) == 1 + assert recwarn[0].category is PTBUserWarning + assert ( + Path(recwarn[0].filename) + == PROJECT_ROOT_PATH / "telegram" / "ext" / "_conversationhandler.py" + ), "wrong stacklevel!" assert str(recwarn[0].message) == ( "'callback' returned state 69 which is unknown to the ConversationHandler xyz." ) @@ -1042,12 +1050,27 @@ async def test_no_running_job_queue_warning(self, app, bot, user1, recwarn, jq): await asyncio.sleep(0.5) if jq: assert len(recwarn) == 1 + assert ( + Path(recwarn[0].filename) + == PROJECT_ROOT_PATH / "telegram" / "ext" / "_conversationhandler.py" + ), "wrong stacklevel!" else: assert len(recwarn) == 2 + assert ( + Path(recwarn[0].filename) + == PROJECT_ROOT_PATH / "telegram" / "ext" / "_conversationhandler.py" + ), "wrong stacklevel!" + assert ( + Path(recwarn[1].filename) + == PROJECT_ROOT_PATH / "telegram" / "ext" / "_conversationhandler.py" + ), "wrong stacklevel!" + assert str(recwarn[0].message if jq else recwarn[1].message).startswith( "Ignoring `conversation_timeout`" ) assert ("is not running" if jq else "No `JobQueue` set up.") in str(recwarn[0].message) + for warning in recwarn: + assert warning.category is PTBUserWarning # now set app.job_queue back to it's original value async def test_schedule_job_exception(self, app, bot, user1, monkeypatch, caplog): @@ -1363,6 +1386,11 @@ def timeout(*args, **kwargs): assert handler.check_update(Update(0, message=message)) assert len(recwarn) == 1 assert str(recwarn[0].message).startswith("ApplicationHandlerStop in TIMEOUT") + assert recwarn[0].category is PTBUserWarning + assert ( + Path(recwarn[0].filename) + == PROJECT_ROOT_PATH / "telegram" / "ext" / "_jobqueue.py" + ), "wrong stacklevel!" await app.stop() diff --git a/tests/ext/test_dictpersistence.py b/tests/ext/test_dictpersistence.py index ae9d71dfbe8..b74db2d55f0 100644 --- a/tests/ext/test_dictpersistence.py +++ b/tests/ext/test_dictpersistence.py @@ -91,7 +91,7 @@ class TestDictPersistence: """Just tests the DictPersistence interface. Integration of persistence into Applictation is tested in TestBasePersistence!""" - async def test_slot_behaviour(self, recwarn): + async def test_slot_behaviour(self): inst = DictPersistence() for attr in inst.__slots__: assert getattr(inst, attr, "err") != "err", f"got extra slot '{attr}'" diff --git a/tests/ext/test_jobqueue.py b/tests/ext/test_jobqueue.py index 355b32c6611..b77d8b087ab 100644 --- a/tests/ext/test_jobqueue.py +++ b/tests/ext/test_jobqueue.py @@ -26,6 +26,7 @@ import pytest from telegram.ext import ApplicationBuilder, CallbackContext, ContextTypes, Job, JobQueue +from telegram.warnings import PTBUserWarning from tests.auxil.envvars import GITHUB_ACTION, TEST_WITH_OPT_DEPS from tests.auxil.pytest_classes import make_bot from tests.auxil.slots import mro_slots @@ -359,6 +360,7 @@ async def test_run_daily_warning(self, job_queue, recwarn): job_queue.run_daily(self.job_run_once, time_of_day, days=(0, 1, 2, 3)) assert len(recwarn) == 1 assert str(recwarn[0].message) == self.expected_warning + assert recwarn[0].category is PTBUserWarning assert recwarn[0].filename == __file__, "wrong stacklevel" @pytest.mark.parametrize("weekday", [0, 1, 2, 3, 4, 5, 6]) @@ -376,6 +378,7 @@ async def test_run_daily_days_of_week(self, job_queue, recwarn, weekday): assert scheduled_time == pytest.approx(expected_reschedule_time) assert len(recwarn) == 1 assert str(recwarn[0].message) == self.expected_warning + assert recwarn[0].category is PTBUserWarning assert recwarn[0].filename == __file__, "wrong stacklevel" async def test_run_monthly(self, job_queue, timezone): diff --git a/tests/ext/test_picklepersistence.py b/tests/ext/test_picklepersistence.py index 2164e12c718..c682776566b 100644 --- a/tests/ext/test_picklepersistence.py +++ b/tests/ext/test_picklepersistence.py @@ -27,6 +27,7 @@ from telegram import Chat, Message, TelegramObject, Update, User from telegram.ext import ContextTypes, PersistenceInput, PicklePersistence from telegram.warnings import PTBUserWarning +from tests.auxil.files import PROJECT_ROOT_PATH from tests.auxil.pytest_classes import make_bot from tests.auxil.slots import mro_slots @@ -891,7 +892,12 @@ async def test_custom_pickler_unpickler_simple( await pickle_persistence.update_chat_data(12345, data_with_bot) assert len(recwarn) == 1 assert recwarn[-1].category is PTBUserWarning + assert recwarn[-1].category is PTBUserWarning assert str(recwarn[-1].message).startswith("Unknown bot instance found.") + assert ( + Path(recwarn[-1].filename) + == PROJECT_ROOT_PATH / "telegram" / "ext" / "_picklepersistence.py" + ), "wrong stacklevel!" pp = PicklePersistence("pickletest", single_file=False, on_flush=False) pp.set_bot(bot) assert (await pp.get_chat_data())[12345]["unknown_bot_in_user"]._bot is None diff --git a/tests/test_bot.py b/tests/test_bot.py index 05ec6731efb..1bf8bc12622 100644 --- a/tests/test_bot.py +++ b/tests/test_bot.py @@ -26,6 +26,7 @@ import socket import time from collections import defaultdict +from pathlib import Path import pytest @@ -76,10 +77,11 @@ from telegram.ext import ExtBot, InvalidCallbackData from telegram.helpers import escape_markdown from telegram.request import BaseRequest, HTTPXRequest, RequestData +from telegram.warnings import PTBUserWarning from tests.auxil.bot_method_checks import check_defaults_handling from tests.auxil.ci_bots import FALLBACKS from tests.auxil.envvars import GITHUB_ACTION, TEST_WITH_OPT_DEPS -from tests.auxil.files import data_file +from tests.auxil.files import PROJECT_ROOT_PATH, data_file from tests.auxil.networking import expect_bad_request from tests.auxil.pytest_classes import PytestBot, PytestExtBot, make_bot from tests.auxil.slots import mro_slots @@ -1676,6 +1678,13 @@ async def test_http2_runtime_error(self, recwarn): "You set the HTTP version for the get_updates_request and request HTTPXRequest " "instance" in str(recwarn[2].message) ) + for warning in recwarn: + print() + print(warning) + assert ( + Path(warning.filename) == PROJECT_ROOT_PATH / "telegram" / "ext" / "_extbot.py" + ), "wrong stacklevel!" + assert warning.category is PTBUserWarning class TestBotWithRequest: diff --git a/tests/test_chatpermissions.py b/tests/test_chatpermissions.py index da984f784b0..9585710c108 100644 --- a/tests/test_chatpermissions.py +++ b/tests/test_chatpermissions.py @@ -20,6 +20,7 @@ import pytest from telegram import ChatPermissions, User +from telegram.warnings import PTBDeprecationWarning from tests.auxil.slots import mro_slots @@ -211,3 +212,5 @@ def test_equality_warning(self, recwarn, chat_permissions): "In v21, granular media settings will be considered as well when comparing" " ChatPermissions instances." ) + assert recwarn[0].category is PTBDeprecationWarning + assert recwarn[0].filename == __file__, "wrong stacklevel" diff --git a/tests/test_keyboardbutton.py b/tests/test_keyboardbutton.py index 63dd08f7735..f58af574c7f 100644 --- a/tests/test_keyboardbutton.py +++ b/tests/test_keyboardbutton.py @@ -26,6 +26,7 @@ KeyboardButtonRequestUser, WebAppInfo, ) +from telegram.warnings import PTBDeprecationWarning from tests.auxil.slots import mro_slots @@ -141,3 +142,5 @@ def test_equality_warning(self, recwarn, keyboard_button): "In v21, granular media settings will be considered as well when comparing" " ChatPermissions instances." ) + assert recwarn[0].category is PTBDeprecationWarning + assert recwarn[0].filename == __file__, "wrong stacklevel" diff --git a/tests/test_telegramobject.py b/tests/test_telegramobject.py index 50ec6c2406a..756f69bcfcf 100644 --- a/tests/test_telegramobject.py +++ b/tests/test_telegramobject.py @@ -28,6 +28,7 @@ from telegram import Bot, BotCommand, Chat, Message, PhotoSize, TelegramObject, User from telegram.ext import PicklePersistence +from telegram.warnings import PTBUserWarning from tests.auxil.files import data_file from tests.auxil.slots import mro_slots @@ -222,6 +223,7 @@ class TGO(TelegramObject): assert a == b assert len(recwarn) == 1 assert str(recwarn[0].message) == expected_warning + assert recwarn[0].category is PTBUserWarning assert recwarn[0].filename == __file__, "wrong stacklevel" def test_meaningful_comparison(self, recwarn): From f9888b64e1de7c6bb4873b66365e4baea7858349 Mon Sep 17 00:00:00 2001 From: Hinrich Mahler <22366557+Bibo-Joshi@users.noreply.github.com> Date: Sat, 22 Apr 2023 18:25:05 +0200 Subject: [PATCH 03/10] review --- tests/ext/test_conversationhandler.py | 16 ++++------------ tests/ext/test_picklepersistence.py | 1 - 2 files changed, 4 insertions(+), 13 deletions(-) diff --git a/tests/ext/test_conversationhandler.py b/tests/ext/test_conversationhandler.py index e79d145e66d..484e30ce743 100644 --- a/tests/ext/test_conversationhandler.py +++ b/tests/ext/test_conversationhandler.py @@ -1050,20 +1050,8 @@ async def test_no_running_job_queue_warning(self, app, bot, user1, recwarn, jq): await asyncio.sleep(0.5) if jq: assert len(recwarn) == 1 - assert ( - Path(recwarn[0].filename) - == PROJECT_ROOT_PATH / "telegram" / "ext" / "_conversationhandler.py" - ), "wrong stacklevel!" else: assert len(recwarn) == 2 - assert ( - Path(recwarn[0].filename) - == PROJECT_ROOT_PATH / "telegram" / "ext" / "_conversationhandler.py" - ), "wrong stacklevel!" - assert ( - Path(recwarn[1].filename) - == PROJECT_ROOT_PATH / "telegram" / "ext" / "_conversationhandler.py" - ), "wrong stacklevel!" assert str(recwarn[0].message if jq else recwarn[1].message).startswith( "Ignoring `conversation_timeout`" @@ -1071,6 +1059,10 @@ async def test_no_running_job_queue_warning(self, app, bot, user1, recwarn, jq): assert ("is not running" if jq else "No `JobQueue` set up.") in str(recwarn[0].message) for warning in recwarn: assert warning.category is PTBUserWarning + assert ( + Path(warning.filename) + == PROJECT_ROOT_PATH / "telegram" / "ext" / "_conversationhandler.py" + ), "wrong stacklevel!" # now set app.job_queue back to it's original value async def test_schedule_job_exception(self, app, bot, user1, monkeypatch, caplog): diff --git a/tests/ext/test_picklepersistence.py b/tests/ext/test_picklepersistence.py index c682776566b..c557cb21636 100644 --- a/tests/ext/test_picklepersistence.py +++ b/tests/ext/test_picklepersistence.py @@ -892,7 +892,6 @@ async def test_custom_pickler_unpickler_simple( await pickle_persistence.update_chat_data(12345, data_with_bot) assert len(recwarn) == 1 assert recwarn[-1].category is PTBUserWarning - assert recwarn[-1].category is PTBUserWarning assert str(recwarn[-1].message).startswith("Unknown bot instance found.") assert ( Path(recwarn[-1].filename) From 5e5f870fc64ec88197a32a97176382890a0ee20d Mon Sep 17 00:00:00 2001 From: Hinrich Mahler <22366557+Bibo-Joshi@users.noreply.github.com> Date: Sat, 22 Apr 2023 22:16:27 +0200 Subject: [PATCH 04/10] Fix existing tests & try to make warnings in Bot & ExtBot uniform --- telegram/_bot.py | 33 ++++++---- telegram/_utils/warnings_transition.py | 7 ++- telegram/ext/_extbot.py | 47 ++++++-------- tests/_files/test_sticker.py | 84 +++++++++++++++----------- tests/test_bot.py | 7 +-- 5 files changed, 97 insertions(+), 81 deletions(-) diff --git a/telegram/_bot.py b/telegram/_bot.py index 5ed3de893f2..61b204061ee 100644 --- a/telegram/_bot.py +++ b/telegram/_bot.py @@ -273,7 +273,7 @@ def __init__( warning_string = "request" if warning_string: - warn( + self._warn( f"You set the HTTP version for the {warning_string} HTTPXRequest instance to " f"HTTP/2. The self hosted bot api instances only support HTTP/1.1. You should " f"either run a HTTP proxy in front of it which supports HTTP/2 or use HTTP/1.1.", @@ -338,6 +338,14 @@ def private_key(self) -> Optional[Any]: """ return self._private_key + @classmethod + def _warn( + cls, message: str, category: Type[Warning] = PTBUserWarning, stacklevel: int = 0 + ) -> None: + """Convencience method to issue a warning. This method is here mostly to make it easier + for ExtBot to add 1 level to all warning calls.""" + warn(message=message, category=category, stacklevel=stacklevel + 1) + def __reduce__(self) -> NoReturn: """Customizes how :func:`copy.deepcopy` processes objects of this type. Bots can not be pickled and this method will always raise an exception. @@ -1157,7 +1165,7 @@ async def send_audio( """ thumbnail_or_thumb: FileInput = warn_about_thumb_return_thumbnail( - deprecated_arg=thumb, new_arg=thumbnail + deprecated_arg=thumb, new_arg=thumbnail, warn_callback=self._warn ) data: JSONDict = { "chat_id": chat_id, @@ -1294,7 +1302,7 @@ async def send_document( """ thumbnail_or_thumb: FileInput = warn_about_thumb_return_thumbnail( - deprecated_arg=thumb, new_arg=thumbnail + deprecated_arg=thumb, new_arg=thumbnail, warn_callback=self._warn ) data: JSONDict = { @@ -1531,7 +1539,7 @@ async def send_video( """ thumbnail_or_thumb: FileInput = warn_about_thumb_return_thumbnail( - deprecated_arg=thumb, new_arg=thumbnail + deprecated_arg=thumb, new_arg=thumbnail, warn_callback=self._warn ) data: JSONDict = { "chat_id": chat_id, @@ -1664,7 +1672,7 @@ async def send_video_note( """ thumbnail_or_thumb: FileInput = warn_about_thumb_return_thumbnail( - deprecated_arg=thumb, new_arg=thumbnail + deprecated_arg=thumb, new_arg=thumbnail, warn_callback=self._warn ) data: JSONDict = { "chat_id": chat_id, @@ -1807,6 +1815,7 @@ async def send_animation( thumbnail_or_thumb: FileInput = warn_about_thumb_return_thumbnail( deprecated_arg=thumb, new_arg=thumbnail, + warn_callback=self._warn, ) data: JSONDict = { "chat_id": chat_id, @@ -5614,11 +5623,11 @@ async def upload_sticker_file( # only, which would have been confusing. if png_sticker: - warn( + self._warn( "Since Bot API 6.6, the parameter `png_sticker` for " "`upload_sticker_file` is deprecated. Please use the new parameters " "`sticker` and `sticker_format` instead.", - stacklevel=4, + stacklevel=3, category=PTBDeprecationWarning, ) @@ -5814,11 +5823,11 @@ async def create_new_sticker_set( # only, which would have been confusing. if any(pre_api_6_6_params.values()): - warn( + self._warn( f"Since Bot API 6.6, the parameters {set(pre_api_6_6_params)} for " "`create_new_sticker_set` are deprecated. Please use the new parameter " "`stickers` and `sticker_format` instead.", - stacklevel=4, + stacklevel=3, category=PTBDeprecationWarning, ) @@ -5986,11 +5995,11 @@ async def add_sticker_to_set( ) if any(pre_api_6_6_params.values()): - warn( + self._warn( f"Since Bot API 6.6, the parameters {set(pre_api_6_6_params)} for " "`add_sticker_to_set` are deprecated. Please use the new parameter `sticker` " "instead.", - stacklevel=4, + stacklevel=3, category=PTBDeprecationWarning, ) @@ -6227,7 +6236,7 @@ async def set_sticker_set_thumb( :class:`telegram.error.TelegramError` """ - warn( + self._warn( message=( "Bot API 6.6 renamed the method 'setStickerSetThumb' to 'setStickerSetThumbnail', " "hence method 'set_sticker_set_thumb' was renamed to 'set_sticker_set_thumbnail' " diff --git a/telegram/_utils/warnings_transition.py b/telegram/_utils/warnings_transition.py index 06bf7eaa8c2..a4578f8dd5c 100644 --- a/telegram/_utils/warnings_transition.py +++ b/telegram/_utils/warnings_transition.py @@ -24,7 +24,7 @@ .. versionadded:: 20.2 """ import functools -from typing import Any +from typing import Any, Callable, Type from telegram._utils.warnings import warn from telegram.warnings import PTBDeprecationWarning @@ -39,6 +39,7 @@ def warn_about_deprecated_arg_return_new_arg( new_arg_name: str, bot_api_version: str, stacklevel: int = 3, + warn_callback: Callable[[str, Type[Warning], int], None] = warn, ) -> Any: """A helper function for the transition in API when argument is renamed. @@ -58,11 +59,11 @@ def warn_about_deprecated_arg_return_new_arg( ) if deprecated_arg: - warn( + warn_callback( f"Bot API {bot_api_version} renamed the argument '{deprecated_arg_name}' to " f"'{new_arg_name}'.", PTBDeprecationWarning, - stacklevel=stacklevel, + stacklevel, ) return deprecated_arg diff --git a/telegram/ext/_extbot.py b/telegram/ext/_extbot.py index e8f02cb3461..373cebff30d 100644 --- a/telegram/ext/_extbot.py +++ b/telegram/ext/_extbot.py @@ -18,7 +18,6 @@ # You should have received a copy of the GNU Lesser Public License # along with this program. If not, see [http://www.gnu.org/licenses/]. """This module contains an object that represents a Telegram Bot with convenience extensions.""" -import warnings from copy import copy from datetime import datetime from typing import ( @@ -31,6 +30,7 @@ Optional, Sequence, Tuple, + Type, TypeVar, Union, cast, @@ -87,11 +87,10 @@ from telegram._utils.defaultvalue import DEFAULT_NONE, DefaultValue from telegram._utils.logging import get_logger from telegram._utils.types import DVInput, FileInput, JSONDict, ODVInput, ReplyMarkup -from telegram._utils.warnings import warn from telegram.ext._callbackdatacache import CallbackDataCache from telegram.ext._utils.types import RLARGS from telegram.request import BaseRequest -from telegram.warnings import PTBDeprecationWarning +from telegram.warnings import PTBUserWarning if TYPE_CHECKING: from telegram import ( @@ -235,6 +234,14 @@ def __init__( self._callback_data_cache = CallbackDataCache(bot=self, maxsize=maxsize) + @classmethod + def _warn( + cls, message: str, category: Type[Warning] = PTBUserWarning, stacklevel: int = 0 + ) -> None: + """We override this method to add one more level to the stacklevel, so that the warning + points to the user's code, not to the PTB code.""" + super()._warn(message=message, category=category, stacklevel=stacklevel + 2) + @property def callback_data_cache(self) -> Optional[CallbackDataCache]: """:class:`telegram.ext.CallbackDataCache`: Optional. The cache for @@ -3284,30 +3291,16 @@ async def set_sticker_set_thumb( api_kwargs: JSONDict = None, rate_limit_args: RLARGS = None, ) -> bool: - # Manually issue deprecation here to get the stacklevel right - # Suppress the warning issued by super().set_sticker_set_thumb just in case - # the user explicitly enables deprecation warnings - # Unfortunately this is not entirely reliable (see tests), but it's a best effort solution - warn( - message=( - "Bot API 6.6 renamed the method 'setStickerSetThumb' to 'setStickerSetThumbnail', " - "hence method 'set_sticker_set_thumb' was renamed to 'set_sticker_set_thumbnail' " - "in PTB." - ), - category=PTBDeprecationWarning, - stacklevel=2, - ) - with warnings.catch_warnings(): - return await super().set_sticker_set_thumb( - name=name, - user_id=user_id, - thumb=thumb, - read_timeout=read_timeout, - write_timeout=write_timeout, - connect_timeout=connect_timeout, - pool_timeout=pool_timeout, - api_kwargs=self._merge_api_rl_kwargs(api_kwargs, rate_limit_args), - ) + return await super().set_sticker_set_thumb( + name=name, + user_id=user_id, + thumb=thumb, + read_timeout=read_timeout, + write_timeout=write_timeout, + connect_timeout=connect_timeout, + pool_timeout=pool_timeout, + api_kwargs=self._merge_api_rl_kwargs(api_kwargs, rate_limit_args), + ) async def set_webhook( self, diff --git a/tests/_files/test_sticker.py b/tests/_files/test_sticker.py index 0bee381e61e..d674260b5c5 100644 --- a/tests/_files/test_sticker.py +++ b/tests/_files/test_sticker.py @@ -35,7 +35,6 @@ ) from telegram.constants import StickerFormat from telegram.error import BadRequest, TelegramError -from telegram.ext import ExtBot from telegram.request import RequestData from telegram.warnings import PTBDeprecationWarning from tests.auxil.bot_method_checks import ( @@ -666,11 +665,7 @@ async def test_upload_sticker_file_mutually_exclusive(self, bot, chat_id): @pytest.mark.parametrize("local_mode", [True, False]) async def test_upload_sticker_file_local_files( - self, - monkeypatch, - bot, - chat_id, - local_mode, + self, monkeypatch, bot, chat_id, local_mode, recwarn ): try: bot._local_mode = local_mode @@ -697,7 +692,14 @@ async def make_assertion(_, data, *args, **kwargs): test_flag = False await bot.upload_sticker_file(chat_id, file) assert test_flag - assert len(recwarn) in (1, 2) # second warning is for unclosed file + + warnings = [w for w in recwarn if w.category is not ResourceWarning] + assert len(warnings) == 1 + assert warnings[0].category is PTBDeprecationWarning + assert warnings[0].filename == __file__ + assert str(warnings[0].message).startswith( + "Since Bot API 6.6, the parameter `png_sticker` for " + ) finally: bot._local_mode = False @@ -726,7 +728,9 @@ async def test_create_new_sticker_set_mutually_exclusive(self, bot, chat_id): ) @pytest.mark.parametrize("local_mode", [True, False]) - async def test_create_new_sticker_set_local_files(self, monkeypatch, bot, chat_id, local_mode): + async def test_create_new_sticker_set_local_files( + self, monkeypatch, bot, chat_id, local_mode, recwarn + ): monkeypatch.setattr(bot, "_local_mode", local_mode) # For just test that the correct paths are passed as we have no local bot API set up test_flag = False @@ -771,9 +775,17 @@ async def make_assertion(_, data, *args, **kwargs): sticker_format=StickerFormat.STATIC, ) assert test_flag - assert len(recwarn) in (1, 2) - async def test_create_new_sticker_all_params(self, monkeypatch, bot, chat_id, mask_position): + warnings = [w for w in recwarn if w.category is not ResourceWarning] + assert len(warnings) == 1 + assert warnings[0].category is PTBDeprecationWarning + assert warnings[0].filename == __file__ + assert str(warnings[0].message).startswith("Since Bot API 6.6, the parameters") + assert "for `create_new_sticker_set` are deprecated" in str(warnings[0].message) + + async def test_create_new_sticker_all_params( + self, monkeypatch, bot, chat_id, mask_position, recwarn + ): async def make_assertion_old_params(_, data, *args, **kwargs): assert data["user_id"] == chat_id assert data["name"] == "name" @@ -808,7 +820,10 @@ async def make_assertion_new_params(_, data, *args, **kwargs): assert len(recwarn) == 1 assert recwarn[0].filename == __file__, "wrong stacklevel" assert recwarn[0].category is PTBDeprecationWarning - assert str(recwarn[0]) == "udaitrne" + assert str(recwarn[0].message).startswith("Since Bot API 6.6, the parameters") + assert "for `create_new_sticker_set` are deprecated" in str(recwarn[0].message) + + recwarn.clear() monkeypatch.setattr(bot, "_post", make_assertion_new_params) await bot.create_new_sticker_set( chat_id, @@ -818,7 +833,7 @@ async def make_assertion_new_params(_, data, *args, **kwargs): sticker_format=StickerFormat.STATIC, needs_repainting=True, ) - assert len(recwarn) == 1 + assert len(recwarn) == 0 async def test_add_sticker_to_set_warning(self, bot, monkeypatch, chat_id, recwarn): async def make_assertion(*args, **kwargs): @@ -841,7 +856,9 @@ async def test_add_sticker_to_set_mutually_exclusive(self, bot, chat_id): await bot.add_sticker_to_set(chat_id, "name", "emojis", sticker="something") @pytest.mark.parametrize("local_mode", [True, False]) - async def test_add_sticker_to_set_local_files(self, monkeypatch, bot, chat_id, local_mode): + async def test_add_sticker_to_set_local_files( + self, monkeypatch, bot, chat_id, local_mode, recwarn + ): monkeypatch.setattr(bot, "_local_mode", local_mode) # For just test that the correct paths are passed as we have no local bot API set up test_flag = False @@ -876,7 +893,13 @@ async def make_assertion(_, data, *args, **kwargs): chat_id, "name", sticker=InputSticker(sticker=file, emoji_list=["this"]) ) assert test_flag - assert len(recwarn) in (1, 2) + + warnings = [w for w in recwarn if w.category is not ResourceWarning] + assert len(warnings) == 1 + assert warnings[0].category is PTBDeprecationWarning + assert warnings[0].filename == __file__ + assert str(warnings[0].message).startswith("Since Bot API 6.6, the parameters") + assert "for `add_sticker_to_set` are deprecated" in str(warnings[0].message) @pytest.mark.parametrize("local_mode", [True, False]) async def test_set_sticker_set_thumbnail_local_files( @@ -902,34 +925,27 @@ async def make_assertion(_, data, *args, **kwargs): finally: bot._local_mode = False + @pytest.mark.parametrize("bot_class", ["Bot", "ExtBot"]) async def test_set_sticker_set_thumb_deprecation_warning( - self, monkeypatch, bot, raw_bot, recwarn + self, monkeypatch, bot, raw_bot, recwarn, bot_class ): - ext_bot = bot + bot = bot if bot_class == "ExtBot" else raw_bot async def _post(*args, **kwargs): return True - for bot in (ext_bot, raw_bot): - cls_name = bot.__class__.__name__ - monkeypatch.setattr(bot, "_post", _post) - await bot.set_sticker_set_thumb("name", "user_id", "thumb") - - if isinstance(bot, ExtBot): - # Unfortunately, warnings.catch_warnings doesn't play well with pytest apparently - assert len(recwarn) == 2, f"Wrong warning number for class {cls_name}!" - else: - assert len(recwarn) == 1, f"No warning for class {cls_name}!" + cls_name = bot.__class__.__name__ + monkeypatch.setattr(bot, "_post", _post) + await bot.set_sticker_set_thumb("name", "user_id", "thumb") - assert ( - recwarn[0].category is PTBDeprecationWarning - ), f"Wrong warning for class {cls_name}!" - assert "renamed the method 'setStickerSetThumb' to 'setStickerSetThumbnail'" in str( - recwarn[0].message - ), f"Wrong message for class {cls_name}!" + assert len(recwarn) == 1, f"No warning for class {cls_name}!" + assert recwarn[0].category is PTBDeprecationWarning, f"Wrong warning for class {cls_name}!" + assert "renamed the method 'setStickerSetThumb' to 'setStickerSetThumbnail'" in str( + recwarn[0].message + ), f"Wrong message for class {cls_name}!" - assert recwarn[0].filename == __file__, f"incorrect stacklevel for class {cls_name}!" - recwarn.clear() + assert recwarn[0].filename == __file__, f"incorrect stacklevel for class {cls_name}!" + recwarn.clear() async def test_get_file_instance_method(self, monkeypatch, sticker): async def make_assertion(*_, **kwargs): diff --git a/tests/test_bot.py b/tests/test_bot.py index 1bf8bc12622..94935d520ae 100644 --- a/tests/test_bot.py +++ b/tests/test_bot.py @@ -26,7 +26,6 @@ import socket import time from collections import defaultdict -from pathlib import Path import pytest @@ -81,7 +80,7 @@ from tests.auxil.bot_method_checks import check_defaults_handling from tests.auxil.ci_bots import FALLBACKS from tests.auxil.envvars import GITHUB_ACTION, TEST_WITH_OPT_DEPS -from tests.auxil.files import PROJECT_ROOT_PATH, data_file +from tests.auxil.files import data_file from tests.auxil.networking import expect_bad_request from tests.auxil.pytest_classes import PytestBot, PytestExtBot, make_bot from tests.auxil.slots import mro_slots @@ -1681,9 +1680,7 @@ async def test_http2_runtime_error(self, recwarn): for warning in recwarn: print() print(warning) - assert ( - Path(warning.filename) == PROJECT_ROOT_PATH / "telegram" / "ext" / "_extbot.py" - ), "wrong stacklevel!" + assert warning.filename == __file__, "wrong stacklevel!" assert warning.category is PTBUserWarning From 08db55ea46e7f131cde89045f7921e7b61885b0a Mon Sep 17 00:00:00 2001 From: Hinrich Mahler <22366557+Bibo-Joshi@users.noreply.github.com> Date: Sat, 22 Apr 2023 22:53:17 +0200 Subject: [PATCH 05/10] Add/extend deprecation tests --- telegram/_bot.py | 4 +++- telegram/_utils/warnings_transition.py | 1 + tests/_files/test_animation.py | 18 ++++++++++++++++- tests/_files/test_audio.py | 18 ++++++++++++++++- tests/_files/test_document.py | 18 ++++++++++++++++- tests/_files/test_sticker.py | 27 ++++++++++++++++++-------- tests/_files/test_video.py | 18 ++++++++++++++++- tests/_files/test_videonote.py | 18 ++++++++++++++++- tests/auxil/deprecations.py | 13 +++++++++++++ tests/test_bot.py | 15 ++++++-------- 10 files changed, 127 insertions(+), 23 deletions(-) diff --git a/telegram/_bot.py b/telegram/_bot.py index 61b204061ee..53b3bb0921a 100644 --- a/telegram/_bot.py +++ b/telegram/_bot.py @@ -1165,7 +1165,9 @@ async def send_audio( """ thumbnail_or_thumb: FileInput = warn_about_thumb_return_thumbnail( - deprecated_arg=thumb, new_arg=thumbnail, warn_callback=self._warn + deprecated_arg=thumb, + new_arg=thumbnail, + warn_callback=self._warn, ) data: JSONDict = { "chat_id": chat_id, diff --git a/telegram/_utils/warnings_transition.py b/telegram/_utils/warnings_transition.py index a4578f8dd5c..aa5aeefd99f 100644 --- a/telegram/_utils/warnings_transition.py +++ b/telegram/_utils/warnings_transition.py @@ -93,6 +93,7 @@ def warn_about_deprecated_attr_in_property( deprecated_arg_name="thumb", new_arg_name="thumbnail", bot_api_version="6.6", + stacklevel=4, ) """A helper function to warn about using a deprecated 'thumb' argument and return it or the new 'thumbnail' argument, introduced in API 6.6. diff --git a/tests/_files/test_animation.py b/tests/_files/test_animation.py index 85c5d9c92a3..95fdf03ad2f 100644 --- a/tests/_files/test_animation.py +++ b/tests/_files/test_animation.py @@ -31,7 +31,10 @@ check_shortcut_call, check_shortcut_signature, ) -from tests.auxil.deprecations import check_thumb_deprecation_warnings_for_args_and_attrs +from tests.auxil.deprecations import ( + check_thumb_deprecation_warnings_for_args_and_attrs, + check_thumb_deprececation_warning_for_method_args, +) from tests.auxil.files import data_file from tests.auxil.slots import mro_slots @@ -191,6 +194,19 @@ async def make_assertion(url, request_data: RequestData, *args, **kwargs): monkeypatch.setattr(bot.request, "post", make_assertion) assert await bot.send_animation(animation=animation, chat_id=chat_id) + @pytest.mark.parametrize("bot_class", ["Bot", "ExtBot"]) + async def test_send_animation_thumb_deprecation_warning( + self, recwarn, monkeypatch, bot_class, bot, raw_bot, chat_id, animation + ): + async def make_assertion(url, request_data: RequestData, *args, **kwargs): + return True + + bot = raw_bot if bot_class == "Bot" else bot + + monkeypatch.setattr(bot.request, "post", make_assertion) + await bot.send_animation(chat_id, animation, thumb="thumb") + check_thumb_deprececation_warning_for_method_args(recwarn, __file__) + async def test_send_animation_with_local_files_throws_error_with_different_thumb_and_thumbnail( self, bot, chat_id ): diff --git a/tests/_files/test_audio.py b/tests/_files/test_audio.py index b8a4c44a1b2..94f207fb065 100644 --- a/tests/_files/test_audio.py +++ b/tests/_files/test_audio.py @@ -31,7 +31,10 @@ check_shortcut_call, check_shortcut_signature, ) -from tests.auxil.deprecations import check_thumb_deprecation_warnings_for_args_and_attrs +from tests.auxil.deprecations import ( + check_thumb_deprecation_warnings_for_args_and_attrs, + check_thumb_deprececation_warning_for_method_args, +) from tests.auxil.files import data_file from tests.auxil.slots import mro_slots @@ -158,6 +161,19 @@ async def make_assertion(url, request_data: RequestData, *args, **kwargs): monkeypatch.setattr(bot.request, "post", make_assertion) assert await bot.send_audio(audio=audio, chat_id=chat_id) + @pytest.mark.parametrize("bot_class", ["Bot", "ExtBot"]) + async def test_send_audio_thumb_deprecation_warning( + self, recwarn, monkeypatch, bot_class, bot, raw_bot, chat_id, audio + ): + async def make_assertion(url, request_data: RequestData, *args, **kwargs): + return True + + bot = raw_bot if bot_class == "Bot" else bot + + monkeypatch.setattr(bot.request, "post", make_assertion) + await bot.send_audio(chat_id, audio, thumb="thumb") + check_thumb_deprececation_warning_for_method_args(recwarn, __file__) + async def test_send_audio_custom_filename(self, bot, chat_id, audio_file, monkeypatch): async def make_assertion(url, request_data: RequestData, *args, **kwargs): return list(request_data.multipart_data.values())[0][0] == "custom_filename" diff --git a/tests/_files/test_document.py b/tests/_files/test_document.py index a447aeb81b6..5c84818ecfb 100644 --- a/tests/_files/test_document.py +++ b/tests/_files/test_document.py @@ -31,7 +31,10 @@ check_shortcut_call, check_shortcut_signature, ) -from tests.auxil.deprecations import check_thumb_deprecation_warnings_for_args_and_attrs +from tests.auxil.deprecations import ( + check_thumb_deprecation_warnings_for_args_and_attrs, + check_thumb_deprececation_warning_for_method_args, +) from tests.auxil.files import data_file from tests.auxil.slots import mro_slots @@ -157,6 +160,19 @@ async def make_assertion(url, request_data: RequestData, *args, **kwargs): assert message + @pytest.mark.parametrize("bot_class", ["Bot", "ExtBot"]) + async def test_send_document_thumb_deprecation_warning( + self, recwarn, monkeypatch, bot_class, bot, raw_bot, chat_id, document + ): + async def make_assertion(url, request_data: RequestData, *args, **kwargs): + return True + + bot = raw_bot if bot_class == "Bot" else bot + + monkeypatch.setattr(bot.request, "post", make_assertion) + await bot.send_document(chat_id, document, thumb="thumb") + check_thumb_deprececation_warning_for_method_args(recwarn, __file__) + @pytest.mark.parametrize("local_mode", [True, False]) async def test_send_document_local_files(self, monkeypatch, bot, chat_id, local_mode): try: diff --git a/tests/_files/test_sticker.py b/tests/_files/test_sticker.py index d674260b5c5..e8e9c653bd5 100644 --- a/tests/_files/test_sticker.py +++ b/tests/_files/test_sticker.py @@ -639,10 +639,14 @@ def test_equality(self): assert a != e assert hash(a) != hash(e) - async def test_upload_sticker_file_warning(self, bot, monkeypatch, chat_id, recwarn): + @pytest.mark.parametrize("bot_class", ["Bot", "ExtBot"]) + async def test_upload_sticker_file_warning( + self, bot, raw_bot, monkeypatch, chat_id, recwarn, bot_class + ): async def make_assertion(*args, **kwargs): return {"file_id": "file_id", "file_unique_id": "file_unique_id"} + bot = raw_bot if bot_class == "Bot" else bot monkeypatch.setattr(bot, "_post", make_assertion) await bot.upload_sticker_file(chat_id, "png_sticker_file_id") @@ -703,10 +707,14 @@ async def make_assertion(_, data, *args, **kwargs): finally: bot._local_mode = False - async def test_create_new_sticker_set_warning(self, bot, monkeypatch, chat_id, recwarn): + @pytest.mark.parametrize("bot_class", ["Bot", "ExtBot"]) + async def test_create_new_sticker_set_warning( + self, bot, raw_bot, bot_class, monkeypatch, chat_id, recwarn + ): async def make_assertion(*args, **kwargs): return True + bot = raw_bot if bot_class == "Bot" else bot monkeypatch.setattr(bot, "_post", make_assertion) await bot.create_new_sticker_set(chat_id, "name", "title", "some_str_emoji") @@ -835,10 +843,14 @@ async def make_assertion_new_params(_, data, *args, **kwargs): ) assert len(recwarn) == 0 - async def test_add_sticker_to_set_warning(self, bot, monkeypatch, chat_id, recwarn): + @pytest.mark.parametrize("bot_class", ["Bot", "ExtBot"]) + async def test_add_sticker_to_set_warning( + self, bot, raw_bot, monkeypatch, bot_class, chat_id, recwarn + ): async def make_assertion(*args, **kwargs): return True + bot = raw_bot if bot_class == "Bot" else bot monkeypatch.setattr(bot, "_post", make_assertion) await bot.add_sticker_to_set(chat_id, "name", "emoji", "fake_file_id") @@ -934,17 +946,16 @@ async def test_set_sticker_set_thumb_deprecation_warning( async def _post(*args, **kwargs): return True - cls_name = bot.__class__.__name__ monkeypatch.setattr(bot, "_post", _post) await bot.set_sticker_set_thumb("name", "user_id", "thumb") - assert len(recwarn) == 1, f"No warning for class {cls_name}!" - assert recwarn[0].category is PTBDeprecationWarning, f"Wrong warning for class {cls_name}!" + assert len(recwarn) == 1 + assert recwarn[0].category is PTBDeprecationWarning assert "renamed the method 'setStickerSetThumb' to 'setStickerSetThumbnail'" in str( recwarn[0].message - ), f"Wrong message for class {cls_name}!" + ) - assert recwarn[0].filename == __file__, f"incorrect stacklevel for class {cls_name}!" + assert recwarn[0].filename == __file__, "incorrect stacklevel!" recwarn.clear() async def test_get_file_instance_method(self, monkeypatch, sticker): diff --git a/tests/_files/test_video.py b/tests/_files/test_video.py index 5d10d81d88d..789bd8cb026 100644 --- a/tests/_files/test_video.py +++ b/tests/_files/test_video.py @@ -31,7 +31,10 @@ check_shortcut_call, check_shortcut_signature, ) -from tests.auxil.deprecations import check_thumb_deprecation_warnings_for_args_and_attrs +from tests.auxil.deprecations import ( + check_thumb_deprecation_warnings_for_args_and_attrs, + check_thumb_deprececation_warning_for_method_args, +) from tests.auxil.files import data_file from tests.auxil.slots import mro_slots @@ -171,6 +174,19 @@ async def make_assertion(url, request_data: RequestData, *args, **kwargs): monkeypatch.setattr(bot.request, "post", make_assertion) assert await bot.send_video(chat_id, video=video) + @pytest.mark.parametrize("bot_class", ["Bot", "ExtBot"]) + async def test_send_video_thumb_deprecation_warning( + self, recwarn, monkeypatch, bot_class, bot, raw_bot, chat_id, video + ): + async def make_assertion(url, request_data: RequestData, *args, **kwargs): + return True + + bot = raw_bot if bot_class == "Bot" else bot + + monkeypatch.setattr(bot.request, "post", make_assertion) + await bot.send_video(chat_id, video, thumb="thumb") + check_thumb_deprececation_warning_for_method_args(recwarn, __file__) + async def test_send_video_custom_filename(self, bot, chat_id, video_file, monkeypatch): async def make_assertion(url, request_data: RequestData, *args, **kwargs): return list(request_data.multipart_data.values())[0][0] == "custom_filename" diff --git a/tests/_files/test_videonote.py b/tests/_files/test_videonote.py index 5ce780fe1dc..435d5728f07 100644 --- a/tests/_files/test_videonote.py +++ b/tests/_files/test_videonote.py @@ -30,7 +30,10 @@ check_shortcut_call, check_shortcut_signature, ) -from tests.auxil.deprecations import check_thumb_deprecation_warnings_for_args_and_attrs +from tests.auxil.deprecations import ( + check_thumb_deprecation_warnings_for_args_and_attrs, + check_thumb_deprececation_warning_for_method_args, +) from tests.auxil.files import data_file from tests.auxil.slots import mro_slots @@ -149,6 +152,19 @@ async def make_assertion(url, request_data: RequestData, *args, **kwargs): monkeypatch.setattr(bot.request, "post", make_assertion) assert await bot.send_video_note(chat_id, video_note=video_note) + @pytest.mark.parametrize("bot_class", ["Bot", "ExtBot"]) + async def test_send_video_note_thumb_deprecation_warning( + self, recwarn, monkeypatch, bot_class, bot, raw_bot, chat_id, video_note + ): + async def make_assertion(url, request_data: RequestData, *args, **kwargs): + return True + + bot = raw_bot if bot_class == "Bot" else bot + + monkeypatch.setattr(bot.request, "post", make_assertion) + await bot.send_video_note(chat_id, video_note, thumb="thumb") + check_thumb_deprececation_warning_for_method_args(recwarn, __file__) + async def test_send_video_note_custom_filename( self, bot, chat_id, video_note_file, monkeypatch ): diff --git a/tests/auxil/deprecations.py b/tests/auxil/deprecations.py index 90c7e057193..1a2f3b1503b 100644 --- a/tests/auxil/deprecations.py +++ b/tests/auxil/deprecations.py @@ -72,3 +72,16 @@ def check_thumb_deprecation_warnings_for_args_and_attrs( ) return True + + +def check_thumb_deprececation_warning_for_method_args( + recwarn: WarningsRecorder, + calling_file: str, + deprecated_name: str = "thumb", + new_name: str = "thumbnail", +): + """Similar as `check_thumb_deprecation_warnings_for_args_and_attrs`, but for bot methods.""" + assert len(recwarn) == 1 + assert recwarn[0].category is PTBDeprecationWarning + assert recwarn[0].filename == calling_file + assert f"argument '{deprecated_name}' to '{new_name}'" in str(recwarn[0].message) diff --git a/tests/test_bot.py b/tests/test_bot.py index 94935d520ae..063c8b93f2d 100644 --- a/tests/test_bot.py +++ b/tests/test_bot.py @@ -1651,22 +1651,21 @@ async def post(*args, **kwargs): bot.callback_data_cache.clear_callback_data() bot.callback_data_cache.clear_callback_queries() - async def test_http2_runtime_error(self, recwarn): - Bot("12345:ABCDE", base_url="http://", request=HTTPXRequest(http_version="2")) - Bot( + @pytest.mark.parametrize("bot_class", [Bot, ExtBot]) + async def test_http2_runtime_error(self, recwarn, bot_class): + bot_class("12345:ABCDE", base_url="http://", request=HTTPXRequest(http_version="2")) + bot_class( "12345:ABCDE", base_url="http://", get_updates_request=HTTPXRequest(http_version="2"), ) - Bot( + bot_class( "12345:ABCDE", base_url="http://", request=HTTPXRequest(http_version="2"), get_updates_request=HTTPXRequest(http_version="2"), ) - # this exists to make sure the error is also raised by extbot - ExtBot("12345:ABCDE", base_url="http://", request=HTTPXRequest(http_version="2")) - assert len(recwarn) == 4 + assert len(recwarn) == 3 assert "You set the HTTP version for the request HTTPXRequest instance" in str( recwarn[0].message ) @@ -1678,8 +1677,6 @@ async def test_http2_runtime_error(self, recwarn): "instance" in str(recwarn[2].message) ) for warning in recwarn: - print() - print(warning) assert warning.filename == __file__, "wrong stacklevel!" assert warning.category is PTBUserWarning From c85dd4ef18b46fb90331332717a35c7c453c47cf Mon Sep 17 00:00:00 2001 From: Hinrich Mahler <22366557+Bibo-Joshi@users.noreply.github.com> Date: Sat, 22 Apr 2023 23:33:13 +0200 Subject: [PATCH 06/10] fix tests --- telegram/_bot.py | 17 ++++++++++++++--- telegram/_utils/warnings_transition.py | 1 - 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/telegram/_bot.py b/telegram/_bot.py index 53b3bb0921a..9c039c4fda6 100644 --- a/telegram/_bot.py +++ b/telegram/_bot.py @@ -1168,6 +1168,7 @@ async def send_audio( deprecated_arg=thumb, new_arg=thumbnail, warn_callback=self._warn, + stacklevel=4, ) data: JSONDict = { "chat_id": chat_id, @@ -1304,7 +1305,10 @@ async def send_document( """ thumbnail_or_thumb: FileInput = warn_about_thumb_return_thumbnail( - deprecated_arg=thumb, new_arg=thumbnail, warn_callback=self._warn + deprecated_arg=thumb, + new_arg=thumbnail, + warn_callback=self._warn, + stacklevel=4, ) data: JSONDict = { @@ -1541,7 +1545,10 @@ async def send_video( """ thumbnail_or_thumb: FileInput = warn_about_thumb_return_thumbnail( - deprecated_arg=thumb, new_arg=thumbnail, warn_callback=self._warn + deprecated_arg=thumb, + new_arg=thumbnail, + warn_callback=self._warn, + stacklevel=4, ) data: JSONDict = { "chat_id": chat_id, @@ -1674,7 +1681,10 @@ async def send_video_note( """ thumbnail_or_thumb: FileInput = warn_about_thumb_return_thumbnail( - deprecated_arg=thumb, new_arg=thumbnail, warn_callback=self._warn + deprecated_arg=thumb, + new_arg=thumbnail, + warn_callback=self._warn, + stacklevel=4, ) data: JSONDict = { "chat_id": chat_id, @@ -1818,6 +1828,7 @@ async def send_animation( deprecated_arg=thumb, new_arg=thumbnail, warn_callback=self._warn, + stacklevel=4, ) data: JSONDict = { "chat_id": chat_id, diff --git a/telegram/_utils/warnings_transition.py b/telegram/_utils/warnings_transition.py index aa5aeefd99f..a4578f8dd5c 100644 --- a/telegram/_utils/warnings_transition.py +++ b/telegram/_utils/warnings_transition.py @@ -93,7 +93,6 @@ def warn_about_deprecated_attr_in_property( deprecated_arg_name="thumb", new_arg_name="thumbnail", bot_api_version="6.6", - stacklevel=4, ) """A helper function to warn about using a deprecated 'thumb' argument and return it or the new 'thumbnail' argument, introduced in API 6.6. From 22a188389841fb029e7f9b7ad85a1cf88a40ff9b Mon Sep 17 00:00:00 2001 From: Hinrich Mahler <22366557+Bibo-Joshi@users.noreply.github.com> Date: Sun, 23 Apr 2023 18:34:59 +0200 Subject: [PATCH 07/10] Review: * Fix typo * Make sure to only use the local information on stacklevel --- telegram/_bot.py | 10 +++---- telegram/_files/_basethumbedmedium.py | 2 +- telegram/_utils/warnings_transition.py | 36 ++++++++++++++++---------- tests/_files/test_animation.py | 4 +-- tests/_files/test_audio.py | 4 +-- tests/_files/test_document.py | 4 +-- tests/_files/test_video.py | 4 +-- tests/_files/test_videonote.py | 4 +-- tests/auxil/deprecations.py | 5 ++-- 9 files changed, 40 insertions(+), 33 deletions(-) diff --git a/telegram/_bot.py b/telegram/_bot.py index 9c039c4fda6..16c67d39f5c 100644 --- a/telegram/_bot.py +++ b/telegram/_bot.py @@ -1168,7 +1168,7 @@ async def send_audio( deprecated_arg=thumb, new_arg=thumbnail, warn_callback=self._warn, - stacklevel=4, + stacklevel=3, ) data: JSONDict = { "chat_id": chat_id, @@ -1308,7 +1308,7 @@ async def send_document( deprecated_arg=thumb, new_arg=thumbnail, warn_callback=self._warn, - stacklevel=4, + stacklevel=3, ) data: JSONDict = { @@ -1548,7 +1548,7 @@ async def send_video( deprecated_arg=thumb, new_arg=thumbnail, warn_callback=self._warn, - stacklevel=4, + stacklevel=3, ) data: JSONDict = { "chat_id": chat_id, @@ -1684,7 +1684,7 @@ async def send_video_note( deprecated_arg=thumb, new_arg=thumbnail, warn_callback=self._warn, - stacklevel=4, + stacklevel=3, ) data: JSONDict = { "chat_id": chat_id, @@ -1828,7 +1828,7 @@ async def send_animation( deprecated_arg=thumb, new_arg=thumbnail, warn_callback=self._warn, - stacklevel=4, + stacklevel=3, ) data: JSONDict = { "chat_id": chat_id, diff --git a/telegram/_files/_basethumbedmedium.py b/telegram/_files/_basethumbedmedium.py index 08ca07e5863..a23e8b8d42e 100644 --- a/telegram/_files/_basethumbedmedium.py +++ b/telegram/_files/_basethumbedmedium.py @@ -93,7 +93,7 @@ def __init__( deprecated_arg_name="thumb", new_arg_name="thumbnail", bot_api_version="6.6", - stacklevel=4, + stacklevel=3, ) @property diff --git a/telegram/_utils/warnings_transition.py b/telegram/_utils/warnings_transition.py index a4578f8dd5c..2e3b0ebad36 100644 --- a/telegram/_utils/warnings_transition.py +++ b/telegram/_utils/warnings_transition.py @@ -23,7 +23,6 @@ .. versionadded:: 20.2 """ -import functools from typing import Any, Callable, Type from telegram._utils.warnings import warn @@ -38,7 +37,7 @@ def warn_about_deprecated_arg_return_new_arg( deprecated_arg_name: str, new_arg_name: str, bot_api_version: str, - stacklevel: int = 3, + stacklevel: int = 2, warn_callback: Callable[[str, Type[Warning], int], None] = warn, ) -> Any: """A helper function for the transition in API when argument is renamed. @@ -63,7 +62,7 @@ def warn_about_deprecated_arg_return_new_arg( f"Bot API {bot_api_version} renamed the argument '{deprecated_arg_name}' to " f"'{new_arg_name}'.", PTBDeprecationWarning, - stacklevel, + stacklevel + 1, ) return deprecated_arg @@ -74,7 +73,7 @@ def warn_about_deprecated_attr_in_property( deprecated_attr_name: str, new_attr_name: str, bot_api_version: str, - stacklevel: int = 3, + stacklevel: int = 2, ) -> None: """A helper function for the transition in API when attribute is renamed. Call from properties. @@ -84,16 +83,25 @@ def warn_about_deprecated_attr_in_property( f"Bot API {bot_api_version} renamed the attribute '{deprecated_attr_name}' to " f"'{new_attr_name}'.", PTBDeprecationWarning, - stacklevel=stacklevel, + stacklevel=stacklevel + 1, ) -warn_about_thumb_return_thumbnail = functools.partial( - warn_about_deprecated_arg_return_new_arg, - deprecated_arg_name="thumb", - new_arg_name="thumbnail", - bot_api_version="6.6", -) -"""A helper function to warn about using a deprecated 'thumb' argument and return it or the new -'thumbnail' argument, introduced in API 6.6. -""" +def warn_about_thumb_return_thumbnail( + deprecated_arg: Any, + new_arg: Any, + stacklevel: int = 2, + warn_callback: Callable[[str, Type[Warning], int], None] = warn, +) -> Any: + """A helper function to warn about using a deprecated 'thumb' argument and return it or the + new 'thumbnail' argument, introduced in API 6.6. + """ + return warn_about_deprecated_arg_return_new_arg( + deprecated_arg=deprecated_arg, + new_arg=new_arg, + warn_callback=warn_callback, + deprecated_arg_name="thumb", + new_arg_name="thumbnail", + bot_api_version="6.6", + stacklevel=stacklevel + 1, + ) diff --git a/tests/_files/test_animation.py b/tests/_files/test_animation.py index 95fdf03ad2f..2761f58002d 100644 --- a/tests/_files/test_animation.py +++ b/tests/_files/test_animation.py @@ -32,8 +32,8 @@ check_shortcut_signature, ) from tests.auxil.deprecations import ( + check_thumb_deprecation_warning_for_method_args, check_thumb_deprecation_warnings_for_args_and_attrs, - check_thumb_deprececation_warning_for_method_args, ) from tests.auxil.files import data_file from tests.auxil.slots import mro_slots @@ -205,7 +205,7 @@ async def make_assertion(url, request_data: RequestData, *args, **kwargs): monkeypatch.setattr(bot.request, "post", make_assertion) await bot.send_animation(chat_id, animation, thumb="thumb") - check_thumb_deprececation_warning_for_method_args(recwarn, __file__) + check_thumb_deprecation_warning_for_method_args(recwarn, __file__) async def test_send_animation_with_local_files_throws_error_with_different_thumb_and_thumbnail( self, bot, chat_id diff --git a/tests/_files/test_audio.py b/tests/_files/test_audio.py index 94f207fb065..2817710ee9d 100644 --- a/tests/_files/test_audio.py +++ b/tests/_files/test_audio.py @@ -32,8 +32,8 @@ check_shortcut_signature, ) from tests.auxil.deprecations import ( + check_thumb_deprecation_warning_for_method_args, check_thumb_deprecation_warnings_for_args_and_attrs, - check_thumb_deprececation_warning_for_method_args, ) from tests.auxil.files import data_file from tests.auxil.slots import mro_slots @@ -172,7 +172,7 @@ async def make_assertion(url, request_data: RequestData, *args, **kwargs): monkeypatch.setattr(bot.request, "post", make_assertion) await bot.send_audio(chat_id, audio, thumb="thumb") - check_thumb_deprececation_warning_for_method_args(recwarn, __file__) + check_thumb_deprecation_warning_for_method_args(recwarn, __file__) async def test_send_audio_custom_filename(self, bot, chat_id, audio_file, monkeypatch): async def make_assertion(url, request_data: RequestData, *args, **kwargs): diff --git a/tests/_files/test_document.py b/tests/_files/test_document.py index 5c84818ecfb..85cfaabab42 100644 --- a/tests/_files/test_document.py +++ b/tests/_files/test_document.py @@ -32,8 +32,8 @@ check_shortcut_signature, ) from tests.auxil.deprecations import ( + check_thumb_deprecation_warning_for_method_args, check_thumb_deprecation_warnings_for_args_and_attrs, - check_thumb_deprececation_warning_for_method_args, ) from tests.auxil.files import data_file from tests.auxil.slots import mro_slots @@ -171,7 +171,7 @@ async def make_assertion(url, request_data: RequestData, *args, **kwargs): monkeypatch.setattr(bot.request, "post", make_assertion) await bot.send_document(chat_id, document, thumb="thumb") - check_thumb_deprececation_warning_for_method_args(recwarn, __file__) + check_thumb_deprecation_warning_for_method_args(recwarn, __file__) @pytest.mark.parametrize("local_mode", [True, False]) async def test_send_document_local_files(self, monkeypatch, bot, chat_id, local_mode): diff --git a/tests/_files/test_video.py b/tests/_files/test_video.py index 789bd8cb026..773f7e21006 100644 --- a/tests/_files/test_video.py +++ b/tests/_files/test_video.py @@ -32,8 +32,8 @@ check_shortcut_signature, ) from tests.auxil.deprecations import ( + check_thumb_deprecation_warning_for_method_args, check_thumb_deprecation_warnings_for_args_and_attrs, - check_thumb_deprececation_warning_for_method_args, ) from tests.auxil.files import data_file from tests.auxil.slots import mro_slots @@ -185,7 +185,7 @@ async def make_assertion(url, request_data: RequestData, *args, **kwargs): monkeypatch.setattr(bot.request, "post", make_assertion) await bot.send_video(chat_id, video, thumb="thumb") - check_thumb_deprececation_warning_for_method_args(recwarn, __file__) + check_thumb_deprecation_warning_for_method_args(recwarn, __file__) async def test_send_video_custom_filename(self, bot, chat_id, video_file, monkeypatch): async def make_assertion(url, request_data: RequestData, *args, **kwargs): diff --git a/tests/_files/test_videonote.py b/tests/_files/test_videonote.py index 435d5728f07..aee0a042d17 100644 --- a/tests/_files/test_videonote.py +++ b/tests/_files/test_videonote.py @@ -31,8 +31,8 @@ check_shortcut_signature, ) from tests.auxil.deprecations import ( + check_thumb_deprecation_warning_for_method_args, check_thumb_deprecation_warnings_for_args_and_attrs, - check_thumb_deprececation_warning_for_method_args, ) from tests.auxil.files import data_file from tests.auxil.slots import mro_slots @@ -163,7 +163,7 @@ async def make_assertion(url, request_data: RequestData, *args, **kwargs): monkeypatch.setattr(bot.request, "post", make_assertion) await bot.send_video_note(chat_id, video_note, thumb="thumb") - check_thumb_deprececation_warning_for_method_args(recwarn, __file__) + check_thumb_deprecation_warning_for_method_args(recwarn, __file__) async def test_send_video_note_custom_filename( self, bot, chat_id, video_note_file, monkeypatch diff --git a/tests/auxil/deprecations.py b/tests/auxil/deprecations.py index 1a2f3b1503b..224ecdc4067 100644 --- a/tests/auxil/deprecations.py +++ b/tests/auxil/deprecations.py @@ -19,7 +19,6 @@ from _pytest.recwarn import WarningsRecorder -from telegram._utils import warnings_transition from telegram.warnings import PTBDeprecationWarning @@ -68,13 +67,13 @@ def check_thumb_deprecation_warnings_for_args_and_attrs( assert recwarn[i].filename == calling_file, ( f'Warning for {names[i]} ("{str(recwarn[i].message)}") was issued by file ' - f"{recwarn[i].filename}, expected {calling_file} or {warnings_transition.__file__}" + f"{recwarn[i].filename}, expected {calling_file}" ) return True -def check_thumb_deprececation_warning_for_method_args( +def check_thumb_deprecation_warning_for_method_args( recwarn: WarningsRecorder, calling_file: str, deprecated_name: str = "thumb", From 92c4d08c145308ead78ce01ab62f63961ce35745 Mon Sep 17 00:00:00 2001 From: Hinrich Mahler <22366557+Bibo-Joshi@users.noreply.github.com> Date: Mon, 24 Apr 2023 21:30:25 +0200 Subject: [PATCH 08/10] Fix a typo --- telegram/_bot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/telegram/_bot.py b/telegram/_bot.py index 16c67d39f5c..2055d5a974d 100644 --- a/telegram/_bot.py +++ b/telegram/_bot.py @@ -342,7 +342,7 @@ def private_key(self) -> Optional[Any]: def _warn( cls, message: str, category: Type[Warning] = PTBUserWarning, stacklevel: int = 0 ) -> None: - """Convencience method to issue a warning. This method is here mostly to make it easier + """Convenience method to issue a warning. This method is here mostly to make it easier for ExtBot to add 1 level to all warning calls.""" warn(message=message, category=category, stacklevel=stacklevel + 1) From f63399e9a5b42aa255fb629aaae3874c2a157057 Mon Sep 17 00:00:00 2001 From: Hinrich Mahler <22366557+Bibo-Joshi@users.noreply.github.com> Date: Wed, 26 Apr 2023 21:46:31 +0200 Subject: [PATCH 09/10] DS --- telegram/_bot.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/telegram/_bot.py b/telegram/_bot.py index 2055d5a974d..e7b060f4a06 100644 --- a/telegram/_bot.py +++ b/telegram/_bot.py @@ -343,7 +343,8 @@ def _warn( cls, message: str, category: Type[Warning] = PTBUserWarning, stacklevel: int = 0 ) -> None: """Convenience method to issue a warning. This method is here mostly to make it easier - for ExtBot to add 1 level to all warning calls.""" + for ExtBot to add 1 level to all warning calls. + """ warn(message=message, category=category, stacklevel=stacklevel + 1) def __reduce__(self) -> NoReturn: From 2d6cb4112b9c3468bb8f32b9e2ed89b09e680f40 Mon Sep 17 00:00:00 2001 From: Hinrich Mahler <22366557+Bibo-Joshi@users.noreply.github.com> Date: Wed, 26 Apr 2023 22:19:46 +0200 Subject: [PATCH 10/10] More DS --- telegram/ext/_extbot.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/telegram/ext/_extbot.py b/telegram/ext/_extbot.py index 373cebff30d..a2995a46783 100644 --- a/telegram/ext/_extbot.py +++ b/telegram/ext/_extbot.py @@ -239,7 +239,8 @@ def _warn( cls, message: str, category: Type[Warning] = PTBUserWarning, stacklevel: int = 0 ) -> None: """We override this method to add one more level to the stacklevel, so that the warning - points to the user's code, not to the PTB code.""" + points to the user's code, not to the PTB code. + """ super()._warn(message=message, category=category, stacklevel=stacklevel + 2) @property