From 41eddffc1ba69991f584f104f78253c8768b7cf8 Mon Sep 17 00:00:00 2001 From: Zomatree Date: Wed, 30 Mar 2022 19:03:03 +0100 Subject: [PATCH] make context generic over client --- revolt/ext/commands/client.py | 3 ++- revolt/ext/commands/context.py | 8 +++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/revolt/ext/commands/client.py b/revolt/ext/commands/client.py index 2d5f1e6..dfe9a82 100755 --- a/revolt/ext/commands/client.py +++ b/revolt/ext/commands/client.py @@ -3,6 +3,7 @@ from __future__ import annotations import traceback import sys from typing import Any, Union, Protocol, runtime_checkable, Optional, TYPE_CHECKING +from typing_extensions import Self from importlib import import_module import revolt @@ -159,7 +160,7 @@ class CommandsClient(revolt.Client, metaclass=CommandsMeta): def get_view(self, message: revolt.Message) -> type[StringView]: return StringView - def get_context(self, message: revolt.Message) -> type[Context]: + def get_context(self, message: revolt.Message) -> type[Context[Self]]: return Context async def process_commands(self, message: revolt.Message) -> Any: diff --git a/revolt/ext/commands/context.py b/revolt/ext/commands/context.py index db5322a..61537ac 100755 --- a/revolt/ext/commands/context.py +++ b/revolt/ext/commands/context.py @@ -1,6 +1,6 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Any, Optional +from typing import TYPE_CHECKING, Any, Generic, Optional, TypeVar import revolt from revolt.utils import maybe_coroutine @@ -12,11 +12,13 @@ if TYPE_CHECKING: from .client import CommandsClient from .view import StringView +ClientT = TypeVar("ClientT", bound="CommandsClient") + __all__ = ( "Context", ) -class Context(revolt.Messageable): +class Context(revolt.Messageable, Generic[ClientT]): """Stores metadata the commands execution. Attributes @@ -45,7 +47,7 @@ class Context(revolt.Messageable): async def _get_channel_id(self) -> str: return self.channel.id - def __init__(self, command: Optional[Command], invoked_with: str, view: StringView, message: revolt.Message, client: CommandsClient): + def __init__(self, command: Optional[Command], invoked_with: str, view: StringView, message: revolt.Message, client: ClientT): self.command = command self.invoked_with = invoked_with self.view = view