From ee1f6e2c9f421d0fddc1e88a5d12d9120d444456 Mon Sep 17 00:00:00 2001
From: Dan <14043624+delivrance@users.noreply.github.com>
Date: Thu, 4 Jul 2019 12:57:07 +0200
Subject: [PATCH] Fix errors and warnings when using Pyrogram async with Python
<3.5.3
---
pyrogram/client/client.py | 5 ++++-
pyrogram/client/methods/chats/archive_chats.py | 18 +++++++++++-------
.../client/methods/chats/iter_chat_members.py | 4 ++--
pyrogram/client/methods/chats/iter_dialogs.py | 4 ++--
.../client/methods/chats/unarchive_chats.py | 18 +++++++++++-------
.../client/methods/messages/iter_history.py | 4 ++--
.../methods/users/iter_profile_photos.py | 4 ++--
7 files changed, 34 insertions(+), 23 deletions(-)
diff --git a/pyrogram/client/client.py b/pyrogram/client/client.py
index e7da9117..faac9051 100644
--- a/pyrogram/client/client.py
+++ b/pyrogram/client/client.py
@@ -447,7 +447,8 @@ class Client(Methods, BaseClient):
Raises:
RPCError: In case of a Telegram RPC error.
"""
- run = asyncio.get_event_loop().run_until_complete
+ loop = asyncio.get_event_loop()
+ run = loop.run_until_complete
run(self.start())
@@ -460,6 +461,8 @@ class Client(Methods, BaseClient):
if self.is_started:
run(self.stop())
+ loop.close()
+
return coroutine
def add_handler(self, handler: Handler, group: int = 0):
diff --git a/pyrogram/client/methods/chats/archive_chats.py b/pyrogram/client/methods/chats/archive_chats.py
index 3f53b25e..379860d5 100644
--- a/pyrogram/client/methods/chats/archive_chats.py
+++ b/pyrogram/client/methods/chats/archive_chats.py
@@ -19,7 +19,6 @@
from typing import Union, List
from pyrogram.api import functions, types
-
from ...ext import BaseClient
@@ -45,14 +44,19 @@ class ArchiveChats(BaseClient):
if not isinstance(chat_ids, list):
chat_ids = [chat_ids]
+ folder_peers = []
+
+ for chat in chat_ids:
+ folder_peers.append(
+ types.InputFolderPeer(
+ peer=await self.resolve_peer(chat),
+ folder_id=1
+ )
+ )
+
await self.send(
functions.folders.EditPeerFolders(
- folder_peers=[
- types.InputFolderPeer(
- peer=await self.resolve_peer(chat),
- folder_id=1
- ) for chat in chat_ids
- ]
+ folder_peers=folder_peers
)
)
diff --git a/pyrogram/client/methods/chats/iter_chat_members.py b/pyrogram/client/methods/chats/iter_chat_members.py
index c4db1521..bdd613fd 100644
--- a/pyrogram/client/methods/chats/iter_chat_members.py
+++ b/pyrogram/client/methods/chats/iter_chat_members.py
@@ -17,7 +17,7 @@
# along with Pyrogram. If not, see .
from string import ascii_lowercase
-from typing import Union, AsyncGenerator, Optional
+from typing import Union, Generator, Optional
import pyrogram
from async_generator import async_generator, yield_
@@ -47,7 +47,7 @@ class IterChatMembers(BaseClient):
limit: int = 0,
query: str = "",
filter: str = Filters.ALL
- ) -> Optional[AsyncGenerator["pyrogram.ChatMember", None]]:
+ ) -> Optional[Generator["pyrogram.ChatMember", None, None]]:
"""Iterate through the members of a chat sequentially.
This convenience method does the same as repeatedly calling :meth:`~Client.get_chat_members` in a loop, thus saving you
diff --git a/pyrogram/client/methods/chats/iter_dialogs.py b/pyrogram/client/methods/chats/iter_dialogs.py
index bbad9b35..3162d31d 100644
--- a/pyrogram/client/methods/chats/iter_dialogs.py
+++ b/pyrogram/client/methods/chats/iter_dialogs.py
@@ -16,7 +16,7 @@
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see .
-from typing import AsyncGenerator, Optional
+from typing import Generator, Optional
import pyrogram
from async_generator import async_generator, yield_
@@ -30,7 +30,7 @@ class IterDialogs(BaseClient):
self,
limit: int = 0,
offset_date: int = None
- ) -> Optional[AsyncGenerator["pyrogram.Dialog", None]]:
+ ) -> Optional[Generator["pyrogram.Dialog", None, None]]:
"""Iterate through a user's dialogs sequentially.
This convenience method does the same as repeatedly calling :meth:`~Client.get_dialogs` in a loop, thus saving
diff --git a/pyrogram/client/methods/chats/unarchive_chats.py b/pyrogram/client/methods/chats/unarchive_chats.py
index 56768dba..8c47557e 100644
--- a/pyrogram/client/methods/chats/unarchive_chats.py
+++ b/pyrogram/client/methods/chats/unarchive_chats.py
@@ -19,7 +19,6 @@
from typing import Union, List
from pyrogram.api import functions, types
-
from ...ext import BaseClient
@@ -45,14 +44,19 @@ class UnarchiveChats(BaseClient):
if not isinstance(chat_ids, list):
chat_ids = [chat_ids]
+ folder_peers = []
+
+ for chat in chat_ids:
+ folder_peers.append(
+ types.InputFolderPeer(
+ peer=await self.resolve_peer(chat),
+ folder_id=0
+ )
+ )
+
await self.send(
functions.folders.EditPeerFolders(
- folder_peers=[
- types.InputFolderPeer(
- peer=await self.resolve_peer(chat),
- folder_id=0
- ) for chat in chat_ids
- ]
+ folder_peers=folder_peers
)
)
diff --git a/pyrogram/client/methods/messages/iter_history.py b/pyrogram/client/methods/messages/iter_history.py
index 2dca96fe..6d69273b 100644
--- a/pyrogram/client/methods/messages/iter_history.py
+++ b/pyrogram/client/methods/messages/iter_history.py
@@ -16,7 +16,7 @@
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see .
-from typing import Union, Optional, AsyncGenerator
+from typing import Union, Optional, Generator
import pyrogram
from async_generator import async_generator, yield_
@@ -34,7 +34,7 @@ class IterHistory(BaseClient):
offset_id: int = 0,
offset_date: int = 0,
reverse: bool = False
- ) -> Optional[AsyncGenerator["pyrogram.Message", None]]:
+ ) -> Optional[Generator["pyrogram.Message", None, None]]:
"""Iterate through a chat history sequentially.
This convenience method does the same as repeatedly calling :meth:`~Client.get_history` in a loop, thus saving
diff --git a/pyrogram/client/methods/users/iter_profile_photos.py b/pyrogram/client/methods/users/iter_profile_photos.py
index bfe3e7f0..4e703e07 100644
--- a/pyrogram/client/methods/users/iter_profile_photos.py
+++ b/pyrogram/client/methods/users/iter_profile_photos.py
@@ -16,7 +16,7 @@
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see .
-from typing import Union, AsyncGenerator, Optional
+from typing import Union, Generator, Optional
import pyrogram
from async_generator import async_generator, yield_
@@ -31,7 +31,7 @@ class IterProfilePhotos(BaseClient):
chat_id: Union[int, str],
offset: int = 0,
limit: int = 0,
- ) -> Optional[AsyncGenerator["pyrogram.Message", None]]:
+ ) -> Optional[Generator["pyrogram.Message", None, None]]:
"""Iterate through a chat or a user profile photos sequentially.
This convenience method does the same as repeatedly calling :meth:`~Client.get_profile_photos` in a loop, thus