Skip to content

Commit 5681cce

Browse files
committed
Add back the ability to pass iterators to some methods
1 parent 427738d commit 5681cce

5 files changed

Lines changed: 30 additions & 38 deletions

File tree

pyrogram/methods/messages/delete_messages.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
# You should have received a copy of the GNU Lesser General Public License
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

19-
from typing import Union, List
19+
from typing import Union, Iterable
2020

2121
import pyrogram
2222
from pyrogram import raw
@@ -26,7 +26,7 @@ class DeleteMessages:
2626
async def delete_messages(
2727
self: "pyrogram.Client",
2828
chat_id: Union[int, str],
29-
message_ids: Union[int, List[int]],
29+
message_ids: Union[int, Iterable[int]],
3030
revoke: bool = True
3131
) -> int:
3232
"""Delete messages, including service messages.
@@ -37,8 +37,8 @@ async def delete_messages(
3737
For your personal cloud (Saved Messages) you can simply use "me" or "self".
3838
For a contact that exists in your Telegram address book you can use his phone number (str).
3939
40-
message_ids (``int`` | List of ``int``):
41-
A list of Message identifiers to delete (integers) or a single message id.
40+
message_ids (``int`` | Iterable of ``int``):
41+
An iterable of message identifiers to delete (integers) or a single message id.
4242
4343
revoke (``bool``, *optional*):
4444
Deletes messages on both parts.
@@ -62,9 +62,7 @@ async def delete_messages(
6262
await app.delete_messages(chat_id, message_id, revoke=False)
6363
"""
6464
peer = await self.resolve_peer(chat_id)
65-
# Follow type annotation of the raw function "DeleteMessage".
66-
if not isinstance(message_ids, list):
67-
message_ids = [message_ids]
65+
message_ids = list(message_ids) if not isinstance(message_ids, int) else [message_ids]
6866

