X Tutup
Skip to content

Commit 592352c

Browse files
committed
dispatcher/*handler methods to snake_case + deprecation warnings
1 parent 9d367e9 commit 592352c

11 files changed

+115
-49
lines changed

telegram/ext/callbackqueryhandler.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
from .handler import Handler
2323
from telegram import Update
24+
from telegram.utils.deprecate import deprecate
2425

2526

2627
class CallbackQueryHandler(Handler):
@@ -29,7 +30,7 @@ class CallbackQueryHandler(Handler):
2930
3031
Args:
3132
callback (function): A function that takes ``bot, update`` as
32-
positional arguments. It will be called when the ``checkUpdate``
33+
positional arguments. It will be called when the ``check_update``
3334
has determined that an update should be processed by this handler.
3435
pass_update_queue (optional[bool]): If the handler should be passed the
3536
update queue as a keyword argument called ``update_queue``. It can
@@ -39,10 +40,15 @@ class CallbackQueryHandler(Handler):
3940
def __init__(self, callback, pass_update_queue=False):
4041
super(CallbackQueryHandler, self).__init__(callback, pass_update_queue)
4142

42-
def checkUpdate(self, update):
43+
def check_update(self, update):
4344
return isinstance(update, Update) and update.callback_query
4445

45-
def handleUpdate(self, update, dispatcher):
46-
optional_args = self.collectOptionalArgs(dispatcher)
46+
def handle_update(self, update, dispatcher):
47+
optional_args = self.collect_optional_args(dispatcher)
4748

4849
self.callback(dispatcher.bot, update, **optional_args)
50+
51+
# old non-PEP8 Handler methods
52+
m = "telegram.CallbackQueryHandler."
53+
checkUpdate = deprecate(check_update, m + "checkUpdate", m + "check_update")
54+
handleUpdate = deprecate(handle_update, m + "handleUpdate", m + "handle_update")

telegram/ext/choseninlineresulthandler.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
from .handler import Handler
2323
from telegram import Update
24+
from telegram.utils.deprecate import deprecate
2425

2526

2627
class ChosenInlineResultHandler(Handler):
@@ -30,7 +31,7 @@ class ChosenInlineResultHandler(Handler):
3031
3132
Args:
3233
callback (function): A function that takes ``bot, update`` as
33-
positional arguments. It will be called when the ``checkUpdate``
34+
positional arguments. It will be called when the ``check_update``
3435
has determined that an update should be processed by this handler.
3536
pass_update_queue (optional[bool]): If the handler should be passed the
3637
update queue as a keyword argument called ``update_queue``. It can
@@ -41,10 +42,15 @@ def __init__(self, callback, pass_update_queue=False):
4142
super(ChosenInlineResultHandler, self).__init__(callback,
4243
pass_update_queue)
4344

44-
def checkUpdate(self, update):
45+
def check_update(self, update):
4546
return isinstance(update, Update) and update.chosen_inline_result
4647

47-
def handleUpdate(self, update, dispatcher):
48-
optional_args = self.collectOptionalArgs(dispatcher)
48+
def handle_update(self, update, dispatcher):
49+
optional_args = self.collect_optional_args(dispatcher)
4950

5051
self.callback(dispatcher.bot, update, **optional_args)
52+
53+
# old non-PEP8 Handler methods
54+
m = "telegram.ChosenInlineResultHandler."
55+
checkUpdate = deprecate(check_update, m + "checkUpdate", m + "check_update")
56+
handleUpdate = deprecate(handle_update, m + "handleUpdate", m + "handle_update")

telegram/ext/commandhandler.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
from .handler import Handler
2323
from telegram import Update
24+
from telegram.utils.deprecate import deprecate
2425

2526

2627
class CommandHandler(Handler):
@@ -32,7 +33,7 @@ class CommandHandler(Handler):
3233
Args:
3334
command (str): The name of the command this handler should listen for.
3435
callback (function): A function that takes ``bot, update`` as
35-
positional arguments. It will be called when the ``checkUpdate``
36+
positional arguments. It will be called when the ``check_update``
3637
has determined that an update should be processed by this handler.
3738
pass_args (optional[bool]): If the handler should be passed the
3839
arguments passed to the command as a keyword argument called `
@@ -49,18 +50,23 @@ def __init__(self, command, callback, pass_args=False,
4950
self.command = command
5051
self.pass_args = pass_args
5152

52-
def checkUpdate(self, update):
53+
def check_update(self, update):
5354
return (isinstance(update, Update) and
5455
update.message and
5556
update.message.text and
5657
update.message.text.startswith('/') and
5758
update.message.text[1:].split(' ')[0].split('@')[0] ==
5859
self.command)
5960

60-
def handleUpdate(self, update, dispatcher):
61-
optional_args = self.collectOptionalArgs(dispatcher)
61+
def handle_update(self, update, dispatcher):
62+
optional_args = self.collect_optional_args(dispatcher)
6263

6364
if self.pass_args:
6465
optional_args['args'] = update.message.text.split(' ')[1:]
6566

6667
self.callback(dispatcher.bot, update, **optional_args)
68+
69+
# old non-PEP8 Handler methods
70+
m = "telegram.CommandHandler."
71+
checkUpdate = deprecate(check_update, m + "checkUpdate", m + "check_update")
72+
handleUpdate = deprecate(handle_update, m + "handleUpdate", m + "handle_update")

telegram/ext/dispatcher.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
from telegram import (TelegramError, NullHandler)
3030
from telegram.ext.handler import Handler
31+
from telegram.utils.deprecate import deprecate
3132

3233
logging.getLogger(__name__).addHandler(NullHandler())
3334

@@ -201,7 +202,7 @@ def processUpdate(self, update):
201202
'processing the update')
202203
break
203204

204-
def addHandler(self, handler, group=DEFAULT_GROUP):
205+
def add_handler(self, handler, group=DEFAULT_GROUP):
205206
"""
206207
Register a handler.
207208
@@ -239,7 +240,7 @@ def addHandler(self, handler, group=DEFAULT_GROUP):
239240

240241
self.handlers[group].append(handler)
241242

242-
def removeHandler(self, handler, group=DEFAULT_GROUP):
243+
def remove_handler(self, handler, group=DEFAULT_GROUP):
243244
"""
244245
Remove a handler from the specified group
245246
@@ -253,7 +254,7 @@ def removeHandler(self, handler, group=DEFAULT_GROUP):
253254
del self.handlers[group]
254255
self.groups.remove(group)
255256

256-
def addErrorHandler(self, callback):
257+
def add_error_handler(self, callback):
257258
"""
258259
Registers an error handler in the Dispatcher.
259260
@@ -264,7 +265,7 @@ def addErrorHandler(self, callback):
264265

265266
self.error_handlers.append(callback)
266267

267-
def removeErrorHandler(self, callback):
268+
def remove_error_handler(self, callback):
268269
"""
269270
De-registers an error handler.
270271
@@ -287,8 +288,10 @@ def dispatchError(self, update, error):
287288
for callback in self.error_handlers:
288289
callback(self.bot, update, error)
289290

290-
# snake_case (PEP8) aliases
291-
add_handler = addHandler
292-
remove_handler = removeHandler
293-
add_error_handler = addErrorHandler
294-
remove_error_handler = removeErrorHandler
291+
# old non-PEP8 Dispatcher methods
292+
m = "telegram.dispatcher."
293+
addHandler = deprecate(add_handler, m + "AddHandler", m + "add_handler")
294+
removeHandler = deprecate(remove_handler, m + "removeHandler", m + "remove_handler")
295+
addErrorHandler = deprecate(add_error_handler, m + "addErrorHandler", m + "add_error_handler")
296+
removeErrorHandler = deprecate(remove_error_handler,
297+
m + "removeErrorHandler", m + "remove_error_handler")

telegram/ext/handler.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
""" This module contains the base class for handlers as used by the
2121
Dispatcher """
2222

23+
from telegram.deprecate import deprecate
24+
2325

2426
class Handler(object):
2527
"""
@@ -28,7 +30,7 @@ class Handler(object):
2830
2931
Args:
3032
callback (function): A function that takes ``bot, update`` as
31-
positional arguments. It will be called when the ``checkUpdate``
33+
positional arguments. It will be called when the ``check_update``
3234
has determined that an update should be processed by this handler.
3335
pass_update_queue (optional[bool]): If the callback should be passed
3436
the update queue as a keyword argument called ``update_queue``. It
@@ -39,7 +41,7 @@ def __init__(self, callback, pass_update_queue=False):
3941
self.callback = callback
4042
self.pass_update_queue = pass_update_queue
4143

42-
def checkUpdate(self, update):
44+
def check_update(self, update):
4345
"""
4446
This method is called to determine if an update should be handled by
4547
this handler instance. It should always be overridden.
@@ -52,7 +54,7 @@ def checkUpdate(self, update):
5254
"""
5355
raise NotImplementedError
5456

55-
def handleUpdate(self, update, dispatcher):
57+
def handle_update(self, update, dispatcher):
5658
"""
5759
This method is called if it was determined that an update should indeed
5860
be handled by this instance. It should also be overridden, but in most
@@ -65,7 +67,7 @@ def handleUpdate(self, update, dispatcher):
6567
"""
6668
raise NotImplementedError
6769

68-
def collectOptionalArgs(self, dispatcher):
70+
def collect_optional_args(self, dispatcher):
6971
"""
7072
Prepares the optional arguments that are the same for all types of
7173
handlers
@@ -78,3 +80,10 @@ def collectOptionalArgs(self, dispatcher):
7880
optional_args['update_queue'] = dispatcher.update_queue
7981

8082
return optional_args
83+
84+
# old non-PEP8 Handler methods
85+
m = "telegram.Handler."
86+
checkUpdate = deprecate(check_update, m + "checkUpdate", m + "check_update")
87+
handleUpdate = deprecate(handle_update, m + "handleUpdate", m + "handle_update")
88+
collectOptionalArgs = deprecate(collect_optional_args,
89+
m + "collectOptionalArgs", m + "collect_optional_args")

telegram/ext/inlinequeryhandler.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
from .handler import Handler
2323
from telegram import Update
24+
from telegram.utils.deprecate import deprecate
2425

2526

2627
class InlineQueryHandler(Handler):
@@ -29,7 +30,7 @@ class InlineQueryHandler(Handler):
2930
3031
Args:
3132
callback (function): A function that takes ``bot, update`` as
32-
positional arguments. It will be called when the ``checkUpdate``
33+
positional arguments. It will be called when the ``check_update``
3334
has determined that an update should be processed by this handler.
3435
pass_update_queue (optional[bool]): If the handler should be passed the
3536
update queue as a keyword argument called ``update_queue``. It can
@@ -39,10 +40,15 @@ class InlineQueryHandler(Handler):
3940
def __init__(self, callback, pass_update_queue=False):
4041
super(InlineQueryHandler, self).__init__(callback, pass_update_queue)
4142

42-
def checkUpdate(self, update):
43+
def check_update(self, update):
4344
return isinstance(update, Update) and update.inline_query
4445

45-
def handleUpdate(self, update, dispatcher):
46-
optional_args = self.collectOptionalArgs(dispatcher)
46+
def handle_update(self, update, dispatcher):
47+
optional_args = self.collect_optional_args(dispatcher)
4748

4849
self.callback(dispatcher.bot, update, **optional_args)
50+
51+
# old non-PEP8 Handler methods
52+
m = "telegram.InlineQueryHandler."
53+
checkUpdate = deprecate(check_update, m + "checkUpdate", m + "check_update")
54+
handleUpdate = deprecate(handle_update, m + "handleUpdate", m + "handle_update")

telegram/ext/messagehandler.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
from .handler import Handler
2323
from telegram import Update
24+
from telegram.utils.deprecate import deprecate
2425

2526

2627
class Filters(object):
@@ -103,7 +104,7 @@ class MessageHandler(Handler):
103104
accepted. If ``bool(filters)`` evaluates to ``False``, messages are
104105
not filtered.
105106
callback (function): A function that takes ``bot, update`` as
106-
positional arguments. It will be called when the ``checkUpdate``
107+
positional arguments. It will be called when the ``check_update``
107108
has determined that an update should be processed by this handler.
108109
pass_update_queue (optional[bool]): If the handler should be passed the
109110
update queue as a keyword argument called ``update_queue``. It can
@@ -114,7 +115,7 @@ def __init__(self, filters, callback, pass_update_queue=False):
114115
super(MessageHandler, self).__init__(callback, pass_update_queue)
115116
self.filters = filters
116117

117-
def checkUpdate(self, update):
118+
def check_update(self, update):
118119
if isinstance(update, Update) and update.message:
119120
if not self.filters:
120121
res = True
@@ -124,7 +125,12 @@ def checkUpdate(self, update):
124125
res = False
125126
return res
126127

127-
def handleUpdate(self, update, dispatcher):
128-
optional_args = self.collectOptionalArgs(dispatcher)
128+
def handle_update(self, update, dispatcher):
129+
optional_args = self.collect_optional_args(dispatcher)
129130

130131
self.callback(dispatcher.bot, update, **optional_args)
132+
133+
# old non-PEP8 Handler methods
134+
m = "telegram.MessageHandler."
135+
checkUpdate = deprecate(check_update, m + "checkUpdate", m + "check_update")
136+
handleUpdate = deprecate(handle_update, m + "handleUpdate", m + "handle_update")

telegram/ext/regexhandler.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
from .handler import Handler
2727
from telegram import Update
28+
from telegram.utils.deprecate import deprecate
2829

2930

3031
class RegexHandler(Handler):
@@ -37,7 +38,7 @@ class RegexHandler(Handler):
3738
Args:
3839
pattern (str or Pattern): The regex pattern.
3940
callback (function): A function that takes ``bot, update`` as
40-
positional arguments. It will be called when the ``checkUpdate``
41+
positional arguments. It will be called when the ``check_update``
4142
has determined that an update should be processed by this handler.
4243
pass_groups (optional[bool]): If the callback should be passed the
4344
result of ``re.match(pattern, text).groups()`` as a keyword
@@ -61,7 +62,7 @@ def __init__(self, pattern, callback, pass_groups=False,
6162
self.pass_groups = pass_groups
6263
self.pass_groupdict = pass_groupdict
6364

64-
def checkUpdate(self, update):
65+
def check_update(self, update):
6566
if (isinstance(update, Update) and
6667
update.message and
6768
update.message.text):
@@ -70,8 +71,8 @@ def checkUpdate(self, update):
7071
else:
7172
return False
7273

73-
def handleUpdate(self, update, dispatcher):
74-
optional_args = self.collectOptionalArgs(dispatcher)
74+
def handle_update(self, update, dispatcher):
75+
optional_args = self.collect_optional_args(dispatcher)
7576
match = re.match(self.pattern, update.message.text)
7677

7778
if self.pass_groups:
@@ -80,3 +81,8 @@ def handleUpdate(self, update, dispatcher):
8081
optional_args['groupdict'] = match.groupdict()
8182

8283
self.callback(dispatcher.bot, update, **optional_args)
84+
85+
# old non-PEP8 Handler methods
86+
m = "telegram.RegexHandler."
87+
checkUpdate = deprecate(check_update, m + "checkUpdate", m + "check_update")
88+
handleUpdate = deprecate(handle_update, m + "handleUpdate", m + "handle_update")

telegram/ext/stringcommandhandler.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
""" This module contains the StringCommandHandler class """
2121

2222
from .handler import Handler
23+
from telegram.utils.deprecate import deprecate
2324

2425

2526
class StringCommandHandler(Handler):
@@ -30,7 +31,7 @@ class StringCommandHandler(Handler):
3031
Args:
3132
command (str): The name of the command this handler should listen for.
3233
callback (function): A function that takes ``bot, update`` as
33-
positional arguments. It will be called when the ``checkUpdate``
34+
positional arguments. It will be called when the ``check_update``
3435
has determined that an update should be processed by this handler.
3536
pass_args (optional[bool]): If the handler should be passed the
3637
arguments passed to the command as a keyword argument called `
@@ -47,15 +48,20 @@ def __init__(self, command, callback, pass_args=False,
4748
self.command = command
4849
self.pass_args = pass_args
4950

50-
def checkUpdate(self, update):
51+
def check_update(self, update):
5152
return (isinstance(update, str) and
5253
update.startswith('/') and
5354
update[1:].split(' ')[0] == self.command)
5455

55-
def handleUpdate(self, update, dispatcher):
56-
optional_args = self.collectOptionalArgs(dispatcher)
56+
def handle_update(self, update, dispatcher):
57+
optional_args = self.collect_optional_args(dispatcher)
5758

5859
if self.pass_args:
5960
optional_args['args'] = update.split(' ')[1:]
6061

6162
self.callback(dispatcher.bot, update, **optional_args)
63+
64+
# old non-PEP8 Handler methods
65+
m = "telegram.StringCommandHandler."
66+
checkUpdate = deprecate(check_update, m + "checkUpdate", m + "check_update")
67+
handleUpdate = deprecate(handle_update, m + "handleUpdate", m + "handle_update")

0 commit comments

Comments
 (0)
X Tutup