Add Two UserWarnings (#2464)

* Warn if number of workers < one

Signed-off-by: starry69 <starry369126@outlook.com>

* improve warning message

Signed-off-by: starry69 <starry369126@outlook.com>

* warn if converstation handler return unknown state

Signed-off-by: starry69 <starry369126@outlook.com>

* Show handler name and state aswell, in warning

Signed-off-by: starry69 <starry369126@outlook.com>

* minor nitpick

Signed-off-by: starry69 <starry369126@outlook.com>
This commit is contained in:
Stɑrry Shivɑm 2021-04-16 22:32:43 +05:30 committed by GitHub
parent 9d93417d9a
commit b63877b1f2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 49 additions and 0 deletions

View file

@ -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:

View file

@ -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 = {}

View file

@ -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,

View file

@ -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':