[commands] Add missing and fix existing type annotations

This commit is contained in:
Stocker 2022-03-22 06:04:46 -04:00 committed by GitHub
parent 46b3e036e2
commit eca4727593
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 18 additions and 14 deletions

View File

@ -45,18 +45,20 @@ else:
P = TypeVar('P') P = TypeVar('P')
MaybeCoroFunc = Tuple[P, T] MaybeCoroFunc = Tuple[P, T]
_Bot = Union['Bot', 'AutoShardedBot']
Coro = Coroutine[Any, Any, T] Coro = Coroutine[Any, Any, T]
MaybeCoro = Union[T, Coro[T]] MaybeCoro = Union[T, Coro[T]]
CoroFunc = Callable[..., Coro[Any]] CoroFunc = Callable[..., Coro[Any]]
ContextT = TypeVar('ContextT', bound='Context')
_Bot = Union['Bot', 'AutoShardedBot']
BotT = TypeVar('BotT', bound=_Bot, covariant=True)
Check = Union[Callable[["Cog", "ContextT"], MaybeCoro[bool]], Callable[["ContextT"], MaybeCoro[bool]]] Check = Union[Callable[["Cog", "ContextT"], MaybeCoro[bool]], Callable[["ContextT"], MaybeCoro[bool]]]
Hook = Union[Callable[["Cog", "ContextT"], Coro[Any]], Callable[["ContextT"], Coro[Any]]] Hook = Union[Callable[["Cog", "ContextT"], Coro[Any]], Callable[["ContextT"], Coro[Any]]]
Error = Union[Callable[["Cog", "ContextT", "CommandError"], Coro[Any]], Callable[["ContextT", "CommandError"], Coro[Any]]] Error = Union[Callable[["Cog", "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. # This is merely a tag type to avoid circular import issues.
# Yes, this is a terrible solution but ultimately it is the only solution. # Yes, this is a terrible solution but ultimately it is the only solution.

View File

@ -151,7 +151,7 @@ class BotBase(GroupMixin[None]):
def __init__( def __init__(
self, self,
command_prefix: PrefixType[BotT], command_prefix: PrefixType[BotT],
help_command: Optional[HelpCommand] = _default, help_command: Optional[HelpCommand[Any]] = _default,
description: Optional[str] = None, description: Optional[str] = None,
**options: Any, **options: Any,
) -> None: ) -> None:
@ -166,7 +166,7 @@ class BotBase(GroupMixin[None]):
self._check_once: List[Check] = [] self._check_once: List[Check] = []
self._before_invoke: Optional[CoroFunc] = None self._before_invoke: Optional[CoroFunc] = None
self._after_invoke: Optional[CoroFunc] = None self._after_invoke: Optional[CoroFunc] = None
self._help_command: Optional[HelpCommand] = None self._help_command: Optional[HelpCommand[Any]] = None
self.description: str = inspect.cleandoc(description) if description else '' self.description: str = inspect.cleandoc(description) if description else ''
self.owner_id: Optional[int] = options.get('owner_id') self.owner_id: Optional[int] = options.get('owner_id')
self.owner_ids: Optional[Collection[int]] = options.get('owner_ids', set()) self.owner_ids: Optional[Collection[int]] = options.get('owner_ids', set())
@ -989,11 +989,11 @@ class BotBase(GroupMixin[None]):
# help command stuff # help command stuff
@property @property
def help_command(self) -> Optional[HelpCommand]: def help_command(self) -> Optional[HelpCommand[Any]]:
return self._help_command return self._help_command
@help_command.setter @help_command.setter
def help_command(self, value: Optional[HelpCommand]) -> None: def help_command(self, value: Optional[HelpCommand[Any]]) -> None:
if value is not None: if value is not None:
if not isinstance(value, HelpCommand): if not isinstance(value, HelpCommand):
raise TypeError('help_command must be a subclass of HelpCommand') raise TypeError('help_command must be a subclass of HelpCommand')

View File

@ -68,6 +68,8 @@ if TYPE_CHECKING:
Check, Check,
Hook, Hook,
Error, Error,
ErrorT,
HookT,
) )
@ -105,7 +107,6 @@ CogT = TypeVar('CogT', bound='Optional[Cog]')
CommandT = TypeVar('CommandT', bound='Command') CommandT = TypeVar('CommandT', bound='Command')
# CHT = TypeVar('CHT', bound='Check') # CHT = TypeVar('CHT', bound='Check')
GroupT = TypeVar('GroupT', bound='Group') GroupT = TypeVar('GroupT', bound='Group')
FuncT = TypeVar('FuncT', bound=Callable[..., Any])
if TYPE_CHECKING: if TYPE_CHECKING:
P = ParamSpec('P') P = ParamSpec('P')
@ -920,7 +921,7 @@ class Command(_BaseCommand, Generic[CogT, P, T]):
if call_hooks: if call_hooks:
await self.call_after_hooks(ctx) await self.call_after_hooks(ctx)
def error(self, coro: FuncT) -> FuncT: def error(self, coro: ErrorT) -> ErrorT:
"""A decorator that registers a coroutine as a local error handler. """A decorator that registers a coroutine as a local error handler.
A local error handler is an :func:`.on_command_error` event limited to A local error handler is an :func:`.on_command_error` event limited to
@ -951,7 +952,7 @@ class Command(_BaseCommand, Generic[CogT, P, T]):
""" """
return hasattr(self, 'on_error') return hasattr(self, 'on_error')
def before_invoke(self, coro: FuncT) -> FuncT: def before_invoke(self, coro: HookT) -> HookT:
"""A decorator that registers a coroutine as a pre-invoke hook. """A decorator that registers a coroutine as a pre-invoke hook.
A pre-invoke hook is called directly before the command is A pre-invoke hook is called directly before the command is
@ -978,7 +979,7 @@ class Command(_BaseCommand, Generic[CogT, P, T]):
self._before_invoke = coro self._before_invoke = coro
return coro return coro
def after_invoke(self, coro: FuncT) -> FuncT: def after_invoke(self, coro: HookT) -> HookT:
"""A decorator that registers a coroutine as a post-invoke hook. """A decorator that registers a coroutine as a post-invoke hook.
A post-invoke hook is called directly after the command is A post-invoke hook is called directly after the command is
@ -2319,7 +2320,7 @@ def max_concurrency(number: int, per: BucketType = BucketType.default, *, wait:
return decorator # type: ignore return decorator # type: ignore
def before_invoke(coro) -> Callable[[T], T]: def before_invoke(coro: Hook[ContextT]) -> Callable[[T], T]:
"""A decorator that registers a coroutine as a pre-invoke hook. """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 This allows you to refer to one before invoke hook for several commands that
@ -2367,7 +2368,7 @@ def before_invoke(coro) -> Callable[[T], T]:
return decorator # type: ignore return decorator # type: ignore
def after_invoke(coro) -> Callable[[T], T]: def after_invoke(coro: Hook[ContextT]) -> Callable[[T], T]:
"""A decorator that registers a coroutine as a post-invoke hook. """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 This allows you to refer to one after invoke hook for several commands that

View File

@ -262,6 +262,7 @@ class HelpCommand(HelpCommandCommand, Generic[ContextT]):
) -> None: ) -> None:
self.show_hidden: bool = show_hidden self.show_hidden: bool = show_hidden
self.verify_checks: bool = verify_checks self.verify_checks: bool = verify_checks
self.command_attrs: Dict[str, Any]
self.command_attrs = attrs = command_attrs if command_attrs is not MISSING else {} self.command_attrs = attrs = command_attrs if command_attrs is not MISSING else {}
attrs.setdefault('name', 'help') attrs.setdefault('name', 'help')
attrs.setdefault('help', 'Shows this message') attrs.setdefault('help', 'Shows this message')