Use warnings.warn for conversationhandler warnings. (#1343)

* FIXED: ConversationHandler errors were logged to root logger

* Use warnings.warn instead of self.logger.warning.
This commit is contained in:
Jasmin Bom 2019-02-13 23:28:23 +01:00 committed by GitHub
parent d0936f76ad
commit b64698e4b6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 18 deletions

View file

@ -19,6 +19,7 @@
"""This module contains the ConversationHandler."""
import logging
import warnings
from telegram import Update
from telegram.ext import (Handler, CallbackQueryHandler, InlineQueryHandler,
@ -173,8 +174,8 @@ class ConversationHandler(Handler):
raise ValueError("'per_user', 'per_chat' and 'per_message' can't all be 'False'")
if self.per_message and not self.per_chat:
logging.warning("If 'per_message=True' is used, 'per_chat=True' should also be used, "
"since message IDs are not globally unique.")
warnings.warn("If 'per_message=True' is used, 'per_chat=True' should also be used, "
"since message IDs are not globally unique.")
all_handlers = list()
all_handlers.extend(entry_points)
@ -186,22 +187,22 @@ class ConversationHandler(Handler):
if self.per_message:
for handler in all_handlers:
if not isinstance(handler, CallbackQueryHandler):
logging.warning("If 'per_message=True', all entry points and state handlers"
" must be 'CallbackQueryHandler', since no other handlers "
"have a message context.")
warnings.warn("If 'per_message=True', all entry points and state handlers"
" must be 'CallbackQueryHandler', since no other handlers "
"have a message context.")
break
else:
for handler in all_handlers:
if isinstance(handler, CallbackQueryHandler):
logging.warning("If 'per_message=False', 'CallbackQueryHandler' will not be "
"tracked for every message.")
warnings.warn("If 'per_message=False', 'CallbackQueryHandler' will not be "
"tracked for every message.")
break
if self.per_chat:
for handler in all_handlers:
if isinstance(handler, (InlineQueryHandler, ChosenInlineResultHandler)):
logging.warning("If 'per_chat=True', 'InlineQueryHandler' can not be used, "
"since inline queries have no chat context.")
warnings.warn("If 'per_chat=True', 'InlineQueryHandler' can not be used, "
"since inline queries have no chat context.")
break
def _get_key(self, update):

View file

@ -407,7 +407,7 @@ class TestConversationHandler(object):
assert handler.conversations.get((self.group.id, user1.id)) is None
assert handler.conversations.get((self.group.id, user2.id)) is None
def test_per_message_warning_is_only_shown_once(self, caplog):
def test_per_message_warning_is_only_shown_once(self, recwarn):
ConversationHandler(
entry_points=self.entry_points,
states={
@ -417,14 +417,14 @@ class TestConversationHandler(object):
fallbacks=self.fallbacks,
per_message=True
)
assert len(caplog.messages) == 1
assert caplog.messages[0] == (
assert len(recwarn) == 1
assert str(recwarn[0].message) == (
"If 'per_message=True', all entry points and state handlers"
" must be 'CallbackQueryHandler', since no other handlers"
" have a message context."
)
def test_per_message_false_warning_is_only_shown_once(self, caplog):
def test_per_message_false_warning_is_only_shown_once(self, recwarn):
ConversationHandler(
entry_points=self.entry_points,
states={
@ -434,13 +434,13 @@ class TestConversationHandler(object):
fallbacks=self.fallbacks,
per_message=False
)
assert len(caplog.messages) == 1
assert caplog.messages[0] == (
assert len(recwarn) == 1
assert str(recwarn[0].message) == (
"If 'per_message=False', 'CallbackQueryHandler' will not be "
"tracked for every message."
)
def test_warnings_per_chat_is_only_shown_once(self, caplog):
def test_warnings_per_chat_is_only_shown_once(self, recwarn):
def hello(bot, update):
return self.BREWING
@ -457,8 +457,8 @@ class TestConversationHandler(object):
per_chat=True
)
assert len(caplog.messages) == 1
assert caplog.messages[0] == (
assert len(recwarn) == 1
assert str(recwarn[0].message) == (
"If 'per_chat=True', 'InlineQueryHandler' can not be used,"
" since inline queries have no chat context."
)