From 0167aecdfe4b08c3b6f1808840e4bedd3b34a818 Mon Sep 17 00:00:00 2001 From: Harshil <37377066+harshil21@users.noreply.github.com> Date: Sun, 18 Jun 2023 05:38:04 +0530 Subject: [PATCH 1/4] Use @final and Literal in more places --- telegram/_bot.py | 3 ++- telegram/_message.py | 6 +++--- telegram/ext/_defaults.py | 3 ++- telegram/helpers.py | 8 +++++--- telegram/request/_requestdata.py | 3 ++- telegram/request/_requestparameter.py | 3 ++- 6 files changed, 16 insertions(+), 10 deletions(-) diff --git a/telegram/_bot.py b/telegram/_bot.py index a6b46600269..e87ef9ec8f5 100644 --- a/telegram/_bot.py +++ b/telegram/_bot.py @@ -32,6 +32,7 @@ Callable, Dict, List, + Literal, NoReturn, Optional, Sequence, @@ -6602,7 +6603,7 @@ async def send_poll( is_anonymous: Optional[bool] = None, type: Optional[str] = None, # pylint: disable=redefined-builtin allows_multiple_answers: Optional[bool] = None, - correct_option_id: Optional[int] = None, + correct_option_id: Optional[Literal[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]] = None, is_closed: Optional[bool] = None, disable_notification: ODVInput[bool] = DEFAULT_NONE, reply_to_message_id: Optional[int] = None, diff --git a/telegram/_message.py b/telegram/_message.py index e854142eb4c..538fd031831 100644 --- a/telegram/_message.py +++ b/telegram/_message.py @@ -20,7 +20,7 @@ """This module contains an object that represents a Telegram Message.""" import datetime from html import escape -from typing import TYPE_CHECKING, Dict, List, Optional, Sequence, Tuple, Union +from typing import TYPE_CHECKING, Dict, List, Literal, Optional, Sequence, Tuple, Union from telegram._chat import Chat from telegram._dice import Dice @@ -3442,10 +3442,10 @@ def _parse_markdown( message_text: Optional[str], entities: Dict[MessageEntity, str], urled: bool = False, - version: int = 1, + version: Literal[1, 2] = 1, offset: int = 0, ) -> Optional[str]: - version = int(version) + version = int(version) # type: ignore if message_text is None: return None diff --git a/telegram/ext/_defaults.py b/telegram/ext/_defaults.py index 7117fd1851b..9d2ed9feddd 100644 --- a/telegram/ext/_defaults.py +++ b/telegram/ext/_defaults.py @@ -18,11 +18,12 @@ # along with this program. If not, see [http://www.gnu.org/licenses/]. """This module contains the class Defaults, which allows passing default values to Application.""" import datetime -from typing import Any, Dict, NoReturn, Optional +from typing import Any, Dict, NoReturn, Optional, final from telegram._utils.datetime import UTC +@final class Defaults: """Convenience Class to gather all parameters with a (user defined) default value diff --git a/telegram/helpers.py b/telegram/helpers.py index 7ce0a76c07b..99de9197b5b 100644 --- a/telegram/helpers.py +++ b/telegram/helpers.py @@ -33,7 +33,7 @@ import re from html import escape -from typing import TYPE_CHECKING, Optional, Union +from typing import TYPE_CHECKING, Literal, Optional, Union from telegram.constants import MessageType @@ -41,7 +41,9 @@ from telegram import Message, Update -def escape_markdown(text: str, version: int = 1, entity_type: Optional[str] = None) -> str: +def escape_markdown( + text: str, version: Literal[1, 2] = 1, entity_type: Optional[str] = None +) -> str: """Helper function to escape telegram markup symbols. .. versionchanged:: 20.3 @@ -88,7 +90,7 @@ def mention_html(user_id: Union[int, str], name: str) -> str: return f'{escape(name)}' -def mention_markdown(user_id: Union[int, str], name: str, version: int = 1) -> str: +def mention_markdown(user_id: Union[int, str], name: str, version: Literal[1, 2] = 1) -> str: """ Helper function to create a user mention in Markdown syntax. diff --git a/telegram/request/_requestdata.py b/telegram/request/_requestdata.py index 550bcd7983a..48372f9ee5a 100644 --- a/telegram/request/_requestdata.py +++ b/telegram/request/_requestdata.py @@ -18,13 +18,14 @@ # along with this program. If not, see [http://www.gnu.org/licenses/]. """This module contains a class that holds the parameters of a request to the Bot API.""" import json -from typing import Any, Dict, List, Optional, Union +from typing import Any, Dict, List, Optional, Union, final from urllib.parse import urlencode from telegram._utils.types import UploadFileDict from telegram.request._requestparameter import RequestParameter +@final class RequestData: """Instances of this class collect the data needed for one request to the Bot API, including all parameters and files to be sent along with the request. diff --git a/telegram/request/_requestparameter.py b/telegram/request/_requestparameter.py index 40fdcd2c713..13d47e75341 100644 --- a/telegram/request/_requestparameter.py +++ b/telegram/request/_requestparameter.py @@ -20,7 +20,7 @@ import json from dataclasses import dataclass from datetime import datetime -from typing import List, Optional, Sequence, Tuple +from typing import List, Optional, Sequence, Tuple, final from telegram._files.inputfile import InputFile from telegram._files.inputmedia import InputMedia @@ -31,6 +31,7 @@ from telegram._utils.types import UploadFileDict +@final @dataclass(repr=True, eq=False, order=False, frozen=True) class RequestParameter: """Instances of this class represent a single parameter to be sent along with a request to From fe8e76a15b8821dacb897bd4f1fde3ac100f1343 Mon Sep 17 00:00:00 2001 From: Harshil <37377066+harshil21@users.noreply.github.com> Date: Sun, 18 Jun 2023 14:07:53 +0530 Subject: [PATCH 2/4] fix tests --- telegram/_chat.py | 4 ++-- telegram/_message.py | 2 +- telegram/_user.py | 4 ++-- telegram/ext/_extbot.py | 3 ++- 4 files changed, 7 insertions(+), 6 deletions(-) diff --git a/telegram/_chat.py b/telegram/_chat.py index bce269ba6cf..a83f7e57311 100644 --- a/telegram/_chat.py +++ b/telegram/_chat.py @@ -20,7 +20,7 @@ """This module contains an object that represents a Telegram Chat.""" from datetime import datetime from html import escape -from typing import TYPE_CHECKING, Final, Optional, Sequence, Tuple, Union +from typing import TYPE_CHECKING, Final, Literal, Optional, Sequence, Tuple, Union from telegram import constants from telegram._chatlocation import ChatLocation @@ -2201,7 +2201,7 @@ async def send_poll( is_anonymous: Optional[bool] = None, type: Optional[str] = None, allows_multiple_answers: Optional[bool] = None, - correct_option_id: Optional[int] = None, + correct_option_id: Optional[Literal[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]] = None, is_closed: Optional[bool] = None, disable_notification: ODVInput[bool] = DEFAULT_NONE, reply_to_message_id: Optional[int] = None, diff --git a/telegram/_message.py b/telegram/_message.py index 538fd031831..19280d6169e 100644 --- a/telegram/_message.py +++ b/telegram/_message.py @@ -2023,7 +2023,7 @@ async def reply_poll( is_anonymous: Optional[bool] = None, type: Optional[str] = None, # pylint: disable=redefined-builtin allows_multiple_answers: Optional[bool] = None, - correct_option_id: Optional[int] = None, + correct_option_id: Optional[Literal[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]] = None, is_closed: Optional[bool] = None, disable_notification: ODVInput[bool] = DEFAULT_NONE, reply_to_message_id: Optional[int] = None, diff --git a/telegram/_user.py b/telegram/_user.py index abafce0c380..0eaf09cc29c 100644 --- a/telegram/_user.py +++ b/telegram/_user.py @@ -19,7 +19,7 @@ # along with this program. If not, see [http://www.gnu.org/licenses/]. """This module contains an object that represents a Telegram User.""" from datetime import datetime -from typing import TYPE_CHECKING, Optional, Sequence, Tuple, Union +from typing import TYPE_CHECKING, Literal, Optional, Sequence, Tuple, Union from telegram._inline.inlinekeyboardbutton import InlineKeyboardButton from telegram._menubutton import MenuButton @@ -1349,7 +1349,7 @@ async def send_poll( is_anonymous: Optional[bool] = None, type: Optional[str] = None, allows_multiple_answers: Optional[bool] = None, - correct_option_id: Optional[int] = None, + correct_option_id: Optional[Literal[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]] = None, is_closed: Optional[bool] = None, disable_notification: ODVInput[bool] = DEFAULT_NONE, reply_to_message_id: Optional[int] = None, diff --git a/telegram/ext/_extbot.py b/telegram/ext/_extbot.py index c0f37efd9a5..0549bc2d309 100644 --- a/telegram/ext/_extbot.py +++ b/telegram/ext/_extbot.py @@ -27,6 +27,7 @@ Dict, Generic, List, + Literal, Optional, Sequence, Tuple, @@ -2705,7 +2706,7 @@ async def send_poll( is_anonymous: Optional[bool] = None, type: Optional[str] = None, # pylint: disable=redefined-builtin allows_multiple_answers: Optional[bool] = None, - correct_option_id: Optional[int] = None, + correct_option_id: Optional[Literal[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]] = None, is_closed: Optional[bool] = None, disable_notification: ODVInput[bool] = DEFAULT_NONE, reply_to_message_id: Optional[int] = None, From 2946f14dcee70f74732f712ad6f2c530de78aa33 Mon Sep 17 00:00:00 2001 From: Harshil <37377066+harshil21@users.noreply.github.com> Date: Sun, 18 Jun 2023 15:27:54 +0530 Subject: [PATCH 3/4] review: DRY --- telegram/_bot.py | 12 +++++++++--- telegram/_chat.py | 13 ++++++++++--- telegram/_message.py | 16 ++++++++++++---- telegram/_user.py | 13 ++++++++++--- telegram/_utils/types.py | 4 ++++ telegram/ext/_extbot.py | 12 +++++++++--- telegram/helpers.py | 7 ++++--- 7 files changed, 58 insertions(+), 19 deletions(-) diff --git a/telegram/_bot.py b/telegram/_bot.py index e87ef9ec8f5..5f060a81987 100644 --- a/telegram/_bot.py +++ b/telegram/_bot.py @@ -32,7 +32,6 @@ Callable, Dict, List, - Literal, NoReturn, Optional, Sequence, @@ -97,7 +96,14 @@ from telegram._utils.defaultvalue import DEFAULT_NONE, DefaultValue from telegram._utils.files import is_local_file, parse_file_input from telegram._utils.logging import get_logger -from telegram._utils.types import DVInput, FileInput, JSONDict, ODVInput, ReplyMarkup +from telegram._utils.types import ( + CorrectOptionID, + DVInput, + FileInput, + JSONDict, + ODVInput, + ReplyMarkup, +) from telegram._utils.warnings import warn from telegram._utils.warnings_transition import warn_about_thumb_return_thumbnail from telegram._webhookinfo import WebhookInfo @@ -6603,7 +6609,7 @@ async def send_poll( is_anonymous: Optional[bool] = None, type: Optional[str] = None, # pylint: disable=redefined-builtin allows_multiple_answers: Optional[bool] = None, - correct_option_id: Optional[Literal[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]] = None, + correct_option_id: Optional[CorrectOptionID] = None, is_closed: Optional[bool] = None, disable_notification: ODVInput[bool] = DEFAULT_NONE, reply_to_message_id: Optional[int] = None, diff --git a/telegram/_chat.py b/telegram/_chat.py index a83f7e57311..26f0ecf5838 100644 --- a/telegram/_chat.py +++ b/telegram/_chat.py @@ -20,7 +20,7 @@ """This module contains an object that represents a Telegram Chat.""" from datetime import datetime from html import escape -from typing import TYPE_CHECKING, Final, Literal, Optional, Sequence, Tuple, Union +from typing import TYPE_CHECKING, Final, Optional, Sequence, Tuple, Union from telegram import constants from telegram._chatlocation import ChatLocation @@ -32,7 +32,14 @@ from telegram._utils import enum from telegram._utils.argumentparsing import parse_sequence_arg from telegram._utils.defaultvalue import DEFAULT_NONE -from telegram._utils.types import DVInput, FileInput, JSONDict, ODVInput, ReplyMarkup +from telegram._utils.types import ( + CorrectOptionID, + DVInput, + FileInput, + JSONDict, + ODVInput, + ReplyMarkup, +) from telegram.helpers import escape_markdown from telegram.helpers import mention_html as helpers_mention_html from telegram.helpers import mention_markdown as helpers_mention_markdown @@ -2201,7 +2208,7 @@ async def send_poll( is_anonymous: Optional[bool] = None, type: Optional[str] = None, allows_multiple_answers: Optional[bool] = None, - correct_option_id: Optional[Literal[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]] = None, + correct_option_id: Optional[CorrectOptionID] = None, is_closed: Optional[bool] = None, disable_notification: ODVInput[bool] = DEFAULT_NONE, reply_to_message_id: Optional[int] = None, diff --git a/telegram/_message.py b/telegram/_message.py index 19280d6169e..3e032cb6dfe 100644 --- a/telegram/_message.py +++ b/telegram/_message.py @@ -20,7 +20,7 @@ """This module contains an object that represents a Telegram Message.""" import datetime from html import escape -from typing import TYPE_CHECKING, Dict, List, Literal, Optional, Sequence, Tuple, Union +from typing import TYPE_CHECKING, Dict, List, Optional, Sequence, Tuple, Union from telegram._chat import Chat from telegram._dice import Dice @@ -58,7 +58,15 @@ from telegram._utils.argumentparsing import parse_sequence_arg from telegram._utils.datetime import extract_tzinfo_from_defaults, from_timestamp from telegram._utils.defaultvalue import DEFAULT_NONE, DefaultValue -from telegram._utils.types import DVInput, FileInput, JSONDict, ODVInput, ReplyMarkup +from telegram._utils.types import ( + CorrectOptionID, + DVInput, + FileInput, + JSONDict, + MarkdownVersions, + ODVInput, + ReplyMarkup, +) from telegram._utils.warnings import warn from telegram._videochat import ( VideoChatEnded, @@ -2023,7 +2031,7 @@ async def reply_poll( is_anonymous: Optional[bool] = None, type: Optional[str] = None, # pylint: disable=redefined-builtin allows_multiple_answers: Optional[bool] = None, - correct_option_id: Optional[Literal[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]] = None, + correct_option_id: Optional[CorrectOptionID] = None, is_closed: Optional[bool] = None, disable_notification: ODVInput[bool] = DEFAULT_NONE, reply_to_message_id: Optional[int] = None, @@ -3442,7 +3450,7 @@ def _parse_markdown( message_text: Optional[str], entities: Dict[MessageEntity, str], urled: bool = False, - version: Literal[1, 2] = 1, + version: MarkdownVersions = 1, offset: int = 0, ) -> Optional[str]: version = int(version) # type: ignore diff --git a/telegram/_user.py b/telegram/_user.py index 0eaf09cc29c..7c5be15d4a9 100644 --- a/telegram/_user.py +++ b/telegram/_user.py @@ -19,13 +19,20 @@ # along with this program. If not, see [http://www.gnu.org/licenses/]. """This module contains an object that represents a Telegram User.""" from datetime import datetime -from typing import TYPE_CHECKING, Literal, Optional, Sequence, Tuple, Union +from typing import TYPE_CHECKING, Optional, Sequence, Tuple, Union from telegram._inline.inlinekeyboardbutton import InlineKeyboardButton from telegram._menubutton import MenuButton from telegram._telegramobject import TelegramObject from telegram._utils.defaultvalue import DEFAULT_NONE -from telegram._utils.types import DVInput, FileInput, JSONDict, ODVInput, ReplyMarkup +from telegram._utils.types import ( + CorrectOptionID, + DVInput, + FileInput, + JSONDict, + ODVInput, + ReplyMarkup, +) from telegram.helpers import mention_html as helpers_mention_html from telegram.helpers import mention_markdown as helpers_mention_markdown @@ -1349,7 +1356,7 @@ async def send_poll( is_anonymous: Optional[bool] = None, type: Optional[str] = None, allows_multiple_answers: Optional[bool] = None, - correct_option_id: Optional[Literal[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]] = None, + correct_option_id: Optional[CorrectOptionID] = None, is_closed: Optional[bool] = None, disable_notification: ODVInput[bool] = DEFAULT_NONE, reply_to_message_id: Optional[int] = None, diff --git a/telegram/_utils/types.py b/telegram/_utils/types.py index e815bf529a4..a3ebef9cb8b 100644 --- a/telegram/_utils/types.py +++ b/telegram/_utils/types.py @@ -91,3 +91,7 @@ """Allowed HTTP versions. .. versionadded:: NEXT.VERSION""" + +CorrectOptionID = Literal[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] + +MarkdownVersions = Literal[1, 2] diff --git a/telegram/ext/_extbot.py b/telegram/ext/_extbot.py index 0549bc2d309..aa222c3d69f 100644 --- a/telegram/ext/_extbot.py +++ b/telegram/ext/_extbot.py @@ -27,7 +27,6 @@ Dict, Generic, List, - Literal, Optional, Sequence, Tuple, @@ -89,7 +88,14 @@ from telegram._utils.datetime import to_timestamp 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.types import ( + CorrectOptionID, + DVInput, + FileInput, + JSONDict, + ODVInput, + ReplyMarkup, +) from telegram.ext._callbackdatacache import CallbackDataCache from telegram.ext._utils.types import RLARGS from telegram.request import BaseRequest @@ -2706,7 +2712,7 @@ async def send_poll( is_anonymous: Optional[bool] = None, type: Optional[str] = None, # pylint: disable=redefined-builtin allows_multiple_answers: Optional[bool] = None, - correct_option_id: Optional[Literal[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]] = None, + correct_option_id: Optional[CorrectOptionID] = None, is_closed: Optional[bool] = None, disable_notification: ODVInput[bool] = DEFAULT_NONE, reply_to_message_id: Optional[int] = None, diff --git a/telegram/helpers.py b/telegram/helpers.py index 99de9197b5b..423ff770344 100644 --- a/telegram/helpers.py +++ b/telegram/helpers.py @@ -33,8 +33,9 @@ import re from html import escape -from typing import TYPE_CHECKING, Literal, Optional, Union +from typing import TYPE_CHECKING, Optional, Union +from telegram._utils.types import MarkdownVersions from telegram.constants import MessageType if TYPE_CHECKING: @@ -42,7 +43,7 @@ def escape_markdown( - text: str, version: Literal[1, 2] = 1, entity_type: Optional[str] = None + text: str, version: MarkdownVersions = 1, entity_type: Optional[str] = None ) -> str: """Helper function to escape telegram markup symbols. @@ -90,7 +91,7 @@ def mention_html(user_id: Union[int, str], name: str) -> str: return f'{escape(name)}' -def mention_markdown(user_id: Union[int, str], name: str, version: Literal[1, 2] = 1) -> str: +def mention_markdown(user_id: Union[int, str], name: str, version: MarkdownVersions = 1) -> str: """ Helper function to create a user mention in Markdown syntax. From 1f53410a71e7e09e4e67d2c3093e8381b0af88b5 Mon Sep 17 00:00:00 2001 From: Harshil <37377066+harshil21@users.noreply.github.com> Date: Sun, 18 Jun 2023 16:07:28 +0530 Subject: [PATCH 4/4] MarkdownVersions -> MarkdownVersion --- telegram/_message.py | 4 ++-- telegram/_utils/types.py | 2 +- telegram/helpers.py | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/telegram/_message.py b/telegram/_message.py index 3e032cb6dfe..5492b6c2e36 100644 --- a/telegram/_message.py +++ b/telegram/_message.py @@ -63,7 +63,7 @@ DVInput, FileInput, JSONDict, - MarkdownVersions, + MarkdownVersion, ODVInput, ReplyMarkup, ) @@ -3450,7 +3450,7 @@ def _parse_markdown( message_text: Optional[str], entities: Dict[MessageEntity, str], urled: bool = False, - version: MarkdownVersions = 1, + version: MarkdownVersion = 1, offset: int = 0, ) -> Optional[str]: version = int(version) # type: ignore diff --git a/telegram/_utils/types.py b/telegram/_utils/types.py index a3ebef9cb8b..d619103dc42 100644 --- a/telegram/_utils/types.py +++ b/telegram/_utils/types.py @@ -94,4 +94,4 @@ CorrectOptionID = Literal[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] -MarkdownVersions = Literal[1, 2] +MarkdownVersion = Literal[1, 2] diff --git a/telegram/helpers.py b/telegram/helpers.py index 423ff770344..91d7937ac74 100644 --- a/telegram/helpers.py +++ b/telegram/helpers.py @@ -35,7 +35,7 @@ from html import escape from typing import TYPE_CHECKING, Optional, Union -from telegram._utils.types import MarkdownVersions +from telegram._utils.types import MarkdownVersion from telegram.constants import MessageType if TYPE_CHECKING: @@ -43,7 +43,7 @@ def escape_markdown( - text: str, version: MarkdownVersions = 1, entity_type: Optional[str] = None + text: str, version: MarkdownVersion = 1, entity_type: Optional[str] = None ) -> str: """Helper function to escape telegram markup symbols. @@ -91,7 +91,7 @@ def mention_html(user_id: Union[int, str], name: str) -> str: return f'{escape(name)}' -def mention_markdown(user_id: Union[int, str], name: str, version: MarkdownVersions = 1) -> str: +def mention_markdown(user_id: Union[int, str], name: str, version: MarkdownVersion = 1) -> str: """ Helper function to create a user mention in Markdown syntax.