raise attribute errors when someone tries to assign values to chat/us… (#1495)

* raise attribute errors when someone tries to assign values to chat/user_data (closes #1402)

* fix test

* something something not switching entirely and using a messy patch and no not anymore and argh
This commit is contained in:
Poolitzer 2019-09-05 13:48:28 -07:00 committed by Eldinnie
parent 280306d1e9
commit d5399de99b
2 changed files with 34 additions and 4 deletions

View file

@ -73,13 +73,31 @@ class CallbackContext(object):
raise ValueError('CallbackContext should not be used with a non context aware '
'dispatcher!')
self._dispatcher = dispatcher
self.chat_data = None
self.user_data = None
self._chat_data = None
self._user_data = None
self.args = None
self.matches = None
self.error = None
self.job = None
@property
def chat_data(self):
return self._chat_data
@chat_data.setter
def chat_data(self, value):
raise AttributeError("You can not assign a new value to chat_data, see "
"https://git.io/fjxKe")
@property
def user_data(self):
return self._user_data
@user_data.setter
def user_data(self, value):
raise AttributeError("You can not assign a new value to user_data, see "
"https://git.io/fjxKe")
@classmethod
def from_error(cls, update, error, dispatcher):
self = cls.from_update(update, dispatcher)
@ -94,9 +112,9 @@ class CallbackContext(object):
user = update.effective_user
if chat:
self.chat_data = dispatcher.chat_data[chat.id]
self._chat_data = dispatcher.chat_data[chat.id]
if user:
self.user_data = dispatcher.user_data[user.id]
self._user_data = dispatcher.user_data[user.id]
return self
@classmethod

View file

@ -105,3 +105,15 @@ class TestCallbackContext(object):
callback_context.matches = ['test', 'blah']
assert callback_context.match == 'test'
def test_data_assignment(self, cdp):
update = Update(0, message=Message(0, User(1, 'user', False), None, Chat(1, 'chat')))
callback_context = CallbackContext.from_update(update, cdp)
with pytest.raises(AttributeError):
callback_context.chat_data = {"test": 123}
with pytest.raises(AttributeError):
callback_context.user_data = {}
with pytest.raises(AttributeError):
callback_context.chat_data = "test"