6967
if isinstance(peer, raw.types.InputPeerChannel):
7068
r = await self.invoke(
@@ -77,10 +75,8 @@ async def delete_messages(
7775
r = await self.invoke(
7876
raw.functions.messages.DeleteMessages(
7977
id=message_ids,
80-
revoke=revoke or None # Follow the type annotation.
78+
revoke=revoke
8179
)
8280
)
8381

84-
# Deleting messages you don't have right onto won't raise any error.
85-
# Check for pts_count, which is 0 in case deletes fail.
8682
return r.pts_count

pyrogram/methods/messages/forward_messages.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

1919
from datetime import datetime
20-
from typing import Union, List
20+
from typing import Union, List, Iterable
2121

2222
import pyrogram
2323
from pyrogram import raw, utils
@@ -29,7 +29,7 @@ async def forward_messages(
2929
self: "pyrogram.Client",
3030
chat_id: Union[int, str],
3131
from_chat_id: Union[int, str],
32-
message_ids: Union[int, List[int]],
32+
message_ids: Union[int, Iterable[int]],
3333
disable_notification: bool = None,
3434
schedule_date: datetime = None,
3535
protect_content: bool = None
@@ -47,8 +47,8 @@ async def forward_messages(
4747
For your personal cloud (Saved Messages) you can simply use "me" or "self".
4848
For a contact that exists in your Telegram address book you can use his phone number (str).
4949
50-
message_ids (``int`` | List of ``int``):
51-
A list of Message identifiers in the chat specified in *from_chat_id* or a single message id.
50+
message_ids (``int`` | Iterable of ``int``):
51+
An iterable of message identifiers in the chat specified in *from_chat_id* or a single message id.
5252
5353
disable_notification (``bool``, *optional*):
5454
Sends the message silently.
@@ -74,9 +74,8 @@ async def forward_messages(
7474
await app.forward_messages(to_chat, from_chat, [1, 2, 3])
7575
"""
7676

77-
is_list = isinstance(message_ids, list)
78-
if not is_list:
79-
message_ids = [message_ids]
77+
is_iterable = not isinstance(message_ids, int)
78+
message_ids = list(message_ids) if is_iterable else [message_ids]
8079

8180
r = await self.invoke(
8281
raw.functions.messages.ForwardMessages(
@@ -106,4 +105,4 @@ async def forward_messages(
106105
)
107106
)
108107

109-
return types.List(forwarded_messages) if is_list else forwarded_messages[0]
108+
return types.List(forwarded_messages) if is_iterable else forwarded_messages[0]

pyrogram/methods/messages/get_messages.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

1919
import logging
20-
from typing import Union, List
20+
from typing import Union, List, Iterable
2121

2222
import pyrogram
2323
from pyrogram import raw
@@ -34,8 +34,8 @@ class GetMessages:
3434
async def get_messages(
3535
self: "pyrogram.Client",
3636
chat_id: Union[int, str],
37-
message_ids: Union[int, List[int]] = None,
38-
reply_to_message_ids: Union[int, List[int]] = None,
37+
message_ids: Union[int, Iterable[int]] = None,
38+
reply_to_message_ids: Union[int, Iterable[int]] = None,
3939
replies: int = 1
4040
) -> Union["types.Message", List["types.Message"]]:
4141
"""Get one or more messages from a chat by using message identifiers.
@@ -48,12 +48,12 @@ async def get_messages(
4848
For your personal cloud (Saved Messages) you can simply use "me" or "self".
4949
For a contact that exists in your Telegram address book you can use his phone number (str).
5050
51-
message_ids (``int`` | List of ``int``, *optional*):
52-
Pass a single message identifier or a list of message ids (as integers) to get the content of the
51+
message_ids (``int`` | Iterable of ``int``, *optional*):
52+
Pass a single message identifier or an iterable of message ids (as integers) to get the content of the
5353
message themselves.
5454
55-
reply_to_message_ids (``int`` | List of ``int``, *optional*):
56-
Pass a single message identifier or a list of message ids (as integers) to get the content of
55+
reply_to_message_ids (``int`` | Iterable of ``int``, *optional*):
56+
Pass a single message identifier or an iterable of message ids (as integers) to get the content of
5757
the previous message you replied to using this message.
5858
If *message_ids* is set, this argument will be ignored.
5959
@@ -98,10 +98,8 @@ async def get_messages(
9898

9999
peer = await self.resolve_peer(chat_id)
100100

101-
is_list = isinstance(ids, list)
102-
if not is_list:
103-
ids = [ids]
104-
101+
is_iterable = not isinstance(ids, int)
102+
ids = list(ids) if is_iterable else [ids]
105103
ids = [ids_type(id=i) for i in ids]
106104

107105
if replies < 0:
@@ -116,4 +114,4 @@ async def get_messages(
116114

117115
messages = await utils.parse_messages(self, r, replies=replies)
118116

119-
return messages if is_list else messages[0] if messages else None
117+
return messages if is_iterable else messages[0] if messages else None

pyrogram/methods/users/get_users.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

1919
import asyncio
20-
from typing import Union, List
20+
from typing import Union, List, Iterable
2121

2222
import pyrogram
2323
from pyrogram import raw
@@ -27,13 +27,13 @@
2727
class GetUsers:
2828
async def get_users(
2929
self: "pyrogram.Client",
30-
user_ids: Union[int, str, List[Union[int, str]]]
30+
user_ids: Union[int, str, Iterable[Union[int, str]]]
3131
) -> Union["types.User", List["types.User"]]:
3232
"""Get information about a user.
3333
You can retrieve up to 200 users at once.
3434
3535
Parameters:
36-
user_ids (``int`` | ``str`` | List of ``int`` or ``str``):
36+
user_ids (``int`` | ``str`` | Iterable of ``int`` or ``str``):
3737
A list of User identifiers (id or username) or a single user id/username.
3838
For a contact that exists in your Telegram address book you can use his phone number (str).
3939
@@ -50,10 +50,9 @@ async def get_users(
5050
# Get information about multiple users at once
5151
await app.get_users([user_id1, user_id2, user_id3])
5252
"""
53-
is_list = isinstance(user_ids, list)
54-
if not is_list:
55-
user_ids = [user_ids]
5653

54+
is_iterable = not isinstance(user_ids, (int, str))
55+
user_ids = list(user_ids) if is_iterable else [user_ids]
5756
user_ids = await asyncio.gather(*[self.resolve_peer(i) for i in user_ids])
5857

5958
r = await self.invoke(
@@ -67,4 +66,4 @@ async def get_users(
6766
for i in r:
6867
users.append(types.User._parse(self, i))
6968

70-
return users if is_list else users[0]
69+
return users if is_iterable else users[0]

pyrogram/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ async def parse_messages(client, messages: "raw.types.messages.Messages", replie
113113

114114
reply_messages = await client.get_messages(
115115
chat_id,
116-
reply_to_message_ids=list(messages_with_replies.keys()),
116+
reply_to_message_ids=messages_with_replies.keys(),
117117
replies=replies - 1
118118
)
119119

0 commit comments

Comments
 (0)