Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Simplify Handling of Empty Data in TelegramObject.de_json and Friends #4617

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
merged 17 commits into from
Jan 14, 2025
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ docs/_build/
# PyBuilder
target/
.idea/
.run/

# Sublime Text 2
*.sublime*
Expand Down
54 changes: 27 additions & 27 deletions telegram/_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -901,7 +901,7 @@ async def get_me(
api_kwargs=api_kwargs,
)
self._bot_user = User.de_json(result, self)
return self._bot_user # type: ignore[return-value]
return self._bot_user

async def send_message(
self,
Expand Down Expand Up @@ -3689,7 +3689,7 @@ async def save_prepared_inline_message(
"allow_group_chats": allow_group_chats,
"allow_channel_chats": allow_channel_chats,
}
return PreparedInlineMessage.de_json( # type: ignore[return-value]
return PreparedInlineMessage.de_json(
await self._post(
"savePreparedInlineMessage",
data,
Expand Down Expand Up @@ -3744,7 +3744,7 @@ async def get_user_profile_photos(
api_kwargs=api_kwargs,
)

return UserProfilePhotos.de_json(result, self) # type: ignore[return-value]
return UserProfilePhotos.de_json(result, self)

async def get_file(
self,
Expand Down Expand Up @@ -3809,7 +3809,7 @@ async def get_file(
if file_path and not is_local_file(file_path):
result["file_path"] = f"{self._base_file_url}/{file_path}"

return File.de_json(result, self) # type: ignore[return-value]
return File.de_json(result, self)

async def ban_chat_member(
self,
Expand Down Expand Up @@ -4729,7 +4729,7 @@ async def get_chat(
api_kwargs=api_kwargs,
)

return ChatFullInfo.de_json(result, self) # type: ignore[return-value]
return ChatFullInfo.de_json(result, self)

async def get_chat_administrators(
self,
Expand Down Expand Up @@ -4842,7 +4842,7 @@ async def get_chat_member(
pool_timeout=pool_timeout,
api_kwargs=api_kwargs,
)
return ChatMember.de_json(result, self) # type: ignore[return-value]
return ChatMember.de_json(result, self)

async def set_chat_sticker_set(
self,
Expand Down Expand Up @@ -4937,7 +4937,7 @@ async def get_webhook_info(
pool_timeout=pool_timeout,
api_kwargs=api_kwargs,
)
return WebhookInfo.de_json(result, self) # type: ignore[return-value]
return WebhookInfo.de_json(result, self)

async def set_game_score(
self,
Expand Down Expand Up @@ -5444,7 +5444,7 @@ async def answer_web_app_query(
api_kwargs=api_kwargs,
)

return SentWebAppMessage.de_json(api_result, self) # type: ignore[return-value]
return SentWebAppMessage.de_json(api_result, self)

async def restrict_chat_member(
self,
Expand Down Expand Up @@ -5858,7 +5858,7 @@ async def create_chat_invite_link(
api_kwargs=api_kwargs,
)

return ChatInviteLink.de_json(result, self) # type: ignore[return-value]
return ChatInviteLink.de_json(result, self)

async def edit_chat_invite_link(
self,
Expand Down Expand Up @@ -5937,7 +5937,7 @@ async def edit_chat_invite_link(
api_kwargs=api_kwargs,
)

return ChatInviteLink.de_json(result, self) # type: ignore[return-value]
return ChatInviteLink.de_json(result, self)

async def revoke_chat_invite_link(
self,
Expand Down Expand Up @@ -5984,7 +5984,7 @@ async def revoke_chat_invite_link(
api_kwargs=api_kwargs,
)

return ChatInviteLink.de_json(result, self) # type: ignore[return-value]
return ChatInviteLink.de_json(result, self)

async def approve_chat_join_request(
self,
Expand Down Expand Up @@ -6456,7 +6456,7 @@ async def get_sticker_set(
pool_timeout=pool_timeout,
api_kwargs=api_kwargs,
)
return StickerSet.de_json(result, self) # type: ignore[return-value]
return StickerSet.de_json(result, self)

async def get_custom_emoji_stickers(
self,
Expand Down Expand Up @@ -6559,7 +6559,7 @@ async def upload_sticker_file(
pool_timeout=pool_timeout,
api_kwargs=api_kwargs,
)
return File.de_json(result, self) # type: ignore[return-value]
return File.de_json(result, self)

async def add_sticker_to_set(
self,
Expand Down Expand Up @@ -7416,7 +7416,7 @@ async def stop_poll(
pool_timeout=pool_timeout,
api_kwargs=api_kwargs,
)
return Poll.de_json(result, self) # type: ignore[return-value]
return Poll.de_json(result, self)

async def send_dice(
self,
Expand Down Expand Up @@ -7572,7 +7572,7 @@ async def get_my_default_administrator_rights(
api_kwargs=api_kwargs,
)

return ChatAdministratorRights.de_json(result, self) # type: ignore[return-value]
return ChatAdministratorRights.de_json(result, self)

async def set_my_default_administrator_rights(
self,
Expand Down Expand Up @@ -7982,7 +7982,7 @@ async def copy_message(
pool_timeout=pool_timeout,
api_kwargs=api_kwargs,
)
return MessageId.de_json(result, self) # type: ignore[return-value]
return MessageId.de_json(result, self)

async def copy_messages(
self,
Expand Down Expand Up @@ -8133,7 +8133,7 @@ async def get_chat_menu_button(
pool_timeout=pool_timeout,
api_kwargs=api_kwargs,
)
return MenuButton.de_json(result, bot=self) # type: ignore[return-value]
return MenuButton.de_json(result, bot=self)

async def create_invoice_link(
self,
Expand Down Expand Up @@ -8384,7 +8384,7 @@ async def create_forum_topic(
pool_timeout=pool_timeout,
api_kwargs=api_kwargs,
)
return ForumTopic.de_json(result, self) # type: ignore[return-value]
return ForumTopic.de_json(result, self)

async def edit_forum_topic(
self,
Expand Down Expand Up @@ -8972,7 +8972,7 @@ async def get_my_description(

"""
data = {"language_code": language_code}
return BotDescription.de_json( # type: ignore[return-value]
return BotDescription.de_json(
await self._post(
"getMyDescription",
data,
Expand Down Expand Up @@ -9011,7 +9011,7 @@ async def get_my_short_description(

"""
data = {"language_code": language_code}
return BotShortDescription.de_json( # type: ignore[return-value]
return BotShortDescription.de_json(
await self._post(
"getMyShortDescription",
data,
Expand Down Expand Up @@ -9097,7 +9097,7 @@ async def get_my_name(

"""
data = {"language_code": language_code}
return BotName.de_json( # type: ignore[return-value]
return BotName.de_json(
await self._post(
"getMyName",
data,
Expand Down Expand Up @@ -9139,7 +9139,7 @@ async def get_user_chat_boosts(
:class:`telegram.error.TelegramError`
"""
data: JSONDict = {"chat_id": chat_id, "user_id": user_id}
return UserChatBoosts.de_json( # type: ignore[return-value]
return UserChatBoosts.de_json(
await self._post(
"getUserChatBoosts",
data,
Expand Down Expand Up @@ -9263,7 +9263,7 @@ async def get_business_connection(
:class:`telegram.error.TelegramError`
"""
data: JSONDict = {"business_connection_id": business_connection_id}
return BusinessConnection.de_json( # type: ignore[return-value]
return BusinessConnection.de_json(
await self._post(
"getBusinessConnection",
data,
Expand Down Expand Up @@ -9402,7 +9402,7 @@ async def get_star_transactions(

data: JSONDict = {"offset": offset, "limit": limit}

return StarTransactions.de_json( # type: ignore[return-value]
return StarTransactions.de_json(
await self._post(
"getStarTransactions",
data,
Expand Down Expand Up @@ -9628,7 +9628,7 @@ async def create_chat_subscription_invite_link(
api_kwargs=api_kwargs,
)

return ChatInviteLink.de_json(result, self) # type: ignore[return-value]
return ChatInviteLink.de_json(result, self)

async def edit_chat_subscription_invite_link(
self,
Expand Down Expand Up @@ -9681,7 +9681,7 @@ async def edit_chat_subscription_invite_link(
api_kwargs=api_kwargs,
)

return ChatInviteLink.de_json(result, self) # type: ignore[return-value]
return ChatInviteLink.de_json(result, self)

async def get_available_gifts(
self,
Expand All @@ -9703,7 +9703,7 @@ async def get_available_gifts(
Raises:
:class:`telegram.error.TelegramError`
"""
return Gifts.de_json( # type: ignore[return-value]
return Gifts.de_json(
await self._post(
"getAvailableGifts",
read_timeout=read_timeout,
Expand Down
7 changes: 1 addition & 6 deletions telegram/_botcommandscope.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@ def __init__(self, type: str, *, api_kwargs: Optional[JSONDict] = None):
self._freeze()

@classmethod
def de_json(
cls, data: Optional[JSONDict], bot: Optional["Bot"] = None
) -> Optional["BotCommandScope"]:
def de_json(cls, data: JSONDict, bot: Optional["Bot"] = None) -> "BotCommandScope":
"""Converts JSON data to the appropriate :class:`BotCommandScope` object, i.e. takes
care of selecting the correct subclass.

Expand All @@ -104,9 +102,6 @@ def de_json(
"""
data = cls._parse_data(data)

if not data:
return None

_class_mapping: dict[str, type[BotCommandScope]] = {
cls.DEFAULT: BotCommandScopeDefault,
cls.ALL_PRIVATE_CHATS: BotCommandScopeAllPrivateChats,
Expand Down
49 changes: 12 additions & 37 deletions telegram/_business.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from telegram._files.sticker import Sticker
from telegram._telegramobject import TelegramObject
from telegram._user import User
from telegram._utils.argumentparsing import parse_sequence_arg
from telegram._utils.argumentparsing import de_json_optional, de_list_optional, parse_sequence_arg
from telegram._utils.datetime import extract_tzinfo_from_defaults, from_timestamp
from telegram._utils.types import JSONDict

Expand Down Expand Up @@ -106,20 +106,15 @@ def __init__(
self._freeze()

@classmethod
def de_json(
cls, data: Optional[JSONDict], bot: Optional["Bot"] = None
) -> Optional["BusinessConnection"]:
def de_json(cls, data: JSONDict, bot: Optional["Bot"] = None) -> "BusinessConnection":
"""See :meth:`telegram.TelegramObject.de_json`."""
data = cls._parse_data(data)

if not data:
return None

# Get the local timezone from the bot if it has defaults
loc_tzinfo = extract_tzinfo_from_defaults(bot)

data["date"] = from_timestamp(data.get("date"), tzinfo=loc_tzinfo)
data["user"] = User.de_json(data.get("user"), bot)
data["user"] = de_json_optional(data.get("user"), User, bot)

return super().de_json(data=data, bot=bot)

Expand Down Expand Up @@ -177,16 +172,11 @@ def __init__(
self._freeze()

@classmethod
def de_json(
cls, data: Optional[JSONDict], bot: Optional["Bot"] = None
) -> Optional["BusinessMessagesDeleted"]:
def de_json(cls, data: JSONDict, bot: Optional["Bot"] = None) -> "BusinessMessagesDeleted":
"""See :meth:`telegram.TelegramObject.de_json`."""
data = cls._parse_data(data)

if not data:
return None

data["chat"] = Chat.de_json(data.get("chat"), bot)
data["chat"] = de_json_optional(data.get("chat"), Chat, bot)

return super().de_json(data=data, bot=bot)

Expand Down Expand Up @@ -236,16 +226,11 @@ def __init__(
self._freeze()

@classmethod
def de_json(
cls, data: Optional[JSONDict], bot: Optional["Bot"] = None
) -> Optional["BusinessIntro"]:
def de_json(cls, data: JSONDict, bot: Optional["Bot"] = None) -> "BusinessIntro":
"""See :meth:`telegram.TelegramObject.de_json`."""
data = cls._parse_data(data)

if not data:
return None

data["sticker"] = Sticker.de_json(data.get("sticker"), bot)
data["sticker"] = de_json_optional(data.get("sticker"), Sticker, bot)

return super().de_json(data=data, bot=bot)

Expand Down Expand Up @@ -290,16 +275,11 @@ def __init__(
self._freeze()

@classmethod
def de_json(
cls, data: Optional[JSONDict], bot: Optional["Bot"] = None
) -> Optional["BusinessLocation"]:
def de_json(cls, data: JSONDict, bot: Optional["Bot"] = None) -> "BusinessLocation":
"""See :meth:`telegram.TelegramObject.de_json`."""
data = cls._parse_data(data)

if not data:
return None

data["location"] = Location.de_json(data.get("location"), bot)
data["location"] = de_json_optional(data.get("location"), Location, bot)

return super().de_json(data=data, bot=bot)

Expand Down Expand Up @@ -439,17 +419,12 @@ def __init__(
self._freeze()

@classmethod
def de_json(
cls, data: Optional[JSONDict], bot: Optional["Bot"] = None
) -> Optional["BusinessOpeningHours"]:
def de_json(cls, data: JSONDict, bot: Optional["Bot"] = None) -> "BusinessOpeningHours":
"""See :meth:`telegram.TelegramObject.de_json`."""
data = cls._parse_data(data)

if not data:
return None

data["opening_hours"] = BusinessOpeningHoursInterval.de_list(
data.get("opening_hours"), bot
data["opening_hours"] = de_list_optional(
data.get("opening_hours"), BusinessOpeningHoursInterval, bot
)

return super().de_json(data=data, bot=bot)
Loading
Loading