Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Simplify Handling of Empty Data inTelegramObject.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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Bibo-Joshi merged 17 commits intomasterfromde-json-empty-input
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
Show all changes
17 commits
Select commitHold shift + click to select a range
e25e5f0
Get Started on Simplifying `de_json/list`
Bibo-JoshiDec 29, 2024
2c6913a
Merge branch 'master' into de-json-empty-input
Bibo-JoshiDec 29, 2024
04de3ef
Get a Number of Tests passing
Bibo-JoshiDec 29, 2024
ae639fb
Get defaults handling tests to run
Bibo-JoshiDec 30, 2024
3fe033d
Use `de_list` instead of `de_list_wo` for required args
Bibo-JoshiDec 30, 2024
c5f3bf7
Use `de_json` instead of `de_json_wo` for required args
Bibo-JoshiDec 30, 2024
0a1b976
Fix failing tests
Bibo-JoshiDec 30, 2024
56cbe65
More failures & one Improvement in TO
Bibo-JoshiDec 30, 2024
7c00b03
remove type checking protocol from runtime
Bibo-JoshiDec 30, 2024
9682a90
Merge branch 'master' into de-json-empty-input
Bibo-JoshiJan 3, 2025
cddf8c4
Revert "Use `de_json` instead of `de_json_wo` for required args"
Bibo-JoshiJan 3, 2025
4a5a93d
Revert "Use `de_list` instead of `de_list_wo` for required args"
Bibo-JoshiJan 3, 2025
4c9ea41
Use data.get in remaining places
Bibo-JoshiJan 3, 2025
e991b93
Rename functions according to review
Bibo-JoshiJan 3, 2025
54b9988
Remove debug code
Bibo-JoshiJan 3, 2025
d942921
Merge branch 'master' into de-json-empty-input
Bibo-JoshiJan 14, 2025
05f52fa
Review
Bibo-JoshiJan 14, 2025
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
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionstelegram/_bot.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 DownExpand 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 DownExpand 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 DownExpand 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 DownExpand 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 DownExpand 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 DownExpand 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 DownExpand 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 DownExpand 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 DownExpand 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 DownExpand 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 DownExpand 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 DownExpand 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 DownExpand 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 DownExpand 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 DownExpand 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 DownExpand 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 DownExpand 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 DownExpand 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 DownExpand 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 DownExpand 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 DownExpand 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 DownExpand 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 DownExpand 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 deletionstelegram/_botcommandscope.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionstelegram/_business.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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 importde_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 DownExpand 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 DownExpand 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 DownExpand 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 DownExpand 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 DownExpand 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

[8]ページ先頭

©2009-2025 Movatter.jp