diff --git a/telegram/ext/conversationhandler.py b/telegram/ext/conversationhandler.py index beb299597..ec91b11d4 100644 --- a/telegram/ext/conversationhandler.py +++ b/telegram/ext/conversationhandler.py @@ -592,6 +592,11 @@ class ConversationHandler(Handler[Update]): ) elif new_state is not None: + if new_state not in self.states: + warnings.warn( + f"Handler returned state {new_state} which is unknown to the " + f"ConversationHandler{' ' + self.name if self.name is not None else ''}." + ) with self._conversations_lock: self.conversations[key] = new_state if self.persistent and self.persistence and self.name: diff --git a/telegram/ext/dispatcher.py b/telegram/ext/dispatcher.py index 0baacbaed..7c77e88b5 100644 --- a/telegram/ext/dispatcher.py +++ b/telegram/ext/dispatcher.py @@ -162,6 +162,11 @@ class Dispatcher: stacklevel=3, ) + if self.workers < 1: + warnings.warn( + 'Asynchronous callbacks can not be processed without at least one worker thread.' + ) + self.user_data: DefaultDict[int, Dict[object, object]] = defaultdict(dict) self.chat_data: DefaultDict[int, Dict[object, object]] = defaultdict(dict) self.bot_data = {} diff --git a/tests/test_conversationhandler.py b/tests/test_conversationhandler.py index 28a6dd130..f5950e6fa 100644 --- a/tests/test_conversationhandler.py +++ b/tests/test_conversationhandler.py @@ -433,6 +433,37 @@ class TestConversationHandler: dp.process_update(Update(update_id=0, message=message)) assert self.current_state[user1.id] == self.THIRSTY + def test_unknown_state_warning(self, dp, bot, user1, recwarn): + handler = ConversationHandler( + entry_points=[CommandHandler("start", lambda u, c: 1)], + states={ + 1: [TypeHandler(Update, lambda u, c: 69)], + 2: [TypeHandler(Update, lambda u, c: -1)], + }, + fallbacks=self.fallbacks, + name="xyz", + ) + dp.add_handler(handler) + message = Message( + 0, + None, + self.group, + from_user=user1, + text='/start', + entities=[ + MessageEntity(type=MessageEntity.BOT_COMMAND, offset=0, length=len('/start')) + ], + bot=bot, + ) + dp.process_update(Update(update_id=0, message=message)) + sleep(0.5) + dp.process_update(Update(update_id=1, message=message)) + sleep(0.5) + assert len(recwarn) == 1 + assert str(recwarn[0].message) == ( + "Handler returned state 69 which is unknown to the ConversationHandler xyz." + ) + def test_conversation_handler_per_chat(self, dp, bot, user1, user2): handler = ConversationHandler( entry_points=self.entry_points, diff --git a/tests/test_dispatcher.py b/tests/test_dispatcher.py index 83550e689..375174b12 100644 --- a/tests/test_dispatcher.py +++ b/tests/test_dispatcher.py @@ -97,6 +97,14 @@ class TestDispatcher: ): self.received = context.error.message + def test_less_than_one_worker_warning(self, dp, recwarn): + Dispatcher(dp.bot, dp.update_queue, job_queue=dp.job_queue, workers=0, use_context=True) + assert len(recwarn) == 1 + assert ( + str(recwarn[0].message) + == 'Asynchronous callbacks can not be processed without at least one worker thread.' + ) + def test_one_context_per_update(self, cdp): def one(update, context): if update.message.text == 'test':