Change some methods to use positional-only marker

Co-authored-by: Danny <Rapptz@users.noreply.github.com>
This commit is contained in:
Bryan Forbes
2022-03-22 17:52:25 -05:00
committed by GitHub
parent c6d113c843
commit 062f4d6f87
5 changed files with 369 additions and 79 deletions

View File

@ -274,7 +274,7 @@ class HelpCommand(HelpCommandCommand, Generic[ContextT]):
if not hasattr(self.on_help_command_error, '__help_command_not_overridden__'):
self.on_error = self.on_help_command_error
async def __call__(self, context: ContextT, *args: Any, **kwargs: Any) -> Any:
async def __call__(self, context: ContextT, /, *args: Any, **kwargs: Any) -> Any:
return await self._set_context(context, *args, **kwargs)
async def _set_context(self, context: ContextT, *args: Any, **kwargs: Any) -> Any:
@ -306,13 +306,13 @@ class HelpCommand(HelpCommandCommand, Generic[ContextT]):
async def _parse_arguments(self, ctx: ContextT) -> None:
return await self._call_without_cog(super()._parse_arguments, ctx)
async def call_before_hooks(self, ctx: ContextT) -> None:
async def call_before_hooks(self, ctx: ContextT, /) -> None:
return await self._call_without_cog(super().call_before_hooks, ctx)
async def call_after_hooks(self, ctx: ContextT) -> None:
async def call_after_hooks(self, ctx: ContextT, /) -> None:
return await self._call_without_cog(super().call_after_hooks, ctx)
async def can_run(self, ctx: ContextT) -> bool:
async def can_run(self, ctx: ContextT, /) -> bool:
return await self._call_without_cog(super().can_run, ctx)
def get_bot_mapping(self) -> Dict[Optional[Cog], List[Command[Any, ..., Any]]]:
@ -343,9 +343,13 @@ class HelpCommand(HelpCommandCommand, Generic[ContextT]):
return command_name
return ctx.invoked_with
def get_command_signature(self, command: Command[Any, ..., Any]) -> str:
def get_command_signature(self, command: Command[Any, ..., Any], /) -> str:
"""Retrieves the signature portion of the help page.
.. versionchanged:: 2.0
``command`` parameter is now positional-only.
Parameters
------------
command: :class:`Command`
@ -378,11 +382,15 @@ class HelpCommand(HelpCommandCommand, Generic[ContextT]):
return f'{self.context.clean_prefix}{alias} {command.signature}'
def remove_mentions(self, string: str) -> str:
def remove_mentions(self, string: str, /) -> str:
"""Removes mentions from the string to prevent abuse.
This includes ``@everyone``, ``@here``, member mentions and role mentions.
.. versionchanged:: 2.0
``string`` parameter is now positional-only.
Returns
-------
:class:`str`
@ -450,7 +458,7 @@ class HelpCommand(HelpCommandCommand, Generic[ContextT]):
if cog is not None:
self._inject_into_cog(cog)
def command_not_found(self, string: str) -> str:
def command_not_found(self, string: str, /) -> str:
"""|maybecoro|
A method called when a command is not found in the help command.
@ -458,6 +466,10 @@ class HelpCommand(HelpCommandCommand, Generic[ContextT]):
Defaults to ``No command called {0} found.``
.. versionchanged:: 2.0
``string`` parameter is now positional-only.
Parameters
------------
string: :class:`str`
@ -471,7 +483,7 @@ class HelpCommand(HelpCommandCommand, Generic[ContextT]):
"""
return f'No command called "{string}" found.'
def subcommand_not_found(self, command: Command[Any, ..., Any], string: str) -> str:
def subcommand_not_found(self, command: Command[Any, ..., Any], string: str, /) -> str:
"""|maybecoro|
A method called when a command did not have a subcommand requested in the help command.
@ -484,6 +496,10 @@ class HelpCommand(HelpCommandCommand, Generic[ContextT]):
- ``'Command "{command.qualified_name}" has no subcommand named {string}'``
- If the ``command`` parameter has subcommands but not one named ``string``.
.. versionchanged:: 2.0
``command`` and ``string`` parameters are now positional-only.
Parameters
------------
command: :class:`Command`
@ -504,6 +520,7 @@ class HelpCommand(HelpCommandCommand, Generic[ContextT]):
async def filter_commands(
self,
commands: Iterable[Command[Any, ..., Any]],
/,
*,
sort: bool = False,
key: Optional[Callable[[Command[Any, ..., Any]], Any]] = None,
@ -515,6 +532,10 @@ class HelpCommand(HelpCommandCommand, Generic[ContextT]):
This takes into account the :attr:`verify_checks` and :attr:`show_hidden`
attributes.
.. versionchanged:: 2.0
``commands`` parameter is now positional-only.
Parameters
------------
commands: Iterable[:class:`Command`]
@ -563,9 +584,13 @@ class HelpCommand(HelpCommandCommand, Generic[ContextT]):
ret.sort(key=key)
return ret
def get_max_size(self, commands: Sequence[Command[Any, ..., Any]]) -> int:
def get_max_size(self, commands: Sequence[Command[Any, ..., Any]], /) -> int:
"""Returns the largest name length of the specified command list.
.. versionchanged:: 2.0
``commands`` parameter is now positional-only.
Parameters
------------
commands: Sequence[:class:`Command`]
@ -594,7 +619,7 @@ class HelpCommand(HelpCommandCommand, Generic[ContextT]):
"""
return self.context.channel
async def send_error_message(self, error: str) -> None:
async def send_error_message(self, error: str, /) -> None:
"""|coro|
Handles the implementation when an error happens in the help command.
@ -609,6 +634,10 @@ class HelpCommand(HelpCommandCommand, Generic[ContextT]):
You can access the invocation context with :attr:`HelpCommand.context`.
.. versionchanged:: 2.0
``error`` parameter is now positional-only.
Parameters
------------
error: :class:`str`
@ -619,7 +648,7 @@ class HelpCommand(HelpCommandCommand, Generic[ContextT]):
await destination.send(error)
@_not_overridden
async def on_help_command_error(self, ctx: ContextT, error: CommandError) -> None:
async def on_help_command_error(self, ctx: ContextT, error: CommandError, /) -> None:
"""|coro|
The help command's error handler, as specified by :ref:`ext_commands_error_handler`.
@ -630,6 +659,10 @@ class HelpCommand(HelpCommandCommand, Generic[ContextT]):
By default this method does nothing and just propagates to the default
error handlers.
.. versionchanged:: 2.0
``ctx`` and ``error`` parameters are now positional-only.
Parameters
------------
ctx: :class:`Context`
@ -639,7 +672,7 @@ class HelpCommand(HelpCommandCommand, Generic[ContextT]):
"""
pass
async def send_bot_help(self, mapping: Mapping[Optional[Cog], List[Command[Any, ..., Any]]]) -> None:
async def send_bot_help(self, mapping: Mapping[Optional[Cog], List[Command[Any, ..., Any]]], /) -> None:
"""|coro|
Handles the implementation of the bot command page in the help command.
@ -659,6 +692,10 @@ class HelpCommand(HelpCommandCommand, Generic[ContextT]):
Also, the commands in the mapping are not filtered. To do the filtering
you will have to call :meth:`filter_commands` yourself.
.. versionchanged:: 2.0
``mapping`` parameter is now positional-only.
Parameters
------------
mapping: Mapping[Optional[:class:`Cog`], List[:class:`Command`]]
@ -668,7 +705,7 @@ class HelpCommand(HelpCommandCommand, Generic[ContextT]):
"""
return None
async def send_cog_help(self, cog: Cog) -> None:
async def send_cog_help(self, cog: Cog, /) -> None:
"""|coro|
Handles the implementation of the cog page in the help command.
@ -689,6 +726,10 @@ class HelpCommand(HelpCommandCommand, Generic[ContextT]):
The commands returned not filtered. To do the filtering you will have to call
:meth:`filter_commands` yourself.
.. versionchanged:: 2.0
``cog`` parameter is now positional-only.
Parameters
-----------
cog: :class:`Cog`
@ -696,7 +737,7 @@ class HelpCommand(HelpCommandCommand, Generic[ContextT]):
"""
return None
async def send_group_help(self, group: Group[Any, ..., Any]) -> None:
async def send_group_help(self, group: Group[Any, ..., Any], /) -> None:
"""|coro|
Handles the implementation of the group page in the help command.
@ -717,6 +758,10 @@ class HelpCommand(HelpCommandCommand, Generic[ContextT]):
:attr:`Group.commands`. The commands returned not filtered. To do the
filtering you will have to call :meth:`filter_commands` yourself.
.. versionchanged:: 2.0
``group`` parameter is now positional-only.
Parameters
-----------
group: :class:`Group`
@ -724,7 +769,7 @@ class HelpCommand(HelpCommandCommand, Generic[ContextT]):
"""
return None
async def send_command_help(self, command: Command[Any, ..., Any]) -> None:
async def send_command_help(self, command: Command[Any, ..., Any], /) -> None:
"""|coro|
Handles the implementation of the single command page in the help command.
@ -755,6 +800,10 @@ class HelpCommand(HelpCommandCommand, Generic[ContextT]):
There are more than just these attributes but feel free to play around with
these to help you get started to get the output that you want.
.. versionchanged:: 2.0
``command`` parameter is now positional-only.
Parameters
-----------
command: :class:`Command`
@ -762,7 +811,7 @@ class HelpCommand(HelpCommandCommand, Generic[ContextT]):
"""
return None
async def prepare_help_command(self, ctx: ContextT, command: Optional[str] = None) -> None:
async def prepare_help_command(self, ctx: ContextT, command: Optional[str] = None, /) -> None:
"""|coro|
A low level method that can be used to prepare the help command
@ -777,6 +826,10 @@ class HelpCommand(HelpCommandCommand, Generic[ContextT]):
This is called *inside* the help command callback body. So all
the usual rules that happen inside apply here as well.
.. versionchanged:: 2.0
``ctx`` and ``command`` parameters are now positional-only.
Parameters
-----------
ctx: :class:`Context`
@ -786,7 +839,7 @@ class HelpCommand(HelpCommandCommand, Generic[ContextT]):
"""
pass
async def command_callback(self, ctx: ContextT, *, command: Optional[str] = None) -> Any:
async def command_callback(self, ctx: ContextT, /, *, command: Optional[str] = None) -> Any:
"""|coro|
The actual implementation of the help command.
@ -804,6 +857,10 @@ class HelpCommand(HelpCommandCommand, Generic[ContextT]):
- :meth:`send_error_message`
- :meth:`on_help_command_error`
- :meth:`prepare_help_command`
.. versionchanged:: 2.0
``ctx`` parameter is now positional-only.
"""
await self.prepare_help_command(ctx, command)
@ -899,8 +956,13 @@ class DefaultHelpCommand(HelpCommand[ContextT]):
super().__init__(**options)
def shorten_text(self, text: str) -> str:
""":class:`str`: Shortens text to fit into the :attr:`width`."""
def shorten_text(self, text: str, /) -> str:
""":class:`str`: Shortens text to fit into the :attr:`width`.
.. versionchanged:: 2.0
``text`` parameter is now positional-only.
"""
if len(text) > self.width:
return text[: self.width - 3].rstrip() + '...'
return text
@ -914,7 +976,7 @@ class DefaultHelpCommand(HelpCommand[ContextT]):
)
def add_indented_commands(
self, commands: Sequence[Command[Any, ..., Any]], *, heading: str, max_size: Optional[int] = None
self, commands: Sequence[Command[Any, ..., Any]], /, *, heading: str, max_size: Optional[int] = None
) -> None:
"""Indents a list of commands after the specified heading.
@ -925,6 +987,10 @@ class DefaultHelpCommand(HelpCommand[ContextT]):
the command's :attr:`Command.short_doc` and then shortened
to fit into the :attr:`width`.
.. versionchanged:: 2.0
``commands`` parameter is now positional-only.
Parameters
-----------
commands: Sequence[:class:`Command`]
@ -957,9 +1023,13 @@ class DefaultHelpCommand(HelpCommand[ContextT]):
for page in self.paginator.pages:
await destination.send(page)
def add_command_formatting(self, command: Command[Any, ..., Any]) -> None:
def add_command_formatting(self, command: Command[Any, ..., Any], /) -> None:
"""A utility function to format the non-indented block of commands and groups.
.. versionchanged:: 2.0
``command`` parameter is now positional-only.
Parameters
------------
command: :class:`Command`
@ -989,11 +1059,11 @@ class DefaultHelpCommand(HelpCommand[ContextT]):
else:
return ctx.channel
async def prepare_help_command(self, ctx: ContextT, command: str) -> None:
async def prepare_help_command(self, ctx: ContextT, command: Optional[str] = None, /) -> None:
self.paginator.clear()
await super().prepare_help_command(ctx, command)
async def send_bot_help(self, mapping: Mapping[Optional[Cog], List[Command[Any, ..., Any]]]) -> None:
async def send_bot_help(self, mapping: Mapping[Optional[Cog], List[Command[Any, ..., Any]]], /) -> None:
ctx = self.context
bot = ctx.bot
@ -1023,12 +1093,12 @@ class DefaultHelpCommand(HelpCommand[ContextT]):
await self.send_pages()
async def send_command_help(self, command: Command[Any, ..., Any]) -> None:
async def send_command_help(self, command: Command[Any, ..., Any], /) -> None:
self.add_command_formatting(command)
self.paginator.close_page()
await self.send_pages()
async def send_group_help(self, group: Group[Any, ..., Any]) -> None:
async def send_group_help(self, group: Group[Any, ..., Any], /) -> None:
self.add_command_formatting(group)
filtered = await self.filter_commands(group.commands, sort=self.sort_commands)
@ -1042,7 +1112,7 @@ class DefaultHelpCommand(HelpCommand[ContextT]):
await self.send_pages()
async def send_cog_help(self, cog: Cog) -> None:
async def send_cog_help(self, cog: Cog, /) -> None:
if cog.description:
self.paginator.add_line(cog.description, empty=True)
@ -1128,7 +1198,7 @@ class MinimalHelpCommand(HelpCommand[ContextT]):
f"You can also use `{self.context.clean_prefix}{command_name} [category]` for more info on a category."
)
def get_command_signature(self, command: Command[Any, ..., Any]) -> str:
def get_command_signature(self, command: Command[Any, ..., Any], /) -> str:
return f'{self.context.clean_prefix}{command.qualified_name} {command.signature}'
def get_ending_note(self) -> str:
@ -1143,7 +1213,7 @@ class MinimalHelpCommand(HelpCommand[ContextT]):
"""
return ''
def add_bot_commands_formatting(self, commands: Sequence[Command[Any, ..., Any]], heading: str) -> None:
def add_bot_commands_formatting(self, commands: Sequence[Command[Any, ..., Any]], heading: str, /) -> None:
"""Adds the minified bot heading with commands to the output.
The formatting should be added to the :attr:`paginator`.
@ -1151,6 +1221,10 @@ class MinimalHelpCommand(HelpCommand[ContextT]):
The default implementation is a bold underline heading followed
by commands separated by an EN SPACE (U+2002) in the next line.
.. versionchanged:: 2.0
``commands`` and ``heading`` parameters are now positional-only.
Parameters
-----------
commands: Sequence[:class:`Command`]
@ -1164,7 +1238,7 @@ class MinimalHelpCommand(HelpCommand[ContextT]):
self.paginator.add_line(f'__**{heading}**__')
self.paginator.add_line(joined)
def add_subcommand_formatting(self, command: Command[Any, ..., Any]) -> None:
def add_subcommand_formatting(self, command: Command[Any, ..., Any], /) -> None:
"""Adds formatting information on a subcommand.
The formatting should be added to the :attr:`paginator`.
@ -1172,6 +1246,10 @@ class MinimalHelpCommand(HelpCommand[ContextT]):
The default implementation is the prefix and the :attr:`Command.qualified_name`
optionally followed by an En dash and the command's :attr:`Command.short_doc`.
.. versionchanged:: 2.0
``command`` parameter is now positional-only.
Parameters
-----------
command: :class:`Command`
@ -1180,7 +1258,7 @@ class MinimalHelpCommand(HelpCommand[ContextT]):
fmt = '{0}{1} \N{EN DASH} {2}' if command.short_doc else '{0}{1}'
self.paginator.add_line(fmt.format(self.context.clean_prefix, command.qualified_name, command.short_doc))
def add_aliases_formatting(self, aliases: Sequence[str]) -> None:
def add_aliases_formatting(self, aliases: Sequence[str], /) -> None:
"""Adds the formatting information on a command's aliases.
The formatting should be added to the :attr:`paginator`.
@ -1190,6 +1268,10 @@ class MinimalHelpCommand(HelpCommand[ContextT]):
This is not called if there are no aliases to format.
.. versionchanged:: 2.0
``aliases`` parameter is now positional-only.
Parameters
-----------
aliases: Sequence[:class:`str`]
@ -1197,9 +1279,13 @@ class MinimalHelpCommand(HelpCommand[ContextT]):
"""
self.paginator.add_line(f'**{self.aliases_heading}** {", ".join(aliases)}', empty=True)
def add_command_formatting(self, command: Command[Any, ..., Any]) -> None:
def add_command_formatting(self, command: Command[Any, ..., Any], /) -> None:
"""A utility function to format commands and groups.
.. versionchanged:: 2.0
``command`` parameter is now positional-only.
Parameters
------------
command: :class:`Command`
@ -1233,11 +1319,11 @@ class MinimalHelpCommand(HelpCommand[ContextT]):
else:
return ctx.channel
async def prepare_help_command(self, ctx: ContextT, command: str) -> None:
async def prepare_help_command(self, ctx: ContextT, command: Optional[str] = None, /) -> None:
self.paginator.clear()
await super().prepare_help_command(ctx, command)
async def send_bot_help(self, mapping: Mapping[Optional[Cog], List[Command[Any, ..., Any]]]) -> None:
async def send_bot_help(self, mapping: Mapping[Optional[Cog], List[Command[Any, ..., Any]]], /) -> None:
ctx = self.context
bot = ctx.bot
@ -1268,7 +1354,7 @@ class MinimalHelpCommand(HelpCommand[ContextT]):
await self.send_pages()
async def send_cog_help(self, cog: Cog) -> None:
async def send_cog_help(self, cog: Cog, /) -> None:
bot = self.context.bot
if bot.description:
self.paginator.add_line(bot.description, empty=True)
@ -1293,7 +1379,7 @@ class MinimalHelpCommand(HelpCommand[ContextT]):
await self.send_pages()
async def send_group_help(self, group: Group[Any, ..., Any]) -> None:
async def send_group_help(self, group: Group[Any, ..., Any], /) -> None:
self.add_command_formatting(group)
filtered = await self.filter_commands(group.commands, sort=self.sort_commands)
@ -1313,7 +1399,7 @@ class MinimalHelpCommand(HelpCommand[ContextT]):
await self.send_pages()
async def send_command_help(self, command: Command[Any, ..., Any]) -> None:
async def send_command_help(self, command: Command[Any, ..., Any], /) -> None:
self.add_command_formatting(command)
self.paginator.close_page()
await self.send_pages()