Repository: python-telegram-bot/python-telegram-bot

Issue:
rename Prefixhandlers internally constructed list to `_commands`
Note to self: do this


Discussion:

Resolving patch:
diff --git a/telegram/ext/commandhandler.py b/telegram/ext/commandhandler.py
--- a/telegram/ext/commandhandler.py
+++ b/telegram/ext/commandhandler.py
@@ -302,6 +302,10 @@ def __init__(self,
                  pass_user_data=False,
                  pass_chat_data=False):
 
+        self._prefix = list()
+        self._command = list()
+        self._commands = list()
+
         super(PrefixHandler, self).__init__(
             'nocommand', callback, filters=filters, allow_edited=None, pass_args=pass_args,
             pass_update_queue=pass_update_queue,
@@ -309,15 +313,36 @@ def __init__(self,
             pass_user_data=pass_user_data,
             pass_chat_data=pass_chat_data)
 
+        self.prefix = prefix
+        self.command = command
+        self._build_commands()
+
+    @property
+    def prefix(self):
+        return self._prefix
+
+    @prefix.setter
+    def prefix(self, prefix):
         if isinstance(prefix, string_types):
-            self.prefix = [prefix.lower()]
+            self._prefix = [prefix.lower()]
         else:
-            self.prefix = prefix
+            self._prefix = prefix
+        self._build_commands()
+
+    @property
+    def command(self):
+        return self._command
+
+    @command.setter
+    def command(self, command):
         if isinstance(command, string_types):
-            self.command = [command.lower()]
+            self._command = [command.lower()]
         else:
-            self.command = command
-        self.command = [x.lower() + y.lower() for x in self.prefix for y in self.command]
+            self._command = command
+        self._build_commands()
+
+    def _build_commands(self):
+        self._commands = [x.lower() + y.lower() for x in self.prefix for y in self.command]
 
     def check_update(self, update):
         """Determines whether an update should be passed to this handlers :attr:`callback`.
@@ -334,7 +359,7 @@ def check_update(self, update):
 
             if message.text:
                 text_list = message.text.split()
-                if text_list[0].lower() not in self.command:
+                if text_list[0].lower() not in self._commands:
                     return None
                 filter_result = self.filters(update)
                 if filter_result:


Test patch:
diff --git a/tests/test_commandhandler.py b/tests/test_commandhandler.py
--- a/tests/test_commandhandler.py
+++ b/tests/test_commandhandler.py
@@ -378,6 +378,29 @@ def test_filters_for_wrong_command(self, mock_filter):
         assert not is_match(handler, make_message_update('/test'))
         assert not mock_filter.tested
 
+    def test_edit_prefix(self):
+        handler = self.make_default_handler()
+        handler.prefix = ['?', '§']
+        assert handler._commands == list(combinations(['?', '§'], self.COMMANDS))
+        handler.prefix = '+'
+        assert handler._commands == list(combinations(['+'], self.COMMANDS))
+
+    def test_edit_command(self):
+        handler = self.make_default_handler()
+        handler.command = 'foo'
+        assert handler._commands == list(combinations(self.PREFIXES, ['foo']))
+
+    def test_basic_after_editing(self, dp, prefix, command):
+        """Test the basic expected response from a prefix handler"""
+        handler = self.make_default_handler()
+        dp.add_handler(handler)
+        text = prefix + command
+
+        assert self.response(dp, make_message_update(text))
+        handler.command = 'foo'
+        text = prefix + 'foo'
+        assert self.response(dp, make_message_update(text))
+
     def test_context(self, cdp, prefix_message_update):
         handler = self.make_default_handler(self.callback_context)
         cdp.add_handler(handler)