diff --git a/telegram/_bot.py b/telegram/_bot.py index a6b46600269..5f060a81987 100644 --- a/telegram/_bot.py +++ b/telegram/_bot.py @@ -96,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 @@ -6602,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[int] = 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 bce269ba6cf..26f0ecf5838 100644 --- a/telegram/_chat.py +++ b/telegram/_chat.py @@ -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[int] = 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 e854142eb4c..5492b6c2e36 100644 --- a/telegram/_message.py +++ b/telegram/_message.py @@ -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, + MarkdownVersion, + 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[int] = 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,10 +3450,10 @@ def _parse_markdown( message_text: Optional[str], entities: Dict[MessageEntity, str], urled: bool = False, - version: int = 1, + version: MarkdownVersion = 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/_user.py b/telegram/_user.py index abafce0c380..7c5be15d4a9 100644 --- a/telegram/_user.py +++ b/telegram/_user.py @@ -25,7 +25,14 @@ 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[int] = 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..d619103dc42 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] + +MarkdownVersion = Literal[1, 2] 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/ext/_extbot.py b/telegram/ext/_extbot.py index c0f37efd9a5..aa222c3d69f 100644 --- a/telegram/ext/_extbot.py +++ b/telegram/ext/_extbot.py @@ -88,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 @@ -2705,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[int] = 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 7ce0a76c07b..91d7937ac74 100644 --- a/telegram/helpers.py +++ b/telegram/helpers.py @@ -35,13 +35,16 @@ from html import escape from typing import TYPE_CHECKING, Optional, Union +from telegram._utils.types import MarkdownVersion from telegram.constants import MessageType if TYPE_CHECKING: 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: MarkdownVersion = 1, entity_type: Optional[str] = None +) -> str: """Helper function to escape telegram markup symbols. .. versionchanged:: 20.3 @@ -88,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: int = 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. 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