From 27dd9868583d6c7f455104c1c462b77da504550b Mon Sep 17 00:00:00 2001 From: Rapptz Date: Tue, 15 Mar 2022 07:09:41 -0400 Subject: [PATCH] Forbid Choice annotations being used with autocomplete parameters Since the Choice are synthetic, the library doesn't have a pre-populated list of choices to know what to use as the resulting value. --- discord/app_commands/commands.py | 10 ++++++++++ discord/app_commands/transformers.py | 3 +++ 2 files changed, 13 insertions(+) diff --git a/discord/app_commands/commands.py b/discord/app_commands/commands.py index 68abfce38..0508fb4a1 100644 --- a/discord/app_commands/commands.py +++ b/discord/app_commands/commands.py @@ -246,6 +246,11 @@ def _populate_autocomplete(params: Dict[str, CommandParameter], autocomplete: Di if param.type not in (AppCommandOptionType.string, AppCommandOptionType.number, AppCommandOptionType.integer): raise TypeError('autocomplete is only supported for integer, string, or number option types') + if param.is_choice_annotation(): + raise TypeError( + 'Choice annotation unsupported for autocomplete parameters, consider using a regular annotation instead' + ) + param.autocomplete = _validate_auto_complete_callback(callback) if autocomplete: @@ -589,6 +594,11 @@ class Command(Generic[GroupT, P, T]): if param.type not in (AppCommandOptionType.string, AppCommandOptionType.number, AppCommandOptionType.integer): raise TypeError('autocomplete is only supported for integer, string, or number option types') + if param.is_choice_annotation(): + raise TypeError( + 'Choice annotation unsupported for autocomplete parameters, consider using a regular annotation instead' + ) + param.autocomplete = _validate_auto_complete_callback(coro) return coro diff --git a/discord/app_commands/transformers.py b/discord/app_commands/transformers.py index 842e74bce..d10754092 100644 --- a/discord/app_commands/transformers.py +++ b/discord/app_commands/transformers.py @@ -124,6 +124,9 @@ class CommandParameter: return base + def is_choice_annotation(self) -> bool: + return getattr(self._annotation, '__discord_app_commands_is_choice__', False) + async def transform(self, interaction: Interaction, value: Any) -> Any: if hasattr(self._annotation, '__discord_app_commands_transformer__'): # This one needs special handling for type safety reasons