Skip to content

Commit 378784f

Browse files
authored
Fix persistence with non telegram.Update updates (python-telegram-bot#1271)
* Allow persistence with no telegram.Update updates For use with TypeHandler * Add test
1 parent ea5b301 commit 378784f

3 files changed

Lines changed: 19 additions & 3 deletions

File tree

telegram/ext/dispatcher.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
from future.builtins import range
3232

33-
from telegram import TelegramError
33+
from telegram import TelegramError, Update
3434
from telegram.ext.handler import Handler
3535
from telegram.utils.promise import Promise
3636
from telegram.ext import BasePersistence
@@ -299,7 +299,7 @@ def process_update(self, update):
299299
try:
300300
for handler in (x for x in self.handlers[group] if x.check_update(update)):
301301
handler.handle_update(update, self)
302-
if self.persistence:
302+
if self.persistence and isinstance(update, Update):
303303
if self.persistence.store_chat_data and update.effective_chat.id:
304304
chat_id = update.effective_chat.id
305305
try:

tests/conftest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ def dp(_dp):
9696
_dp.update_queue.get(False)
9797
_dp.chat_data = defaultdict(dict)
9898
_dp.user_data = defaultdict(dict)
99+
_dp.persistence = None
99100
_dp.handlers = {}
100101
_dp.groups = []
101102
_dp.error_handlers = []

tests/test_persistence.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
from telegram import Update, Message, User, Chat
3333
from telegram.ext import BasePersistence, Updater, ConversationHandler, MessageHandler, Filters, \
34-
PicklePersistence, CommandHandler, DictPersistence
34+
PicklePersistence, CommandHandler, DictPersistence, TypeHandler
3535

3636

3737
@pytest.fixture(scope="function")
@@ -201,6 +201,21 @@ def save_user_data(data):
201201
assert dp.user_data[54321][1] == 'test7'
202202
assert dp.chat_data[-987654][2] == 'test8'
203203

204+
def test_persistence_dispatcher_arbitrary_update_types(self, dp, base_persistence, caplog):
205+
# Updates used with TypeHandler doesn't necessarily have the proper attributes for
206+
# persistence, makes sure it works anyways
207+
208+
dp.persistence = base_persistence
209+
210+
class MyUpdate(object):
211+
pass
212+
213+
dp.add_handler(TypeHandler(MyUpdate, lambda *_: None))
214+
215+
with caplog.at_level(logging.ERROR):
216+
dp.process_update(MyUpdate())
217+
assert 'An uncaught error was raised while processing the update' not in caplog.text
218+
204219

205220
@pytest.fixture(scope='function')
206221
def pickle_persistence():

0 commit comments

Comments
 (0)