diff --git a/telegram/ext/callbackcontext.py b/telegram/ext/callbackcontext.py index 752bf14be..aeb3eb964 100644 --- a/telegram/ext/callbackcontext.py +++ b/telegram/ext/callbackcontext.py @@ -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 diff --git a/tests/test_callbackcontext.py b/tests/test_callbackcontext.py index ce9b8b5df..2eefddaf8 100644 --- a/tests/test_callbackcontext.py +++ b/tests/test_callbackcontext.py @@ -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"