Fix typing issues and improve typing completeness across the library

Co-authored-by: Danny <Rapptz@users.noreply.github.com>
Co-authored-by: Josh <josh.ja.butt@gmail.com>
This commit is contained in:
Stocker
2022-03-13 23:52:10 -04:00
committed by GitHub
parent 603681940f
commit 5aa696ccfa
66 changed files with 1071 additions and 802 deletions

View File

@@ -26,7 +26,22 @@ from __future__ import annotations
import inspect
import sys
import traceback
from typing import Any, Callable, Dict, Generic, List, Literal, Optional, TYPE_CHECKING, Set, Tuple, TypeVar, Union, overload
from typing import (
Any,
TYPE_CHECKING,
Callable,
Dict,
Generic,
List,
Literal,
Optional,
Set,
Tuple,
TypeVar,
Union,
overload,
)
from collections import Counter
@@ -194,13 +209,13 @@ class CommandTree(Generic[ClientT]):
def add_command(
self,
command: Union[Command, ContextMenu, Group],
command: Union[Command[Any, ..., Any], ContextMenu, Group],
/,
*,
guild: Optional[Snowflake] = MISSING,
guilds: List[Snowflake] = MISSING,
override: bool = False,
):
) -> None:
"""Adds an application command to the tree.
This only adds the command locally -- in order to sync the commands
@@ -317,7 +332,7 @@ class CommandTree(Generic[ClientT]):
*,
guild: Optional[Snowflake] = ...,
type: Literal[AppCommandType.chat_input] = ...,
) -> Optional[Union[Command, Group]]:
) -> Optional[Union[Command[Any, ..., Any], Group]]:
...
@overload
@@ -328,7 +343,7 @@ class CommandTree(Generic[ClientT]):
*,
guild: Optional[Snowflake] = ...,
type: AppCommandType = ...,
) -> Optional[Union[Command, ContextMenu, Group]]:
) -> Optional[Union[Command[Any, ..., Any], ContextMenu, Group]]:
...
def remove_command(
@@ -338,7 +353,7 @@ class CommandTree(Generic[ClientT]):
*,
guild: Optional[Snowflake] = None,
type: AppCommandType = AppCommandType.chat_input,
) -> Optional[Union[Command, ContextMenu, Group]]:
) -> Optional[Union[Command[Any, ..., Any], ContextMenu, Group]]:
"""Removes an application command from the tree.
This only removes the command locally -- in order to sync the commands
@@ -396,7 +411,7 @@ class CommandTree(Generic[ClientT]):
*,
guild: Optional[Snowflake] = ...,
type: Literal[AppCommandType.chat_input] = ...,
) -> Optional[Union[Command, Group]]:
) -> Optional[Union[Command[Any, ..., Any], Group]]:
...
@overload
@@ -407,7 +422,7 @@ class CommandTree(Generic[ClientT]):
*,
guild: Optional[Snowflake] = ...,
type: AppCommandType = ...,
) -> Optional[Union[Command, ContextMenu, Group]]:
) -> Optional[Union[Command[Any, ..., Any], ContextMenu, Group]]:
...
def get_command(
@@ -417,7 +432,7 @@ class CommandTree(Generic[ClientT]):
*,
guild: Optional[Snowflake] = None,
type: AppCommandType = AppCommandType.chat_input,
) -> Optional[Union[Command, ContextMenu, Group]]:
) -> Optional[Union[Command[Any, ..., Any], ContextMenu, Group]]:
"""Gets a application command from the tree.
Parameters
@@ -468,7 +483,7 @@ class CommandTree(Generic[ClientT]):
*,
guild: Optional[Snowflake] = ...,
type: Literal[AppCommandType.chat_input] = ...,
) -> List[Union[Command, Group]]:
) -> List[Union[Command[Any, ..., Any], Group]]:
...
@overload
@@ -477,7 +492,7 @@ class CommandTree(Generic[ClientT]):
*,
guild: Optional[Snowflake] = ...,
type: AppCommandType = ...,
) -> Union[List[Union[Command, Group]], List[ContextMenu]]:
) -> Union[List[Union[Command[Any, ..., Any], Group]], List[ContextMenu]]:
...
def get_commands(
@@ -485,7 +500,7 @@ class CommandTree(Generic[ClientT]):
*,
guild: Optional[Snowflake] = None,
type: AppCommandType = AppCommandType.chat_input,
) -> Union[List[Union[Command, Group]], List[ContextMenu]]:
) -> Union[List[Union[Command[Any, ..., Any], Group]], List[ContextMenu]]:
"""Gets all application commands from the tree.
Parameters
@@ -518,9 +533,11 @@ class CommandTree(Generic[ClientT]):
value = type.value
return [command for ((_, g, t), command) in self._context_menus.items() if g == guild_id and t == value]
def _get_all_commands(self, *, guild: Optional[Snowflake] = None) -> List[Union[Command, Group, ContextMenu]]:
def _get_all_commands(
self, *, guild: Optional[Snowflake] = None
) -> List[Union[Command[Any, ..., Any], Group, ContextMenu]]:
if guild is None:
base: List[Union[Command, Group, ContextMenu]] = list(self._global_commands.values())
base: List[Union[Command[Any, ..., Any], Group, ContextMenu]] = list(self._global_commands.values())
base.extend(cmd for ((_, g, _), cmd) in self._context_menus.items() if g is None)
return base
else:
@@ -530,7 +547,7 @@ class CommandTree(Generic[ClientT]):
guild_id = guild.id
return [cmd for ((_, g, _), cmd) in self._context_menus.items() if g == guild_id]
else:
base: List[Union[Command, Group, ContextMenu]] = list(commands.values())
base: List[Union[Command[Any, ..., Any], Group, ContextMenu]] = list(commands.values())
guild_id = guild.id
base.extend(cmd for ((_, g, _), cmd) in self._context_menus.items() if g == guild_id)
return base
@@ -564,7 +581,7 @@ class CommandTree(Generic[ClientT]):
async def on_error(
self,
interaction: Interaction,
command: Optional[Union[ContextMenu, Command]],
command: Optional[Union[ContextMenu, Command[Any, ..., Any]]],
error: AppCommandError,
) -> None:
"""|coro|
@@ -742,7 +759,7 @@ class CommandTree(Generic[ClientT]):
self.client.loop.create_task(wrapper(), name='CommandTree-invoker')
async def _call_context_menu(self, interaction: Interaction, data: ApplicationCommandInteractionData, type: int):
async def _call_context_menu(self, interaction: Interaction, data: ApplicationCommandInteractionData, type: int) -> None:
name = data['name']
guild_id = _get_as_snowflake(data, 'guild_id')
ctx_menu = self._context_menus.get((name, guild_id, type))
@@ -770,7 +787,7 @@ class CommandTree(Generic[ClientT]):
except AppCommandError as e:
await self.on_error(interaction, ctx_menu, e)
async def call(self, interaction: Interaction):
async def call(self, interaction: Interaction) -> None:
"""|coro|
Given an :class:`~discord.Interaction`, calls the matching