Skip to content

Commit

Permalink
chore: resolve lint and typing issues
Browse files Browse the repository at this point in the history
  • Loading branch information
onerandomusername committed Oct 21, 2022
1 parent 9695975 commit 7eda7e4
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 89 deletions.
8 changes: 4 additions & 4 deletions disnake/app_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ def __init__(
name: LocalizedRequired,
dm_permission: Optional[bool] = None,
default_member_permissions: Optional[Union[Permissions, int]] = None,
id: int = None,
id: Optional[int] = None,
):
self.type: ApplicationCommandType = enum_if_int(ApplicationCommandType, type)

Expand Down Expand Up @@ -605,7 +605,7 @@ def __init__(
name: LocalizedRequired,
dm_permission: Optional[bool] = None,
default_member_permissions: Optional[Union[Permissions, int]] = None,
id: int = None,
id: Optional[int] = None,
):
super().__init__(
type=ApplicationCommandType.user,
Expand Down Expand Up @@ -693,7 +693,7 @@ def __init__(
name: LocalizedRequired,
dm_permission: Optional[bool] = None,
default_member_permissions: Optional[Union[Permissions, int]] = None,
id: int = None,
id: Optional[int] = None,
):
super().__init__(
type=ApplicationCommandType.message,
Expand Down Expand Up @@ -799,7 +799,7 @@ def __init__(
options: Optional[List[Option]] = None,
dm_permission: Optional[bool] = None,
default_member_permissions: Optional[Union[Permissions, int]] = None,
id: int = None,
id: Optional[int] = None,
):
super().__init__(
type=ApplicationCommandType.chat_input,
Expand Down
4 changes: 3 additions & 1 deletion disnake/ext/commands/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -1016,7 +1016,9 @@ class CommandRegistrationError(ClientException):
Whether the name that conflicts is an alias of the command we try to add.
"""

def __init__(self, name: str, *, alias_conflict: bool = False, guild_id: int = None) -> None:
def __init__(
self, name: str, *, alias_conflict: bool = False, guild_id: Optional[int] = None
) -> None:
self.name: str = name
self.alias_conflict: bool = alias_conflict
self.guild_id: Optional[int] = guild_id
Expand Down
29 changes: 3 additions & 26 deletions disnake/ext/commands/flags.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,17 @@
"""
The MIT License (MIT)
Copyright (c) 2015-2021 Rapptz
Copyright (c) 2021-present Disnake Development
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
"""
# SPDX-License-Identifier: MIT

from __future__ import annotations

from typing import TYPE_CHECKING

from disnake.flags import BaseFlags, alias_flag_value, all_flags_value, fill_with_flags, flag_value
from disnake.flags import BaseFlags, alias_flag_value, all_flags_value, flag_value

if TYPE_CHECKING:
from typing_extensions import Self

__all__ = ("ApplicationCommandSyncFlags",)


@fill_with_flags()
class ApplicationCommandSyncFlags(BaseFlags):
"""Controls the library's application command syncing policy.
Expand Down
38 changes: 24 additions & 14 deletions disnake/ext/commands/interaction_bot_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class InteractionBotBase(CommonBotBase):
def __init__(
self,
*,
command_sync: ApplicationCommandSyncFlags = None,
command_sync: Optional[ApplicationCommandSyncFlags] = None,
test_guilds: Optional[Sequence[int]] = None,
**options: Any,
):
Expand Down Expand Up @@ -178,9 +178,7 @@ def __init__(
self._schedule_app_command_preparation()

def application_commands_iterator(self) -> Iterable[InvokableApplicationCommand]:
return chain(
self._all_app_commands.values(),
)
yield from self._all_app_commands.values()

@property
def all_app_commands(self) -> List[InvokableApplicationCommand]:
Expand Down Expand Up @@ -371,7 +369,7 @@ def add_message_command(self, message_command: InvokableMessageCommand) -> None:
] = message_command

def remove_slash_command(
self, name: str, *, guild_id: int = None
self, name: str, *, guild_id: Optional[int] = None
) -> Optional[InvokableSlashCommand]:
"""Removes an :class:`InvokableSlashCommand` from the internal list
of slash commands.
Expand Down Expand Up @@ -400,7 +398,7 @@ def remove_slash_command(
return command

def remove_user_command(
self, name: str, *, guild_id: int = None
self, name: str, *, guild_id: Optional[int] = None
) -> Optional[InvokableUserCommand]:
"""Removes an :class:`InvokableUserCommand` from the internal list
of user commands.
Expand All @@ -422,7 +420,7 @@ def remove_user_command(
return command

def remove_message_command(
self, name: str, *, guild_id: int = None
self, name: str, *, guild_id: Optional[int] = None
) -> Optional[InvokableMessageCommand]:
"""Removes an :class:`InvokableMessageCommand` from the internal list
of message commands.
Expand All @@ -445,24 +443,36 @@ def remove_message_command(

@overload
def get_app_command(
self, name: str, type: Literal[ApplicationCommandType.chat_input], *, guild_id: int = None
self,
name: str,
type: Literal[ApplicationCommandType.chat_input],
*,
guild_id: Optional[int] = None,
) -> Optional[InvokableSlashCommand]:
...

@overload
def get_app_command(
self, name: str, type: Literal[ApplicationCommandType.message], *, guild_id: int = None
self,
name: str,
type: Literal[ApplicationCommandType.message],
*,
guild_id: Optional[int] = None,
) -> Optional[InvokableMessageCommand]:
...

@overload
def get_app_command(
self, name: str, type: Literal[ApplicationCommandType.user], *, guild_id: int = None
self,
name: str,
type: Literal[ApplicationCommandType.user],
*,
guild_id: Optional[int] = None,
) -> Optional[InvokableUserCommand]:
...

def get_app_command(
self, name: str, type: ApplicationCommandType, *, guild_id: int = None
self, name: str, type: ApplicationCommandType, *, guild_id: Optional[int] = None
) -> Optional[InvokableApplicationCommand]:
# this does not get commands by ID, use (some other method) to do that
if not isinstance(name, str):
Expand All @@ -473,7 +483,7 @@ def get_app_command(
return command

def get_slash_command(
self, name: str, *, guild_id: int = None
self, name: str, *, guild_id: Optional[int] = None
) -> Optional[Union[InvokableSlashCommand, SubCommandGroup, SubCommand]]:
"""Works like ``Bot.get_command``, but for slash commands.
Expand Down Expand Up @@ -515,7 +525,7 @@ def get_slash_command(
return group.children.get(chain[2])

def get_user_command(
self, name: str, *, guild_id: int = None
self, name: str, *, guild_id: Optional[int] = None
) -> Optional[InvokableUserCommand]:
"""Gets an :class:`InvokableUserCommand` from the internal list
of user commands.
Expand All @@ -533,7 +543,7 @@ def get_user_command(
return self.get_app_command(name, ApplicationCommandType.user, guild_id=guild_id)

def get_message_command(
self, name: str, *, guild_id: int = None
self, name: str, *, guild_id: Optional[int] = None
) -> Optional[InvokableMessageCommand]:
"""Gets an :class:`InvokableMessageCommand` from the internal list
of message commands.
Expand Down
62 changes: 19 additions & 43 deletions disnake/ext/commands/slash_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,11 @@ def root_parent(self) -> InvokableSlashCommand:

@property
def parents(self) -> Tuple[InvokableSlashCommand]:
"""Tuple[:class:`InvokableSlashCommand`]: Returns all parents of this group.
"""Tuple[:class:`InvokableSlashCommand`]: Retrieves the parents of this command.
If the command has no parents then it returns an empty :class:`tuple`.
For example in commands ``/a b test``, the parents are ``(b, a)``.
.. versionadded:: 2.6
"""
Expand All @@ -195,25 +199,10 @@ def mention(self) -> str:
# todo: add docs and make ID non-nullable
return f"</{self.qualified_name}:{self.id}>"

# todo: refactor this class to make this not optional
@property
def parent(self) -> Optional[InvokableSlashCommand]:
def parent(self) -> InvokableSlashCommand:
return self._parent

@property
def parents(
self,
) -> Tuple[InvokableSlashCommand]:
"""Tuple[:class:`InvokableSlashCommand`]: Retrieves the parents of this command.
If the command has no parents then it returns an empty :class:`tuple`.
For example in commands ``/a b test``, the parents are ``(b, a)``.
.. versionadded:: 2.6
"""
return (self.parent,) # type: ignore

def sub_command(
self,
name: LocalizedOptional = None,
Expand Down Expand Up @@ -344,7 +333,7 @@ def root_parent(self) -> InvokableSlashCommand:
.. versionadded:: 2.6
"""
return self.parent.parent if isinstance(self.parent, SubCommandGroup) else self.parent
return self._parent._parent if isinstance(self._parent, SubCommandGroup) else self._parent

@property
def parents(
Expand All @@ -357,10 +346,10 @@ def parents(
.. versionadded:: 2.6
"""
# here I'm not using 'self.parent.parents + (self.parent,)' because it causes typing issues
if isinstance(self.parent, SubCommandGroup):
return (self.parent, self.parent.parent)
return (self.parent,)
# here I'm not using 'self._parent._parents + (self._parent,)' because it causes typing issues
if isinstance(self._parent, SubCommandGroup):
return (self._parent, self._parent._parent)
return (self._parent,)

@property
def description(self) -> str:
Expand All @@ -382,29 +371,9 @@ def mention(self) -> str:
return f"</{self.qualified_name}:{self.id}>"

@property
def parent(self) -> Optional[Union[InvokableSlashCommand, SubCommandGroup]]:
def parent(self) -> Union[InvokableSlashCommand, SubCommandGroup]:
return self._parent

@property
def parents(
self,
) -> Union[Tuple[InvokableSlashCommand], Tuple[SubCommandGroup, InvokableSlashCommand]]:
"""Union[Tuple[:class:`InvokableSlashCommand`], Tuple[:class:`SubCommandGroup`, :class:`InvokableSlashCommand`]]: Retrieves the parents of this command.
If the command has no parents then it returns an empty :class:`tuple`.
For example in commands ``/a b test``, the parents are ``(b, a)``.
.. versionadded:: 2.6
"""
entries = []
command = self
while command.parent is not None: # type: ignore
command = command.parent # type: ignore
entries.append(command)

return tuple(entries)

async def _call_autocompleter(
self, param: str, inter: ApplicationCommandInteraction, user_input: str
) -> Optional[Choices]:
Expand Down Expand Up @@ -560,6 +529,13 @@ def parents(self) -> Tuple[()]:
"""
return ()

@property
def parent(self) -> None:
"""``None``: This is for consistency with :class:`SubCommand` and :class:`SubCommandGroup`.
.. versionadded:: 2.6
"""

def _ensure_assignment_on_copy(self, other: SlashCommandT) -> SlashCommandT:
super()._ensure_assignment_on_copy(other)
if self.connectors != other.connectors:
Expand Down
2 changes: 1 addition & 1 deletion test_bot/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def __init__(self):
command_prefix=Config.prefix,
intents=disnake.Intents.all(),
help_command=None, # type: ignore
sync_commands_debug=Config.sync_commands_debug,
command_sync=commands.ApplicationCommandSyncFlags.all(),
strict_localization=Config.strict_localization,
test_guilds=Config.test_guilds,
reload=Config.auto_reload,
Expand Down

0 comments on commit 7eda7e4

Please sign in to comment.