python-whistle/whistle

Add type hinting for listeners

wajdijurry opened this issue · 1 comments

It's more convenient to add a type hinting for listeners to avoid passing incorrect types as listeners.

As per whistle/dispatcher.py:

    def do_dispatch(self, listeners, event):
        for listener in listeners:
            listener(event)
            if event.propagation_stopped: break

listener is expected to be callable, what if another type passed? It's better to check for listener to be a valid callable, like:

   from typing import Callable
    def do_dispatch(self, listeners, event):
       # Using type hinting
        listener: Callable
        for listener in listeners:
            # Or, using isinstance
            if not isinstance(listener, Callable):
               raise InvalidListener()
            listener(event)
            if event.propagation_stopped: break

Won't change 1.x unless there is a critical bug, but you can provide a pull request for 2.x. I think it should do more than expect a "callable", as this callable should have a specific prototype. I still work on understanding python protocols, but that should be possible.

Closing the "issue", but feel free to open a pull request to discuss this.