Skip to content
This repository was archived by the owner on Aug 28, 2019. It is now read-only.

Commit 30be814

Browse files
committed
Ensure all choices are the same type as the parameter type
Fixes Rapptz#7625
1 parent 3775618 commit 30be814

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

discord/app_commands/commands.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,9 @@ def _populate_choices(params: Dict[str, CommandParameter], all_choices: Dict[str
223223
if param.type not in (AppCommandOptionType.string, AppCommandOptionType.number, AppCommandOptionType.integer):
224224
raise TypeError('choices are only supported for integer, string, or number option types')
225225

226-
# There's a type safety hole if someone does Choice[float] as an annotation
227-
# but the values are actually Choice[int]. Since the input-output is the same this feels
228-
# safe enough to ignore.
226+
if not all(param.type == choice._option_type for choice in choices):
227+
raise TypeError('choices must all have the same inner option type as the parameter choice type')
228+
229229
param.choices = choices
230230

231231
if all_choices:

discord/app_commands/models.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,17 @@ def __hash__(self) -> int:
188188
def __repr__(self) -> str:
189189
return f'{self.__class__.__name__}(name={self.name!r}, value={self.value!r})'
190190

191+
@property
192+
def _option_type(self) -> AppCommandOptionType:
193+
if isinstance(self.value, int):
194+
return AppCommandOptionType.integer
195+
elif isinstance(self.value, float):
196+
return AppCommandOptionType.number
197+
elif isinstance(self.value, str):
198+
return AppCommandOptionType.string
199+
else:
200+
raise TypeError(f'invalid Choice value type given, expected int, str, or float but received {self.value.__class__!r}')
201+
191202
def to_dict(self) -> ApplicationCommandOptionChoice:
192203
return {
193204
'name': self.name,

0 commit comments

Comments
 (0)