Skip to content

Commit 0c99152

Browse files
zeshuaroPoolitzerBibo-Joshi
authored
Add run_async Parameter to ConversationHandler (python-telegram-bot#2292)
* Add run_async parameter * Update docstring * Update test to explicitly specify parameter * Fix test job queue * Add version added tag to docs * Update docstring Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com> * Doc nitpicking Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com> Co-authored-by: Hinrich Mahler <hinrich.mahler@freenet.de>
1 parent 9930725 commit 0c99152

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

telegram/ext/conversationhandler.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,15 @@ class ConversationHandler(Handler[Update]):
136136
map_to_parent (Dict[:obj:`object`, :obj:`object`], optional): A :obj:`dict` that can be
137137
used to instruct a nested conversationhandler to transition into a mapped state on
138138
its parent conversationhandler in place of a specified nested state.
139+
run_async (:obj:`bool`, optional): Pass :obj:`True` to *override* the
140+
:attr:`Handler.run_async` setting of all handlers (in :attr:`entry_points`,
141+
:attr:`states` and :attr:`fallbacks`).
142+
143+
Note:
144+
If set to :obj:`True`, you should not pass a handler instance, that needs to be
145+
run synchronously in another context.
146+
147+
.. versionadded:: 13.2
139148
140149
Raises:
141150
ValueError
@@ -168,6 +177,10 @@ class ConversationHandler(Handler[Update]):
168177
map_to_parent (Dict[:obj:`object`, :obj:`object`]): Optional. A :obj:`dict` that can be
169178
used to instruct a nested conversationhandler to transition into a mapped state on
170179
its parent conversationhandler in place of a specified nested state.
180+
run_async (:obj:`bool`): If :obj:`True`, will override the
181+
:attr:`Handler.run_async` setting of all internal handlers on initialization.
182+
183+
.. versionadded:: 13.2
171184
172185
"""
173186

@@ -192,8 +205,9 @@ def __init__(
192205
name: str = None,
193206
persistent: bool = False,
194207
map_to_parent: Dict[object, object] = None,
208+
run_async: bool = False,
195209
):
196-
self.run_async = False
210+
self.run_async = run_async
197211

198212
self._entry_points = entry_points
199213
self._states = states
@@ -229,7 +243,7 @@ def __init__(
229243
"since message IDs are not globally unique."
230244
)
231245

232-
all_handlers = list()
246+
all_handlers: List[Handler] = list()
233247
all_handlers.extend(entry_points)
234248
all_handlers.extend(fallbacks)
235249

@@ -263,6 +277,10 @@ def __init__(
263277
)
264278
break
265279

280+
if self.run_async:
281+
for handler in all_handlers:
282+
handler.run_async = True
283+
266284
@property
267285
def entry_points(self) -> List[Handler]:
268286
return self._entry_points

tests/test_conversationhandler.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,3 +1429,36 @@ def test_callback(u, c):
14291429
assert self.current_state[user1.id] == self.STOPPING
14301430
assert handler.conversations.get((0, user1.id)) is None
14311431
assert not self.test_flag
1432+
1433+
def test_conversation_handler_run_async_true(self, dp):
1434+
conv_handler = ConversationHandler(
1435+
entry_points=self.entry_points,
1436+
states=self.states,
1437+
fallbacks=self.fallbacks,
1438+
run_async=True,
1439+
)
1440+
1441+
all_handlers = conv_handler.entry_points + conv_handler.fallbacks
1442+
for state_handlers in conv_handler.states.values():
1443+
all_handlers += state_handlers
1444+
1445+
for handler in all_handlers:
1446+
assert handler.run_async
1447+
1448+
def test_conversation_handler_run_async_false(self, dp):
1449+
conv_handler = ConversationHandler(
1450+
entry_points=[CommandHandler('start', self.start_end, run_async=True)],
1451+
states=self.states,
1452+
fallbacks=self.fallbacks,
1453+
run_async=False,
1454+
)
1455+
1456+
for handler in conv_handler.entry_points:
1457+
assert handler.run_async
1458+
1459+
all_handlers = conv_handler.fallbacks
1460+
for state_handlers in conv_handler.states.values():
1461+
all_handlers += state_handlers
1462+
1463+
for handler in all_handlers:
1464+
assert not handler.run_async.value

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy