Repository: alexander-akhmetov/python-telegram

Issue:
[Feature Request] Function to remove handler
Shouldn't be that complicated but would be nice to have.

Discussion:

Patch:
diff --git a/telegram/client.py b/telegram/client.py
index 75b538d..a32387e 100644
--- a/telegram/client.py
+++ b/telegram/client.py
@@ -345,6 +345,16 @@ class Telegram:
         for handler in self._update_handlers[update_type]:
             self._workers_queue.put((handler, update), timeout=self._queue_put_timeout)
 
+    def remove_update_handler(self, handler_type: str, func: Callable) -> None:
+        """
+        Remove a handler with the specified type
+        """
+        try:
+            self._update_handlers[handler_type].remove(func)
+        except (ValueError, KeyError):
+            # not in the list
+            pass
+
     def add_message_handler(self, func: Callable) -> None:
         self.add_update_handler(MESSAGE_HANDLER_TYPE, func)
 


Test patch:
diff --git a/tests/requirements.txt b/tests/requirements.txt
index 6cd6df8..023005f 100644
--- a/tests/requirements.txt
+++ b/tests/requirements.txt
@@ -1,5 +1,5 @@
-pytest==5.3.5
+pytest==5.3.2
 flake8==3.7.9
 pylint==2.4.4
-ipdb==0.12.3
+ipdb==0.12.2
 mypy==0.761
diff --git a/tests/test_telegram_methods.py b/tests/test_telegram_methods.py
index add7068..b019003 100644
--- a/tests/test_telegram_methods.py
+++ b/tests/test_telegram_methods.py
@@ -99,6 +99,29 @@ class TestTelegram:
 
         assert telegram._update_handlers[MESSAGE_HANDLER_TYPE] == [my_handler]
 
+    def test_remove_update_handler(self, telegram):
+        # check remove_update_handler
+        assert telegram._update_handlers[MESSAGE_HANDLER_TYPE] == []
+
+        def my_handler():
+            pass
+
+        telegram.add_message_handler(my_handler)
+
+        telegram.remove_update_handler(MESSAGE_HANDLER_TYPE, my_handler)
+
+        assert telegram._update_handlers[MESSAGE_HANDLER_TYPE] == []
+
+    def test_remove_update_handler_empty_handlers_list(self, telegram):
+        telegram._update_handlers = {}
+
+        def my_handler():
+            pass
+
+        telegram.remove_update_handler(MESSAGE_HANDLER_TYPE, my_handler)
+
+        assert telegram._update_handlers == {}
+
     def test_add_update_handler(self, telegram):
         # check that add_update_handler function
         # appends passsed func to _update_handlers[type] list