aboutsummaryrefslogtreecommitdiff
path: root/docs
diff options
context:
space:
mode:
authorSigmath Bits <[email protected]>2021-04-10 18:50:59 +1200
committerGitHub <[email protected]>2021-04-10 02:50:59 -0400
commit68aef92b377f61ed465660646659d4ba0100c314 (patch)
treec5e2bfd811c9ceac60ed5ce9422012f94e482f25 /docs
parentmake examples on_ready consistent (diff)
downloaddiscord.py-68aef92b377f61ed465660646659d4ba0100c314.tar.xz
discord.py-68aef92b377f61ed465660646659d4ba0100c314.zip
[commands]Add typing.Literal converter
Diffstat (limited to 'docs')
-rw-r--r--docs/ext/commands/commands.rst21
1 files changed, 21 insertions, 0 deletions
diff --git a/docs/ext/commands/commands.rst b/docs/ext/commands/commands.rst
index 3c572536..837e0044 100644
--- a/docs/ext/commands/commands.rst
+++ b/docs/ext/commands/commands.rst
@@ -502,6 +502,27 @@ resumes handling, which in this case would be to pass it into the ``liquid`` par
This converter only works in regular positional parameters, not variable parameters or keyword-only parameters.
+typing.Literal
+^^^^^^^^^^^^^^^^
+
+A :data:`typing.Literal` is a special type hint that requires the passed parameter to be equal to one of the listed values
+after being converted to the same type. For example, given the following:
+
+.. code-block:: python3
+
+ from typing import Literal
+
+ @bot.command()
+ async def shop(ctx, buy_sell: Literal['buy', 'sell'], amount: Literal[1, 2], *, item: str):
+ await ctx.send(f'{buy_sell.capitalize()}ing {amount} {item}(s)!')
+
+
+The ``buy_sell`` parameter must be either the literal string ``"buy"`` or ``"sell"`` and ``amount`` must convert to the
+``int`` ``1`` or ``2``. If ``buy_sell`` or ``amount`` don't match any value, then a special error is raised,
+:exc:`~.ext.commands.BadLiteralArgument`. Any literal values can be mixed and matched within the same :data:`typing.Literal` converter.
+
+Note that ``typing.Literal[True]`` and ``typing.Literal[False]`` still follow the :class:`bool` converter rules.
+
Greedy
^^^^^^^^