Skip to content

Conversation

@ihoru
Copy link
Contributor

@ihoru ihoru commented Jul 23, 2017

There are situations I have very often when I need to control a flow of handlers execution.

Example for Continue:

I need to process a photo in different scopes (contexts): when a user got some state and in all other situations.
Using changes in this PR, I will create two handlers (in the same group):

  1. first, will process more particular case (user has a state)
  2. second, for general situations (everything else)

First will check if a user has not a state it will raise Continue.

Example for Stop:

Some handlers have to be able to stop processing of update without any doubts if there are any other groups or not.

Look at test_dispatcher_flow.py if you need extra explanation.

@Eldinnie
Copy link
Member

This seems very similar to #667 Please take a look at the error's in travis' log.

Calling super().setUp removed, it's useless and leads to errors in Python 2.7
@ihoru
Copy link
Contributor Author

ihoru commented Jul 23, 2017

@Eldinnie, you are right, #667 have similar logic as here but only one from two points.

Errors fixed. (Other problems are not mine :) )

@ihoru
Copy link
Contributor Author

ihoru commented Jul 24, 2017

Conflict with master fixed.

Copy link
Member

@tsnoam tsnoam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ihoru thank you for your contribution.
I have left some (nitpick) comments on the code.
However, I have some bigger rejects about the code:

  1. I am not fond of the "coding by exception" design pattern. I prefer the handlers to return an enum of the desired action (or None, for backward compatibility). I find this design pattern to force the developer to write more complex and less fluent code which is hard to maintain.
    So I am inclined to ask you to change that code to be using enums.

EDIT:
After discussing the issue with the other developers we've come to conclusion that exceptions are the proper way to handle this. So you can ignore the first bullet.

  1. I suspect that this code will not work with run_async (a certain refactoring is needed for Promise & Dispatcher to make it work, but this is regardless to your proposed change, we have other outstanding issues with it.)
    N.B.: I don't think that the support for run_async will work any differently if used with exceptions or enums, but both should be checked and proven.

  2. Following the above, the unitests should test run_async as well.

  3. Documentation for the feature should be added to Dispatcher.add_handler().

EDIT2:
5. The names Continue and Stop are basically ok, up to the limit that they should be globally exported via telegram/ext/__init__.py. So I propose other names like: DispatcherHandlerContinue & DispatcherHandlerStop.

import weakref
from functools import wraps
from threading import Thread, Lock, Event, current_thread, BoundedSemaphore
from threading import BoundedSemaphore, Event, Lock, Thread, current_thread
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it's only an IDE error, and even though it is alphabetically sorted now - please remove it from the PR. I want the minimal possible change.

from collections import defaultdict

from queue import Queue, Empty
from queue import Empty, Queue
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as above (also, keep the empty line where it was... minimal change...)

Args:
update (:obj:`str` | :class:`telegram.Update`): The update to process.
update (:obj:`str` | :class:`telegram.Update` | :class:`telegram.TelegramError`): The update to process.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use the pep8 standard of 99 chars per line.
Please fix.

@tsnoam
Copy link
Member

tsnoam commented Jul 28, 2017

notice the edit on the review comments.

@ihoru
Copy link
Contributor Author

ihoru commented Jul 29, 2017

TODO:

  • line length
  • revert touched imports
  • rename Continue -> DispatcherHandlerContinue, Stop -> DispatcherHandlerStop
  • global export for DispatcherHandlerContinue and DispatcherHandlerStop from ext
  • tests over run_async
  • if this does not work with run_async fix it
  • Documentation for the feature should be added to Dispatcher.add_handler()

ihoru added 4 commits July 29, 2017 11:31
- line length fix
- reverted touched imports
- classes renamed Continue -> DispatcherHandlerContinue, Stop -> DispatcherHandlerStop
- Documentation of Dispatcher.add_handler improved
- global import of run_async from telegram.ext
Args:
update (:obj:`str` | :class:`telegram.Update`): The update to process.
update (:obj:`str` | :class:`telegram.Update` | :class:`telegram.TelegramError`):
The update to process.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra 4 spaces before The update to process is needed.

@@ -0,0 +1,135 @@
#!/usr/bin/env python
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you please include these tests as part of test_updater.py?

@tsnoam
Copy link
Member

tsnoam commented Jul 29, 2017

@ihoru
as discussed:

  1. I left two minor comments above ^^.
  2. It doesn't make sense to support run_async here, so this is no longer a requirement. What we do need is to modify Dispatcher.run_async() so it will make sure that a flow control exception wasn't raised and if it was issue a warning that it was ignored/not-supported with run_async.
  3. Documentation should use the new syntax, for example:
:class:`telegram.ext.DispatcherHandlerStop`

- documentation fixes
- tests moved to common file test_updater.py
- @Property Promise.exception added
- log warning in case of using DispatcherHandlerFlow in executed method asynchronously
@tsnoam tsnoam merged commit 6aacde1 into python-telegram-bot:master Jul 29, 2017
@tsnoam
Copy link
Member

tsnoam commented Jul 29, 2017

@ihoru
Thank you for your contribution.
btw, I forgot to ask but if you're not on AUTHORS.rst and want to be included you can send a PR for that. (or add it to your other PR)

@ihoru ihoru deleted the dispatcher-flow-control branch July 30, 2017 02:47
@github-actions github-actions bot locked and limited conversation to collaborators Aug 23, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants