Skip to content
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

Use Sequence instead of Union[List[…], Tuple[…]] #3412

Merged
merged 23 commits into from Jan 1, 2023
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
e5e582c
try to solve merge conflict 1st commit
clot27 Dec 8, 2022
2ad8bdf
try to solve merge conflict 2nd commit
clot27 Dec 11, 2022
8de5aa8
try to solve merge conflict 3rd commit
clot27 Dec 12, 2022
56c9d6f
try to solve merge conflict last commit
clot27 Dec 12, 2022
b744fa9
add sequence to more places and fix tests
clot27 Dec 24, 2022
96b73dd
add type checker for requestparameter
clot27 Dec 27, 2022
4d9ef3a
Merge branch 'master' into use-sequence
clot27 Dec 28, 2022
06d9d82
add versionchanged directives
clot27 Dec 28, 2022
34a920d
Merge remote-tracking branch 'refs/remotes/origin/use-sequence' into …
clot27 Dec 28, 2022
5c62f33
fix indent and slashes in `Bot.send_media_group` docstr for Sphinx
lemontree210 Dec 28, 2022
a6f8c99
add limits for `media` param of `Bot.send_media_group` to constants.py
lemontree210 Dec 28, 2022
c4c3160
add limit for `suggested_tip_amounts`, fix docstr of `InvoiceLimit`
lemontree210 Dec 28, 2022
2113c00
Revert "add limit for `suggested_tip_amounts`, fix docstr of `Invoice…
lemontree210 Dec 28, 2022
04f4d29
Revert "add limits for `media` param of `Bot.send_media_group` to con…
lemontree210 Dec 28, 2022
63316e0
sequence in send_media_group and respective shortcuts
clot27 Dec 28, 2022
77ebbf8
Fix docstrings and tests
clot27 Dec 29, 2022
5da9581
add suggested changes
clot27 Dec 29, 2022
a8fdaf0
add tests for the change
clot27 Jan 1, 2023
78d63a0
Merge branch 'master' into use-sequence
clot27 Jan 1, 2023
271b20a
add correct substitutions
clot27 Jan 1, 2023
173618a
Merge branch 'master' into use-sequence
clot27 Jan 1, 2023
49cae74
Add a small fix for media groups
Bibo-Joshi Jan 1, 2023
a534c89
Merge remote-tracking branch 'origin/use-sequence' into use-sequence
Bibo-Joshi Jan 1, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion docs/substitutions/global.rst
Expand Up @@ -42,8 +42,10 @@

.. |reply_to_msg_id| replace:: If the message is a reply, ID of the original message.

.. |sequenceclassargs| replace:: Accepts any :class:`collections.abc.Sequence` as input instead of just a list. The input is converted to a tuple.
.. |sequenceclassargs| replace:: |sequenceargs| The input is converted to a tuple.
harshil21 marked this conversation as resolved.
Show resolved Hide resolved

.. |tupleclassattrs| replace:: This attribute is now an immutable tuple.

.. |alwaystuple| replace:: This attribute is now always a tuple, that may be empty.

.. |sequenceargs| replace:: Accepts any :class:`collections.abc.Sequence` as input instead of just a list.
192 changes: 135 additions & 57 deletions telegram/_bot.py

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions telegram/_callbackquery.py
Expand Up @@ -18,7 +18,7 @@
# along with this program. If not, see [http://www.gnu.org/licenses/].
# pylint: disable=redefined-builtin
"""This module contains an object that represents a Telegram CallbackQuery"""
from typing import TYPE_CHECKING, ClassVar, List, Optional, Tuple, Union
from typing import TYPE_CHECKING, ClassVar, Optional, Sequence, Tuple, Union

from telegram import constants
from telegram._files.location import Location
Expand Down Expand Up @@ -192,7 +192,7 @@ async def edit_message_text(
parse_mode: ODVInput[str] = DEFAULT_NONE,
disable_web_page_preview: ODVInput[bool] = DEFAULT_NONE,
reply_markup: "InlineKeyboardMarkup" = None,
entities: Union[List["MessageEntity"], Tuple["MessageEntity", ...]] = None,
entities: Sequence["MessageEntity"] = None,
*,
read_timeout: ODVInput[float] = DEFAULT_NONE,
write_timeout: ODVInput[float] = DEFAULT_NONE,
Expand Down Expand Up @@ -252,7 +252,7 @@ async def edit_message_caption(
caption: str = None,
reply_markup: "InlineKeyboardMarkup" = None,
parse_mode: ODVInput[str] = DEFAULT_NONE,
caption_entities: Union[List["MessageEntity"], Tuple["MessageEntity", ...]] = None,
caption_entities: Sequence["MessageEntity"] = None,
*,
read_timeout: ODVInput[float] = DEFAULT_NONE,
write_timeout: ODVInput[float] = DEFAULT_NONE,
Expand Down Expand Up @@ -720,7 +720,7 @@ async def copy_message(
chat_id: Union[int, str],
caption: str = None,
parse_mode: ODVInput[str] = DEFAULT_NONE,
caption_entities: Union[Tuple["MessageEntity", ...], List["MessageEntity"]] = None,
caption_entities: Sequence["MessageEntity"] = None,
disable_notification: DVInput[bool] = DEFAULT_NONE,
reply_to_message_id: int = None,
allow_sending_without_reply: DVInput[bool] = DEFAULT_NONE,
Expand Down
32 changes: 16 additions & 16 deletions telegram/_chat.py
Expand Up @@ -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, ClassVar, List, Optional, Sequence, Tuple, Union
from typing import TYPE_CHECKING, ClassVar, Optional, Sequence, Tuple, Union

from telegram import constants
from telegram._chatlocation import ChatLocation
Expand Down Expand Up @@ -1239,7 +1239,7 @@ async def send_message(
reply_to_message_id: int = None,
reply_markup: ReplyMarkup = None,
allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE,
entities: Union[List["MessageEntity"], Tuple["MessageEntity", ...]] = None,
entities: Sequence["MessageEntity"] = None,
protect_content: ODVInput[bool] = DEFAULT_NONE,
message_thread_id: int = None,
*,
Expand Down Expand Up @@ -1280,7 +1280,7 @@ async def send_message(

async def send_media_group(
self,
media: List[
media: Sequence[
Union["InputMediaAudio", "InputMediaDocument", "InputMediaPhoto", "InputMediaVideo"]
],
disable_notification: ODVInput[bool] = DEFAULT_NONE,
Expand All @@ -1296,7 +1296,7 @@ async def send_media_group(
api_kwargs: JSONDict = None,
caption: Optional[str] = None,
parse_mode: ODVInput[str] = DEFAULT_NONE,
caption_entities: Union[List["MessageEntity"], Tuple["MessageEntity", ...]] = None,
caption_entities: Sequence["MessageEntity"] = None,
) -> Tuple["Message", ...]:
"""Shortcut for::

Expand Down Expand Up @@ -1369,7 +1369,7 @@ async def send_photo(
reply_markup: ReplyMarkup = None,
parse_mode: ODVInput[str] = DEFAULT_NONE,
allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE,
caption_entities: Union[List["MessageEntity"], Tuple["MessageEntity", ...]] = None,
caption_entities: Sequence["MessageEntity"] = None,
protect_content: ODVInput[bool] = DEFAULT_NONE,
message_thread_id: int = None,
*,
Expand Down Expand Up @@ -1473,7 +1473,7 @@ async def send_audio(
parse_mode: ODVInput[str] = DEFAULT_NONE,
thumb: FileInput = None,
allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE,
caption_entities: Union[List["MessageEntity"], Tuple["MessageEntity", ...]] = None,
caption_entities: Sequence["MessageEntity"] = None,
protect_content: ODVInput[bool] = DEFAULT_NONE,
message_thread_id: int = None,
*,
Expand Down Expand Up @@ -1529,7 +1529,7 @@ async def send_document(
thumb: FileInput = None,
disable_content_type_detection: bool = None,
allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE,
caption_entities: Union[List["MessageEntity"], Tuple["MessageEntity", ...]] = None,
caption_entities: Sequence["MessageEntity"] = None,
protect_content: ODVInput[bool] = DEFAULT_NONE,
message_thread_id: int = None,
*,
Expand Down Expand Up @@ -1663,7 +1663,7 @@ async def send_invoice(
payload: str,
provider_token: str,
currency: str,
prices: List["LabeledPrice"],
prices: Sequence["LabeledPrice"],
start_parameter: str = None,
photo_url: str = None,
photo_size: int = None,
Expand All @@ -1682,7 +1682,7 @@ async def send_invoice(
send_email_to_provider: bool = None,
allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE,
max_tip_amount: int = None,
suggested_tip_amounts: List[int] = None,
suggested_tip_amounts: Sequence[int] = None,
protect_content: ODVInput[bool] = DEFAULT_NONE,
message_thread_id: int = None,
*,
Expand Down Expand Up @@ -1815,7 +1815,7 @@ async def send_animation(
reply_to_message_id: int = None,
reply_markup: ReplyMarkup = None,
allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE,
caption_entities: Union[List["MessageEntity"], Tuple["MessageEntity", ...]] = None,
caption_entities: Sequence["MessageEntity"] = None,
protect_content: ODVInput[bool] = DEFAULT_NONE,
message_thread_id: int = None,
*,
Expand Down Expand Up @@ -1974,7 +1974,7 @@ async def send_video(
supports_streaming: bool = None,
thumb: FileInput = None,
allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE,
caption_entities: Union[List["MessageEntity"], Tuple["MessageEntity", ...]] = None,
caption_entities: Sequence["MessageEntity"] = None,
protect_content: ODVInput[bool] = DEFAULT_NONE,
message_thread_id: int = None,
*,
Expand Down Expand Up @@ -2080,7 +2080,7 @@ async def send_voice(
reply_markup: ReplyMarkup = None,
parse_mode: ODVInput[str] = DEFAULT_NONE,
allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE,
caption_entities: Union[List["MessageEntity"], Tuple["MessageEntity", ...]] = None,
caption_entities: Sequence["MessageEntity"] = None,
protect_content: ODVInput[bool] = DEFAULT_NONE,
message_thread_id: int = None,
*,
Expand Down Expand Up @@ -2125,7 +2125,7 @@ async def send_voice(
async def send_poll(
self,
question: str,
options: List[str],
options: Sequence[str],
is_anonymous: bool = None,
type: str = None,
allows_multiple_answers: bool = None,
Expand All @@ -2139,7 +2139,7 @@ async def send_poll(
open_period: int = None,
close_date: Union[int, datetime] = None,
allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE,
explanation_entities: Union[List["MessageEntity"], Tuple["MessageEntity", ...]] = None,
explanation_entities: Sequence["MessageEntity"] = None,
protect_content: ODVInput[bool] = DEFAULT_NONE,
message_thread_id: int = None,
*,
Expand Down Expand Up @@ -2192,7 +2192,7 @@ async def send_copy(
message_id: int,
caption: str = None,
parse_mode: ODVInput[str] = DEFAULT_NONE,
caption_entities: Union[Tuple["MessageEntity", ...], List["MessageEntity"]] = None,
caption_entities: Sequence["MessageEntity"] = None,
disable_notification: DVInput[bool] = DEFAULT_NONE,
reply_to_message_id: int = None,
allow_sending_without_reply: DVInput[bool] = DEFAULT_NONE,
Expand Down Expand Up @@ -2242,7 +2242,7 @@ async def copy_message(
message_id: int,
caption: str = None,
parse_mode: ODVInput[str] = DEFAULT_NONE,
caption_entities: Union[Tuple["MessageEntity", ...], List["MessageEntity"]] = None,
caption_entities: Sequence["MessageEntity"] = None,
disable_notification: DVInput[bool] = DEFAULT_NONE,
reply_to_message_id: int = None,
allow_sending_without_reply: DVInput[bool] = DEFAULT_NONE,
Expand Down
2 changes: 1 addition & 1 deletion telegram/_games/game.py
Expand Up @@ -77,7 +77,7 @@ class Game(TelegramObject):
using :meth:`telegram.Bot.edit_message_text`.
text_entities (Tuple[:class:`telegram.MessageEntity`]): Optional. Special entities that
appear in text, such as usernames, URLs, bot commands, etc.
This list is empty if the message does not contain text entities.
This tuple is empty if the message does not contain text entities.

.. versionchanged:: 20.0
|tupleclassattrs|
Expand Down
20 changes: 13 additions & 7 deletions telegram/_inline/inlinekeyboardmarkup.py
Expand Up @@ -17,7 +17,7 @@
# You should have received a copy of the GNU Lesser Public License
# along with this program. If not, see [http://www.gnu.org/licenses/].
"""This module contains an object that represents a Telegram InlineKeyboardMarkup."""
from typing import TYPE_CHECKING, List, Optional, Sequence
from typing import TYPE_CHECKING, Optional, Sequence

from telegram._inline.inlinekeyboardbutton import InlineKeyboardButton
from telegram._telegramobject import TelegramObject
Expand Down Expand Up @@ -111,7 +111,7 @@ def from_button(cls, button: InlineKeyboardButton, **kwargs: object) -> "InlineK

@classmethod
def from_row(
cls, button_row: List[InlineKeyboardButton], **kwargs: object
cls, button_row: Sequence[InlineKeyboardButton], **kwargs: object
) -> "InlineKeyboardMarkup":
"""Shortcut for::

Expand All @@ -120,15 +120,18 @@ def from_row(
Return an InlineKeyboardMarkup from a single row of InlineKeyboardButtons

Args:
button_row (List[:class:`telegram.InlineKeyboardButton`]): The button to use in the
markup
button_row (Sequence[:class:`telegram.InlineKeyboardButton`]): The button to use
in the markup

.. versionchanged:: 20.0
|sequenceargs|

"""
return cls([button_row], **kwargs) # type: ignore[arg-type]

@classmethod
def from_column(
cls, button_column: List[InlineKeyboardButton], **kwargs: object
cls, button_column: Sequence[InlineKeyboardButton], **kwargs: object
) -> "InlineKeyboardMarkup":
"""Shortcut for::

Expand All @@ -137,8 +140,11 @@ def from_column(
Return an InlineKeyboardMarkup from a single column of InlineKeyboardButtons

Args:
button_column (List[:class:`telegram.InlineKeyboardButton`]): The button to use in the
markup
button_column (Sequence[:class:`telegram.InlineKeyboardButton`]): The button to use
in the markup

.. versionchanged:: 20.0
|sequenceargs|

"""
button_grid = [[button] for button in button_column]
Expand Down