Browse Source

[commands] Make GroupMixin Generic

pull/7440/head
Josh 4 years ago
committed by GitHub
parent
commit
166152647c
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 31
      discord/ext/commands/core.py

31
discord/ext/commands/core.py

@ -1125,7 +1125,7 @@ class Command(_BaseCommand, Generic[CogT, P, T]):
finally: finally:
ctx.command = original ctx.command = original
class GroupMixin: class GroupMixin(Generic[CogT]):
"""A mixin that implements common functionality for classes that behave """A mixin that implements common functionality for classes that behave
similar to :class:`.Group` and are allowed to register commands. similar to :class:`.Group` and are allowed to register commands.
@ -1139,12 +1139,12 @@ class GroupMixin:
""" """
def __init__(self, *args: Any, **kwargs: Any) -> None: def __init__(self, *args: Any, **kwargs: Any) -> None:
case_insensitive = kwargs.get('case_insensitive', False) case_insensitive = kwargs.get('case_insensitive', False)
self.all_commands: Dict[str, Command] = _CaseInsensitiveDict() if case_insensitive else {} self.all_commands: Dict[str, Command[CogT, Any, Any]] = _CaseInsensitiveDict() if case_insensitive else {}
self.case_insensitive: bool = case_insensitive self.case_insensitive: bool = case_insensitive
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
@property @property
def commands(self) -> Set[Command]: def commands(self) -> Set[Command[CogT, Any, Any]]:
"""Set[:class:`.Command`]: A unique set of commands without aliases that are registered.""" """Set[:class:`.Command`]: A unique set of commands without aliases that are registered."""
return set(self.all_commands.values()) return set(self.all_commands.values())
@ -1154,7 +1154,7 @@ class GroupMixin:
command.recursively_remove_all_commands() command.recursively_remove_all_commands()
self.remove_command(command.name) self.remove_command(command.name)
def add_command(self, command: Command) -> None: def add_command(self, command: Command[CogT, Any, Any]) -> None:
"""Adds a :class:`.Command` into the internal list of commands. """Adds a :class:`.Command` into the internal list of commands.
This is usually not called, instead the :meth:`~.GroupMixin.command` or This is usually not called, instead the :meth:`~.GroupMixin.command` or
@ -1192,7 +1192,7 @@ class GroupMixin:
raise CommandRegistrationError(alias, alias_conflict=True) raise CommandRegistrationError(alias, alias_conflict=True)
self.all_commands[alias] = command self.all_commands[alias] = command
def remove_command(self, name: str) -> Optional[Command]: def remove_command(self, name: str) -> Optional[Command[CogT, Any, Any]]:
"""Remove a :class:`.Command` from the internal list """Remove a :class:`.Command` from the internal list
of commands. of commands.
@ -1229,7 +1229,7 @@ class GroupMixin:
self.all_commands[alias] = cmd self.all_commands[alias] = cmd
return command return command
def walk_commands(self) -> Generator[Command, None, None]: def walk_commands(self) -> Generator[Command[CogT, Any, Any], None, None]:
"""An iterator that recursively walks through all commands and subcommands. """An iterator that recursively walks through all commands and subcommands.
.. versionchanged:: 1.4 .. versionchanged:: 1.4
@ -1245,7 +1245,7 @@ class GroupMixin:
if isinstance(command, GroupMixin): if isinstance(command, GroupMixin):
yield from command.walk_commands() yield from command.walk_commands()
def get_command(self, name: str) -> Optional[Command]: def get_command(self, name: str) -> Optional[Command[CogT, Any, Any]]:
"""Get a :class:`.Command` from the internal list """Get a :class:`.Command` from the internal list
of commands. of commands.
@ -1292,7 +1292,13 @@ class GroupMixin:
cls: Type[Command[CogT, P, T]] = ..., cls: Type[Command[CogT, P, T]] = ...,
*args: Any, *args: Any,
**kwargs: Any, **kwargs: Any,
) -> Callable[[Callable[Concatenate[ContextT, P], Coro[T]]], Command[CogT, P, T]]: ) -> Callable[
[
Union[
Callable[Concatenate[CogT, ContextT, P], Coro[T]],
Callable[Concatenate[ContextT, P], Coro[T]],
]
], Command[CogT, P, T]]:
... ...
@overload @overload
@ -1335,7 +1341,12 @@ class GroupMixin:
cls: Type[Group[CogT, P, T]] = ..., cls: Type[Group[CogT, P, T]] = ...,
*args: Any, *args: Any,
**kwargs: Any, **kwargs: Any,
) -> Callable[[Callable[Concatenate[ContextT, P], Coro[T]]], Group[CogT, P, T]]: ) -> Callable[[
Union[
Callable[Concatenate[CogT, ContextT, P], Coro[T]],
Callable[Concatenate[ContextT, P], Coro[T]]
]
], Group[CogT, P, T]]:
... ...
@overload @overload
@ -1371,7 +1382,7 @@ class GroupMixin:
return decorator return decorator
class Group(GroupMixin, Command[CogT, P, T]): class Group(GroupMixin[CogT], Command[CogT, P, T]):
"""A class that implements a grouping protocol for commands to be """A class that implements a grouping protocol for commands to be
executed as subcommands. executed as subcommands.

Loading…
Cancel
Save