Skip to content

Fix: InputPaidMedia default parse_mode insertation #4761

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions changes/unreleased/4761.mmsngFA6b4ccdEzEpFTZS3.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
bugfixes = "Fix Handling of ``Defaults`` for ``InputPaidMedia``"

[[pull_requests]]
uid = "4761"
author_uid = "ngrogolev"
closes_threads = ["4753"]
8 changes: 6 additions & 2 deletions telegram/ext/_extbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
InlineKeyboardMarkup,
InlineQueryResultsButton,
InputMedia,
InputPaidMedia,
InputPollOption,
LinkPreviewOptions,
Location,
Expand Down Expand Up @@ -114,7 +115,6 @@
InputMediaDocument,
InputMediaPhoto,
InputMediaVideo,
InputPaidMedia,
InputSticker,
LabeledPrice,
MessageEntity,
Expand Down Expand Up @@ -466,7 +466,11 @@ def _insert_defaults(self, data: dict[str, object]) -> None:
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:
Expand Down
13 changes: 10 additions & 3 deletions tests/auxil/bot_method_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
InlineQueryResultArticle,
InlineQueryResultCachedPhoto,
InputMediaPhoto,
InputPaidMediaPhoto,
InputTextMessageContent,
LinkPreviewOptions,
ReplyParameters,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -507,7 +513,8 @@ def check_input_media(m: dict):
)

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:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ def test_api_kwargs_and_timeouts_present(self, bot_class, bot_method_name, bot_m
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,
Expand Down
Loading