From 419fe6dca3b5000e45d3c6d98bb50b3062d7fcd7 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Wed, 17 Oct 2018 11:30:07 +0200 Subject: [PATCH] Raise RuntimeError on with blocks that need async --- telethon/client/auth.py | 6 ++++++ telethon/tl/custom/conversation.py | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/telethon/client/auth.py b/telethon/client/auth.py index b4a1c74a..9fcb1bce 100644 --- a/telethon/client/auth.py +++ b/telethon/client/auth.py @@ -483,6 +483,12 @@ class AuthMethods(MessageParseMethods, UserMethods): # region with blocks def __enter__(self): + if self._loop.is_running(): + raise RuntimeError( + 'You must use "async with" if the event loop ' + 'is running (i.e. you are inside an "async def")' + ) + return self.start() async def __aenter__(self): diff --git a/telethon/tl/custom/conversation.py b/telethon/tl/custom/conversation.py index 97b6816a..04f3ad5e 100644 --- a/telethon/tl/custom/conversation.py +++ b/telethon/tl/custom/conversation.py @@ -379,6 +379,12 @@ class Conversation(ChatGetter): fut.cancel() def __enter__(self): + if self._client.loop.is_running(): + raise RuntimeError( + 'You must use "async with" if the event loop ' + 'is running (i.e. you are inside an "async def")' + ) + return self._client.loop.run_until_complete(self.__aenter__()) async def __aenter__(self):