Improve Warning About Unknown ConversationHandler States (#3242)

This commit is contained in:
Harshil 2022-09-17 01:43:11 +05:30 committed by GitHub
parent a80a053b0a
commit 3536bb247e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 5 deletions

View file

@ -855,13 +855,13 @@ class ConversationHandler(BaseHandler[Update, CCT]):
) )
if isinstance(self.map_to_parent, dict) and new_state in self.map_to_parent: if isinstance(self.map_to_parent, dict) and new_state in self.map_to_parent:
self._update_state(self.END, conversation_key) self._update_state(self.END, conversation_key, handler)
if raise_dp_handler_stop: if raise_dp_handler_stop:
raise ApplicationHandlerStop(self.map_to_parent.get(new_state)) raise ApplicationHandlerStop(self.map_to_parent.get(new_state))
return self.map_to_parent.get(new_state) return self.map_to_parent.get(new_state)
if current_state != self.WAITING: if current_state != self.WAITING:
self._update_state(new_state, conversation_key) self._update_state(new_state, conversation_key, handler)
if raise_dp_handler_stop: if raise_dp_handler_stop:
# Don't pass the new state here. If we're in a nested conversation, the parent is # Don't pass the new state here. If we're in a nested conversation, the parent is
@ -870,7 +870,9 @@ class ConversationHandler(BaseHandler[Update, CCT]):
# Signals a possible parent conversation to stay in the current state # Signals a possible parent conversation to stay in the current state
return None return None
def _update_state(self, new_state: object, key: ConversationKey) -> None: def _update_state(
self, new_state: object, key: ConversationKey, handler: BaseHandler = None
) -> None:
if new_state == self.END: if new_state == self.END:
if key in self._conversations: if key in self._conversations:
# If there is no key in conversations, nothing is done. # If there is no key in conversations, nothing is done.
@ -884,7 +886,8 @@ class ConversationHandler(BaseHandler[Update, CCT]):
elif new_state is not None: elif new_state is not None:
if new_state not in self.states: if new_state not in self.states:
warn( warn(
f"BaseHandler returned state {new_state} which is unknown to the " f"{repr(handler.callback.__name__) if handler is not None else 'BaseHandler'} "
f"returned state {new_state} which is unknown to the "
f"ConversationHandler{' ' + self.name if self.name is not None else ''}.", f"ConversationHandler{' ' + self.name if self.name is not None else ''}.",
stacklevel=2, stacklevel=2,
) )

View file

@ -670,7 +670,7 @@ class TestConversationHandler:
raise exc raise exc
assert len(recwarn) == 1 assert len(recwarn) == 1
assert str(recwarn[0].message) == ( assert str(recwarn[0].message) == (
"BaseHandler returned state 69 which is unknown to the ConversationHandler xyz." "'callback' returned state 69 which is unknown to the ConversationHandler xyz."
) )
async def test_conversation_handler_per_chat(self, app, bot, user1, user2): async def test_conversation_handler_per_chat(self, app, bot, user1, user2):