From a2c2b5e2dc07790f590178ece7c3a8ec53b8c2e7 Mon Sep 17 00:00:00 2001 From: Luna-Klatzer Date: Fri, 3 Sep 2021 18:20:52 +0200 Subject: [PATCH] Added Default coroutine behaviour for `wait_for`, which by default adds a new empty coro, so that the args and kwargs can be accessed as returns. --- CHANGELOG.md | 8 +++++--- openhivenpy/events/__init__.py | 26 ++++++++++++++++++-------- pytest/test_core/test_events.py | 2 +- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index db0b0c6..1aae78d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,8 +13,10 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## Unreleased ### Added -- Added parsers for events `on_message_create` and `on_message_delete` -- Added new property `room_ids` to `HivenClient` +- Parsers for events `on_message_create` and `on_message_delete` +- New property `room_ids` to `HivenClient` +- Default coroutine behaviour for `wait_for`, which by default adds a new empty + coro, so that the args and kwargs can be accessed as returns. ### Changed - Moved cleanup from `HivenClient.close()` to `HivenClient.connect()` to clean up even when the Client closes unexpectedly or due to an issue. @@ -26,7 +28,7 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). - Fixed `rooms` and `entities` property in `House` - Fixed `mentions`, `author`, `house` and `attachment` property in `Message` ### Removed -- unnecessary reset of Client-User and the call of `init_client_user_obj` in +- Unnecessary reset of Client-User and the call of `init_client_user_obj` in `ClientCache.closing_cleanup()` - `log_websocket` from the `ClientCache` diff --git a/openhivenpy/events/__init__.py b/openhivenpy/events/__init__.py index da0088a..b08cfd9 100644 --- a/openhivenpy/events/__init__.py +++ b/openhivenpy/events/__init__.py @@ -143,14 +143,13 @@ def set_awaitable(self, awaitable: Union[Awaitable, Callable]) -> None: or standard asyncio awaitable functions are defined using the `async def` syntax """ - if inspect.iscoroutine(awaitable): - raise RuntimeError( - "Coroutine are not allowed as event_listener awaitable") - elif inspect.isawaitable(awaitable) or inspect.iscoroutinefunction( - awaitable): + if inspect.isawaitable(awaitable) \ + or inspect.iscoroutinefunction(awaitable): self._awaitable = awaitable else: - raise RuntimeError(f"Expected awaitable, but got {type(awaitable)}") + raise RuntimeError( + f"Expected awaitable, but got {type(awaitable)}" + ) async def dispatch(self) -> None: ... @@ -176,7 +175,9 @@ def __repr__(self): ('dispatched', self.dispatched), ('awaitable', getattr(self, 'awaitable', None)) ] - return ''.format(' '.join('%s=%s' % t for t in info)) + return ''.format( + ' '.join('%s=%s' % t for t in info) + ) @property def dispatched(self) -> bool: @@ -208,7 +209,10 @@ async def dispatch(self, *args, **kwargs) -> None: brief=f"[EVENTS] Ignoring exception in {repr(self)}:", exc_info=sys.exc_info() ) - raise RuntimeError(f"Failed to execute assigned coroutine '{self.awaitable.__name__}'") from e + raise RuntimeError( + f"Failed to execute assigned coroutine " + f"'{self.awaitable.__name__}'" + ) from e self._dispatched = True self.client.remove_listener(self) @@ -384,6 +388,12 @@ async def wait_for( "The passed event type is invalid/does not exist" ) + if awaitable is None: + async def _empty(*args, **kwargs): + ... + + awaitable = _empty + listener = self.add_single_listener(event_name, awaitable) while not listener.dispatched: await asyncio.sleep(.05) diff --git a/pytest/test_core/test_events.py b/pytest/test_core/test_events.py index 7823706..b556dbf 100644 --- a/pytest/test_core/test_events.py +++ b/pytest/test_core/test_events.py @@ -149,7 +149,7 @@ async def on_ready(): client.run(token) - def test_wait_for(self): + def test_wait_for_simple_coroutine(self): async def on_ready(): print("\non_ready was called!") return