From d12d41c71abeb7af3e3bcf8375c24857110b62f1 Mon Sep 17 00:00:00 2001 From: Lumouille <144063653+Lumabots@users.noreply.github.com> Date: Fri, 16 May 2025 13:01:20 +0300 Subject: [PATCH 1/4] fix: add support for Literal type in typing annotations for Discord choices --- discord/commands/core.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/discord/commands/core.py b/discord/commands/core.py index bee43341fd..386266b38f 100644 --- a/discord/commands/core.py +++ b/discord/commands/core.py @@ -73,9 +73,9 @@ from .options import Option, OptionChoice if sys.version_info >= (3, 11): - from typing import Annotated, get_args, get_origin + from typing import Annotated, Literal, get_args, get_origin else: - from typing_extensions import Annotated, get_args, get_origin + from typing_extensions import Annotated, Literal, get_args, get_origin __all__ = ( "_BaseCommand", @@ -805,6 +805,18 @@ def _parse_options(self, params, *, check_params: bool = True) -> list[Option]: option = p_obj.annotation if option == inspect.Parameter.empty: option = str + if get_origin(option) is Literal: + literal_values = get_args(option) + if not all(isinstance(v, (str, int, float)) for v in literal_values): + raise TypeError( + "Literal values must be str, int, or float for Discord choices." + ) + option = Option( + str, + choices=[ + OptionChoice(name=str(v), value=str(v)) for v in literal_values + ], + ) if self._is_typing_annotated(option): type_hint = get_args(option)[0] From 8df1da72146201d959f7d867f8e1018e659c1fe0 Mon Sep 17 00:00:00 2001 From: Lumouille <144063653+Lumabots@users.noreply.github.com> Date: Fri, 16 May 2025 13:05:16 +0300 Subject: [PATCH 2/4] Update CHANGELOG.md --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 85d14e3309..94c157b189 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -53,6 +53,8 @@ These changes are available on the `master` branch, but have not yet been releas ([#2714](https://github.com/Pycord-Development/pycord/pull/2714)) - Added the ability to pass a `datetime.time` object to `format_dt` ([#2747](https://github.com/Pycord-Development/pycord/pull/2747)) +- Added support for Literal[...] to define command choices. + ([#2781](https://github.com/Pycord-Development/pycord/pull/2781)) ### Fixed From 1a890428b4bfb11bcb323076255f1c58326dbd1a Mon Sep 17 00:00:00 2001 From: Lumouille <144063653+Lumabots@users.noreply.github.com> Date: Fri, 16 May 2025 13:16:41 +0300 Subject: [PATCH 3/4] Update CHANGELOG.md Signed-off-by: Lumouille <144063653+Lumabots@users.noreply.github.com> --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9ee992da65..dfe8709b2c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -54,7 +54,7 @@ These changes are available on the `master` branch, but have not yet been releas - Added the ability to pass a `datetime.time` object to `format_dt` ([#2747](https://github.com/Pycord-Development/pycord/pull/2747)) - Added support for Literal[...] to define command choices. - ([#2781](https://github.com/Pycord-Development/pycord/pull/2781)) + ([#2782](https://github.com/Pycord-Development/pycord/pull/2782)) ### Fixed From fceba5fff5b4d80bf1ef3baaed5b1e3a19d54e72 Mon Sep 17 00:00:00 2001 From: Lumouille <144063653+Lumabots@users.noreply.github.com> Date: Fri, 23 May 2025 09:07:15 +0300 Subject: [PATCH 4/4] Update CHANGELOG.md Co-authored-by: JustaSqu1d <89910983+JustaSqu1d@users.noreply.github.com> Signed-off-by: Lumouille <144063653+Lumabots@users.noreply.github.com> --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e88602782c..bc82ef3cb5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -55,7 +55,7 @@ These changes are available on the `master` branch, but have not yet been releas ([#2714](https://github.com/Pycord-Development/pycord/pull/2714)) - Added the ability to pass a `datetime.time` object to `format_dt` ([#2747](https://github.com/Pycord-Development/pycord/pull/2747)) -- Added support for Literal[...] to define command choices. +- Added support for `Literal[...]` to define command choices. ([#2782](https://github.com/Pycord-Development/pycord/pull/2782)) ### Fixed