Skip to content

Commit

Permalink
Use @Final and Literal in more places (#3762)
Browse files Browse the repository at this point in the history
  • Loading branch information
harshil21 committed Jun 25, 2023
1 parent 27a8683 commit 97965b6
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 17 deletions.
11 changes: 9 additions & 2 deletions telegram/_bot.py
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
11 changes: 9 additions & 2 deletions telegram/_chat.py
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
16 changes: 12 additions & 4 deletions telegram/_message.py
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
11 changes: 9 additions & 2 deletions telegram/_user.py
Expand Up @@ -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

Expand Down Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions telegram/_utils/types.py
Expand Up @@ -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]
3 changes: 2 additions & 1 deletion telegram/ext/_defaults.py
Expand Up @@ -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
Expand Down
11 changes: 9 additions & 2 deletions telegram/ext/_extbot.py
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
7 changes: 5 additions & 2 deletions telegram/helpers.py
Expand Up @@ -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
Expand Down Expand Up @@ -88,7 +91,7 @@ def mention_html(user_id: Union[int, str], name: str) -> str:
return f'<a href="tg://user?id={user_id}">{escape(name)}</a>'


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.
Expand Down
3 changes: 2 additions & 1 deletion telegram/request/_requestdata.py
Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion telegram/request/_requestparameter.py
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 97965b6

Please sign in to comment.