2323
2424
2525class Handler (object ):
26- """The base class for all update handlers. Create custom handlers by inheriting from it.
26+ """
27+ The base class for all update handlers. Create custom handlers by inheriting from it.
28+
29+ If your subclass needs the *autowiring* functionality, make sure to call ``set_autowired_flags``
30+ **after** initializing the ``pass_*`` members. The ``passable`` argument to this method denotes
31+ all the flags your Handler supports, e.g. ``{'update_queue', 'job_queue', 'args'}``.
2732
2833 Attributes:
2934 callback (:obj:`callable`): The callback function for this handler.
@@ -49,10 +54,9 @@ class Handler(object):
4954 It will be called when the :attr:`check_update` has determined that an update should be
5055 processed by this handler.
5156 autowire (:obj:`bool`, optional): If set to ``True``, your callback handler will be
52- inspected for positional arguments and pass objects whose names match any of the
57+ inspected for positional arguments and be passed objects whose names match any of the
5358 ``pass_*`` flags of this Handler. Using any ``pass_*`` argument in conjunction with
54- ``autowire`` will yield
55- a warning.
59+ ``autowire`` will yield a warning.
5660 pass_update_queue (:obj:`bool`, optional): If set to ``True``, a keyword argument called
5761 ``update_queue`` will be passed to the callback function. It will be the ``Queue``
5862 instance used by the :class:`telegram.ext.Updater` and :class:`telegram.ext.Dispatcher`
@@ -116,7 +120,7 @@ def handle_update(self, update, dispatcher):
116120 def __get_available_pass_flags (self ):
117121 """
118122 Used to provide warnings if the user decides to use `autowire` in conjunction with
119- `pass_*` flags, and to recalculate all flags.
123+ `` pass_*` ` flags, and to recalculate all flags.
120124
121125 Getting objects dynamically is better than hard-coding all passable objects and setting
122126 them to False in here, because the base class should not know about the existence of
@@ -126,9 +130,15 @@ def __get_available_pass_flags(self):
126130
127131 def set_autowired_flags (self , passable = {'update_queue' , 'job_queue' , 'user_data' , 'chat_data' }):
128132 """
133+ This method inspects the callback handler for used arguments. If it finds arguments that
134+ are ``passable``, i.e. types that can also be passed by the various ``pass_*`` flags,
135+ it sets the according flags to true.
136+
137+ If the handler signature is prone to change at runtime for whatever reason, you can call
138+ this method again to recalculate the flags to use.
129139
130- Make the passable arguments explicit as opposed to dynamically generated to be absolutely
131- safe that no arguments will be passed that are not allowed.
140+ The `` passable`` arguments are required to be explicit as opposed to dynamically generated
141+ to be absolutely safe that no arguments will be passed that are not allowed.
132142 """
133143
134144 if not self .autowire :
@@ -192,7 +202,8 @@ def collect_optional_args(self, dispatcher, update=None):
192202 optional_args = dict ()
193203
194204 if self .autowire :
195- # Subclasses are responsible for calling `set_autowired_flags` in their __init__
205+ # Subclasses are responsible for calling `set_autowired_flags`
206+ # at the end of their __init__
196207 assert self ._autowire_initialized
197208
198209 if self .pass_update_queue :
@@ -209,6 +220,20 @@ def collect_optional_args(self, dispatcher, update=None):
209220 return optional_args
210221
211222 def collect_bot_update_args (self , dispatcher , update ):
223+ """
224+ Prepares the positional arguments ``bot`` and/or ``update`` that are required for every
225+ python-telegram-bot handler that is not **autowired**. If ``autowire`` is set to ``True``,
226+ this method uses the inspected callback arguments to decide whether bot or update,
227+ respectively, need to be passed. The order is always (bot, update).
228+
229+
230+ Args:
231+ dispatcher (:class:`telegram.ext.Dispatcher`): The dispatcher.
232+ update (:class:`telegram.Update`): The update.
233+
234+ Returns:
235+ A tuple of bot, update, or both
236+ """
212237 if self .autowire :
213238 # Subclasses are responsible for calling `set_autowired_flags` in their __init__
214239 assert self ._autowire_initialized
@@ -218,6 +243,6 @@ def collect_bot_update_args(self, dispatcher, update):
218243 positional_args .append (dispatcher .bot )
219244 if 'update' in self ._callback_args :
220245 positional_args .append (update )
221- return positional_args
246+ return tuple ( positional_args )
222247 else :
223248 return (dispatcher .bot , update )
0 commit comments