Skip to content

Commit 5da1bb1

Browse files
committed
Switched to setattr() to reduce cyclomatic complexity
1 parent b388d1a commit 5da1bb1

File tree

2 files changed

+10
-17
lines changed

2 files changed

+10
-17
lines changed

telegram/ext/handler.py

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ class Handler(object):
7272
7373
"""
7474

75+
PASSABLE_OBJECTS = {'update_queue', 'job_queue', 'user_data', 'chat_data', 'args', 'groups', 'groupdict'}
76+
7577
def __init__(self,
7678
callback,
7779
autowire=False,
@@ -143,8 +145,7 @@ def __should_pass_obj(self, name):
143145
the user handler's signature, makes sense in this context,
144146
and is not explicitly set to `False`.
145147
"""
146-
all_passable_objects = {'update_queue', 'job_queue', 'user_data', 'chat_data', 'args', 'groups', 'groupdict'}
147-
is_requested = name in all_passable_objects and name in self._callback_args
148+
is_requested = name in self.PASSABLE_OBJECTS and name in self._callback_args
148149
if is_requested and name not in self._passable:
149150
warnings.warn("The argument `{}` cannot be autowired since it is not available "
150151
"on `{}s`.".format(name, type(self).__name__))
@@ -162,6 +163,9 @@ def set_autowired_flags(self, passable={'update_queue', 'job_queue', 'user_data'
162163
163164
The ``passable`` arguments are required to be explicit as opposed to dynamically generated
164165
to be absolutely safe that no arguments will be passed that are not allowed.
166+
167+
Args:
168+
passable: An iterable that contains the allowed flags for this handler
165169
"""
166170
self._passable = passable
167171

@@ -178,20 +182,10 @@ def set_autowired_flags(self, passable={'update_queue', 'job_queue', 'user_data'
178182

179183
self._callback_args = inspect_arguments(self.callback)
180184

181-
if self.__should_pass_obj('update_queue'):
182-
self.pass_update_queue = True
183-
if self.__should_pass_obj('job_queue'):
184-
self.pass_job_queue = True
185-
if self.__should_pass_obj('user_data'):
186-
self.pass_user_data = True
187-
if self.__should_pass_obj('chat_data'):
188-
self.pass_chat_data = True
189-
if self.__should_pass_obj('args'):
190-
self.pass_args = True
191-
if self.__should_pass_obj('groups'):
192-
self.pass_groups = True
193-
if self.__should_pass_obj('groupdict'):
194-
self.pass_groupdict = True
185+
# Actually set `pass_*` flags to True
186+
for to_pass in self.PASSABLE_OBJECTS:
187+
if self.__should_pass_obj(to_pass):
188+
setattr(self, 'pass_' + to_pass, True)
195189

196190
self._autowire_initialized = True
197191

tests/test_handler.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
from telegram.ext import Handler
2323

24-
2524
class TestHandler(object):
2625
test_flag = False
2726

0 commit comments

Comments
 (0)