From e524a74b8452f22ebbd6ba83d35084b40d2de494 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Sat, 18 Sep 2021 15:41:04 +0200 Subject: [PATCH] Remove client.disconnected property --- readthedocs/misc/v2-migration-guide.rst | 6 ++++ telethon/_client/telegrambaseclient.py | 3 -- telethon/_client/telegramclient.py | 38 ++++++------------------- telethon/_client/updates.py | 14 ++------- 4 files changed, 18 insertions(+), 43 deletions(-) diff --git a/readthedocs/misc/v2-migration-guide.rst b/readthedocs/misc/v2-migration-guide.rst index 62a8bc30..1ca03003 100644 --- a/readthedocs/misc/v2-migration-guide.rst +++ b/readthedocs/misc/v2-migration-guide.rst @@ -394,6 +394,12 @@ However, most likely, you were already doing the right thing (or else you would' "why is this not being edited", which you would most likely consider a bug rather than a feature). +The client.disconnected property has been removed +------------------------------------------------- + +``client.run_until_disconnected()`` should be used instead. + + The TelegramClient is no longer made out of mixins -------------------------------------------------- diff --git a/telethon/_client/telegrambaseclient.py b/telethon/_client/telegrambaseclient.py index 85c07057..c8023429 100644 --- a/telethon/_client/telegrambaseclient.py +++ b/telethon/_client/telegrambaseclient.py @@ -301,9 +301,6 @@ def init( def get_loop(self: 'TelegramClient') -> asyncio.AbstractEventLoop: return asyncio.get_event_loop() -def get_disconnected(self: 'TelegramClient') -> asyncio.Future: - return self._sender.disconnected - def get_flood_sleep_threshold(self): return self._flood_sleep_threshold diff --git a/telethon/_client/telegramclient.py b/telethon/_client/telegramclient.py index 839e1ea3..e2f1de76 100644 --- a/telethon/_client/telegramclient.py +++ b/telethon/_client/telegramclient.py @@ -2823,22 +2823,6 @@ class TelegramClient: """ return telegrambaseclient.get_loop(**locals()) - @property - def disconnected(self: 'TelegramClient') -> asyncio.Future: - """ - Property with a ``Future`` that resolves upon disconnection. - - Example - .. code-block:: python - - # Wait for a disconnection to occur - try: - await client.disconnected - except OSError: - print('Error on disconnect') - """ - return telegrambaseclient.get_disconnected(**locals()) - @property def flood_sleep_threshold(self): return telegrambaseclient.get_flood_sleep_threshold(**locals()) @@ -2928,30 +2912,26 @@ class TelegramClient: def run_until_disconnected(self: 'TelegramClient'): """ - Runs the event loop until the library is disconnected. + Wait until the library is disconnected. It also notifies Telegram that we want to receive updates as described in https://core.telegram.org/api/updates. + Event handlers will continue to run while the method awaits for a + disconnection to occur. Essentially, this method "blocks" until a + disconnection occurs, and keeps your code running if you have nothing + else to do. + Manual disconnections can be made by calling `disconnect() ` - or sending a ``KeyboardInterrupt`` (e.g. by pressing ``Ctrl+C`` on - the console window running the script). + or exiting the context-manager using the client (for example, a + ``KeyboardInterrupt`` by pressing ``Ctrl+C`` on the console window + would propagate the error, exit the ``with`` block and disconnect). If a disconnection error occurs (i.e. the library fails to reconnect automatically), said error will be raised through here, so you have a chance to ``except`` it on your own code. - If the loop is already running, this method returns a coroutine - that you should await on your own code. - - .. note:: - - If you want to handle ``KeyboardInterrupt`` in your code, - simply run the event loop in your code too in any way, such as - ``loop.run_forever()`` or ``await client.disconnected`` (e.g. - ``loop.run_until_complete(client.disconnected)``). - Example .. code-block:: python diff --git a/telethon/_client/updates.py b/telethon/_client/updates.py index 93e8a9bc..b319f8e2 100644 --- a/telethon/_client/updates.py +++ b/telethon/_client/updates.py @@ -18,23 +18,15 @@ if typing.TYPE_CHECKING: Callback = typing.Callable[[typing.Any], typing.Any] -async def _run_until_disconnected(self: 'TelegramClient'): - try: - # Make a high-level request to notify that we want updates - await self(_tl.fn.updates.GetState()) - return await self.disconnected - except KeyboardInterrupt: - pass - finally: - await self.disconnect() - async def set_receive_updates(self: 'TelegramClient', receive_updates): self._no_updates = not receive_updates if receive_updates: await self(_tl.fn.updates.GetState()) async def run_until_disconnected(self: 'TelegramClient'): - return await _run_until_disconnected(self) + # Make a high-level request to notify that we want updates + await self(_tl.fn.updates.GetState()) + return await self._sender.disconnected def on(self: 'TelegramClient', event: EventBuilder): def decorator(f):