From 2c00a966a73f98069a7579b1ab98add547a2d2d4 Mon Sep 17 00:00:00 2001 From: Lunarmagpie Date: Fri, 20 Jan 2023 14:41:21 -0500 Subject: [PATCH 1/4] add event decorator --- docs/source/api_references/events.rst | 6 +++++ toolbox/events.py | 34 +++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 docs/source/api_references/events.rst create mode 100644 toolbox/events.py diff --git a/docs/source/api_references/events.rst b/docs/source/api_references/events.rst new file mode 100644 index 0000000..a5219e2 --- /dev/null +++ b/docs/source/api_references/events.rst @@ -0,0 +1,6 @@ +==================== +Events API Reference +==================== + +.. automodule:: toolbox.events + :members: \ No newline at end of file diff --git a/toolbox/events.py b/toolbox/events.py new file mode 100644 index 0000000..20ac13f --- /dev/null +++ b/toolbox/events.py @@ -0,0 +1,34 @@ +import typing as t + +import hikari +import functools + +__all__: t.Sequence[str] = ["consume_event"] + +P = t.ParamSpec("P") +T = t.TypeVar("T") + + +def consume_event(callback: t.Callable[P, T]) -> t.Callable[t.Concatenate[hikari.Event, P], T]: + """ + Consume the first argument of an event callback. + + .. code-block:: python + + import hikari + import toolbox + + @toolbox.consume_event + async def on_started(): + ... + + bot = hikari.GatewayBot("TOKEN") + bot.subscribe(hikari.StartingEvent, on_started) + bot.run() + """ + + @functools.wraps(callback) + async def inner(_: hikari.Event, *args: P.args, **kwargs: P.kwargs) -> T: + return await callback(*args, **kwargs) + + return inner From dd83046bece59e59f309f1ab3d6bd77085a13124 Mon Sep 17 00:00:00 2001 From: Lunarmagpie Date: Fri, 20 Jan 2023 14:50:04 -0500 Subject: [PATCH 2/4] export --- toolbox/__init__.py | 1 + toolbox/events.py | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/toolbox/__init__.py b/toolbox/__init__.py index 4da249a..e348b0d 100644 --- a/toolbox/__init__.py +++ b/toolbox/__init__.py @@ -1,5 +1,6 @@ from .commands import * from .errors import * +from .events import * from .members import * from .messages import * from .roles import * diff --git a/toolbox/events.py b/toolbox/events.py index 20ac13f..6990f31 100644 --- a/toolbox/events.py +++ b/toolbox/events.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import typing as t import hikari @@ -5,7 +7,9 @@ __all__: t.Sequence[str] = ["consume_event"] -P = t.ParamSpec("P") +if t.TYPE_CHECKING: + P = t.ParamSpec("P") + T = t.TypeVar("T") From 0bf30eaec22ec33e93107228a4d1f18e63fbfaab Mon Sep 17 00:00:00 2001 From: Lunarmagpie Date: Fri, 20 Jan 2023 14:50:24 -0500 Subject: [PATCH 3/4] this looks a little nicer --- toolbox/events.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/toolbox/events.py b/toolbox/events.py index 6990f31..7894968 100644 --- a/toolbox/events.py +++ b/toolbox/events.py @@ -9,8 +9,7 @@ if t.TYPE_CHECKING: P = t.ParamSpec("P") - -T = t.TypeVar("T") + T = t.TypeVar("T") def consume_event(callback: t.Callable[P, T]) -> t.Callable[t.Concatenate[hikari.Event, P], T]: From 92bbf964f1498ba85c859b771b59c2a768c26362 Mon Sep 17 00:00:00 2001 From: Lunarmagpie Date: Fri, 20 Jan 2023 15:00:19 -0500 Subject: [PATCH 4/4] add dependency cause i give up --- requirements.txt | 3 ++- toolbox/events.py | 12 +++++++----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/requirements.txt b/requirements.txt index 8eb87ad..52c777e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ -hikari>=2.0.0.dev113 \ No newline at end of file +hikari>=2.0.0.dev113 +typing_extensions>=4.4.0 \ No newline at end of file diff --git a/toolbox/events.py b/toolbox/events.py index 7894968..44520cf 100644 --- a/toolbox/events.py +++ b/toolbox/events.py @@ -1,18 +1,20 @@ from __future__ import annotations import typing as t +import typing_extensions as te import hikari import functools __all__: t.Sequence[str] = ["consume_event"] -if t.TYPE_CHECKING: - P = t.ParamSpec("P") - T = t.TypeVar("T") +P = te.ParamSpec("P") +T = t.TypeVar("T") -def consume_event(callback: t.Callable[P, T]) -> t.Callable[t.Concatenate[hikari.Event, P], T]: +def consume_event( + callback: t.Callable[P, t.Awaitable[T]] +) -> t.Callable[te.Concatenate[hikari.Event, P], t.Awaitable[T]]: """ Consume the first argument of an event callback. @@ -31,7 +33,7 @@ async def on_started(): """ @functools.wraps(callback) - async def inner(_: hikari.Event, *args: P.args, **kwargs: P.kwargs) -> T: + async def inner(_event: hikari.Event, /, *args: P.args, **kwargs: P.kwargs) -> T: return await callback(*args, **kwargs) return inner