Fix Handling of Defaults for InputPaidMedia (#4761)

Co-authored-by: Nikita Grogolev <ngrogolev@leantech.ai>
This commit is contained in:
ngrogolev
2025-04-19 18:02:43 +03:00
committed by GitHub
parent c6e12b1958
commit 17ae6a7028
4 changed files with 23 additions and 6 deletions
@@ -0,0 +1,6 @@
bugfixes = "Fix Handling of ``Defaults`` for ``InputPaidMedia``"
[[pull_requests]]
uid = "4761"
author_uid = "ngrogolev"
closes_threads = ["4753"]
+6 -2
View File
@@ -62,6 +62,7 @@ from telegram import (
InlineKeyboardMarkup,
InlineQueryResultsButton,
InputMedia,
InputPaidMedia,
InputPollOption,
LinkPreviewOptions,
Location,
@@ -114,7 +115,6 @@ if TYPE_CHECKING:
InputMediaDocument,
InputMediaPhoto,
InputMediaVideo,
InputPaidMedia,
InputSticker,
LabeledPrice,
MessageEntity,
@@ -466,7 +466,11 @@ class ExtBot(Bot, Generic[RLARGS]):
with copied_val._unfrozen():
copied_val.parse_mode = self.defaults.parse_mode
data[key] = copied_val
elif key == "media" and isinstance(val, Sequence):
elif (
key == "media"
and isinstance(val, Sequence)
and not isinstance(val[0], InputPaidMedia)
):
# Copy objects as not to edit them in-place
copy_list = [copy(media) for media in val]
for media in copy_list:
+10 -3
View File
@@ -35,6 +35,7 @@ from telegram import (
InlineQueryResultArticle,
InlineQueryResultCachedPhoto,
InputMediaPhoto,
InputPaidMediaPhoto,
InputTextMessageContent,
LinkPreviewOptions,
ReplyParameters,
@@ -285,8 +286,13 @@ def build_kwargs(
elif name in ["prices", "commands", "errors"]:
kws[name] = []
elif name == "media":
media = InputMediaPhoto("media", parse_mode=manually_passed_value)
if "list" in str(param.annotation).lower():
if "star_count" in signature.parameters:
media = InputPaidMediaPhoto("media")
else:
media = InputMediaPhoto("media", parse_mode=manually_passed_value)
param_annotation = str(param.annotation).lower()
if "sequence" in param_annotation or "list" in param_annotation:
kws[name] = [media]
else:
kws[name] = media
@@ -507,7 +513,8 @@ async def make_assertion(
)
media = data.pop("media", None)
if media:
paid_media = media and data.pop("star_count", None)
if media and not paid_media:
if isinstance(media, dict) and isinstance(media.get("type", None), InputMediaType):
check_input_media(media)
else:
+1 -1
View File
@@ -543,7 +543,7 @@ class TestBotWithoutRequest:
if bot_method_name.replace("_", "").lower() != "getupdates" and bot_class is ExtBot:
assert rate_arg in param_names, f"{bot_method} is missing the parameter `{rate_arg}`"
@bot_methods(ext_bot=False)
@bot_methods()
async def test_defaults_handling(
self,
bot_class,