diff --git a/discord/ext/commands/_types.py b/discord/ext/commands/_types.py index 5c57a330e..b70a4bfdb 100644 --- a/discord/ext/commands/_types.py +++ b/discord/ext/commands/_types.py @@ -23,7 +23,7 @@ DEALINGS IN THE SOFTWARE. """ -from typing import Any, Awaitable, Callable, Coroutine, TYPE_CHECKING, TypeVar, Union, Tuple +from typing import Any, Awaitable, Callable, Coroutine, TYPE_CHECKING, TypeVar, Union, Tuple, Optional T = TypeVar('T') @@ -48,14 +48,13 @@ CoroFunc = Callable[..., Coro[Any]] MaybeCoro = Union[T, Coro[T]] MaybeAwaitable = Union[T, Awaitable[T]] -Check = Union[Callable[["Cog", "ContextT"], MaybeCoro[bool]], Callable[["ContextT"], MaybeCoro[bool]]] -Hook = Union[Callable[["Cog", "ContextT"], Coro[Any]], Callable[["ContextT"], Coro[Any]]] -Error = Union[Callable[["Cog", "ContextT", "CommandError"], Coro[Any]], Callable[["ContextT", "CommandError"], Coro[Any]]] +CogT = TypeVar('CogT', bound='Optional[Cog]') +Check = Callable[["ContextT"], MaybeCoro[bool]] +Hook = Union[Callable[["CogT", "ContextT"], Coro[Any]], Callable[["ContextT"], Coro[Any]]] +Error = Union[Callable[["CogT", "ContextT", "CommandError"], Coro[Any]], Callable[["ContextT", "CommandError"], Coro[Any]]] ContextT = TypeVar('ContextT', bound='Context[Any]') BotT = TypeVar('BotT', bound=_Bot, covariant=True) -ErrorT = TypeVar('ErrorT', bound='Error[Context[Any]]') -HookT = TypeVar('HookT', bound='Hook[Context[Any]]') # This is merely a tag type to avoid circular import issues. diff --git a/discord/ext/commands/core.py b/discord/ext/commands/core.py index 033c2547c..9d253aa9c 100644 --- a/discord/ext/commands/core.py +++ b/discord/ext/commands/core.py @@ -47,7 +47,7 @@ from typing import ( import discord -from ._types import _BaseCommand +from ._types import _BaseCommand, CogT from .cog import Cog from .context import Context from .converter import Greedy, run_converters @@ -60,7 +60,7 @@ if TYPE_CHECKING: from discord.message import Message - from ._types import BotT, Check, ContextT, Coro, CoroFunc, Error, ErrorT, Hook, HookT + from ._types import BotT, Check, ContextT, Coro, CoroFunc, Error, Hook __all__ = ( @@ -93,7 +93,6 @@ __all__ = ( MISSING: Any = discord.utils.MISSING T = TypeVar('T') -CogT = TypeVar('CogT', bound='Optional[Cog]') CommandT = TypeVar('CommandT', bound='Command') # CHT = TypeVar('CHT', bound='Check') GroupT = TypeVar('GroupT', bound='Group') @@ -956,7 +955,7 @@ class Command(_BaseCommand, Generic[CogT, P, T]): if call_hooks: await self.call_after_hooks(ctx) - def error(self, coro: ErrorT, /) -> ErrorT: + def error(self, coro: Error[CogT, ContextT], /) -> Error[CogT, ContextT]: """A decorator that registers a coroutine as a local error handler. A local error handler is an :func:`.on_command_error` event limited to @@ -991,7 +990,7 @@ class Command(_BaseCommand, Generic[CogT, P, T]): """ return hasattr(self, 'on_error') - def before_invoke(self, coro: HookT, /) -> HookT: + def before_invoke(self, coro: Hook[CogT, ContextT], /) -> Hook[CogT, ContextT]: """A decorator that registers a coroutine as a pre-invoke hook. A pre-invoke hook is called directly before the command is @@ -1022,7 +1021,7 @@ class Command(_BaseCommand, Generic[CogT, P, T]): self._before_invoke = coro return coro - def after_invoke(self, coro: HookT, /) -> HookT: + def after_invoke(self, coro: Hook[CogT, ContextT], /) -> Hook[CogT, ContextT]: """A decorator that registers a coroutine as a post-invoke hook. A post-invoke hook is called directly after the command is @@ -2385,7 +2384,7 @@ def max_concurrency(number: int, per: BucketType = BucketType.default, *, wait: return decorator # type: ignore -def before_invoke(coro: Hook[ContextT], /) -> Callable[[T], T]: +def before_invoke(coro: Hook[CogT, ContextT], /) -> Callable[[T], T]: """A decorator that registers a coroutine as a pre-invoke hook. This allows you to refer to one before invoke hook for several commands that @@ -2437,7 +2436,7 @@ def before_invoke(coro: Hook[ContextT], /) -> Callable[[T], T]: return decorator # type: ignore -def after_invoke(coro: Hook[ContextT], /) -> Callable[[T], T]: +def after_invoke(coro: Hook[CogT, ContextT], /) -> Callable[[T], T]: """A decorator that registers a coroutine as a post-invoke hook. This allows you to refer to one after invoke hook for several commands that