Browse Source

Add support for Range[str, ...] for app commands

pull/8200/head
Rapptz 3 years ago
parent
commit
bac66a9dab
  1. 26
      discord/app_commands/transformers.py

26
discord/app_commands/transformers.py

@ -122,10 +122,14 @@ class CommandParameter:
base['channel_types'] = [t.value for t in self.channel_types] base['channel_types'] = [t.value for t in self.channel_types]
if self.autocomplete: if self.autocomplete:
base['autocomplete'] = True base['autocomplete'] = True
min_key, max_key = (
('min_value', 'max_value') if self.type is not AppCommandOptionType.string else ('min_length', 'max_length')
)
if self.min_value is not None: if self.min_value is not None:
base['min_value'] = self.min_value base[min_key] = self.min_value
if self.max_value is not None: if self.max_value is not None:
base['max_value'] = self.max_value base[max_key] = self.max_value
return base return base
@ -215,8 +219,8 @@ class Transformer:
def min_value(cls) -> Optional[Union[int, float]]: def min_value(cls) -> Optional[Union[int, float]]:
"""Optional[:class:`int`]: The minimum supported value for this parameter. """Optional[:class:`int`]: The minimum supported value for this parameter.
Only valid if the :meth:`type` returns :attr:`~discord.AppCommandOptionType.number` or Only valid if the :meth:`type` returns :attr:`~discord.AppCommandOptionType.number`
:attr:`~discord.AppCommandOptionType.integer`. :attr:`~discord.AppCommandOptionType.integer`, or :attr:`~discord.AppCommandOptionType.string`.
Defaults to ``None``. Defaults to ``None``.
""" """
@ -226,8 +230,8 @@ class Transformer:
def max_value(cls) -> Optional[Union[int, float]]: def max_value(cls) -> Optional[Union[int, float]]:
"""Optional[:class:`int`]: The maximum supported value for this parameter. """Optional[:class:`int`]: The maximum supported value for this parameter.
Only valid if the :meth:`type` returns :attr:`~discord.AppCommandOptionType.number` or Only valid if the :meth:`type` returns :attr:`~discord.AppCommandOptionType.number`
:attr:`~discord.AppCommandOptionType.integer`. :attr:`~discord.AppCommandOptionType.integer`, or :attr:`~discord.AppCommandOptionType.string`.
Defaults to ``None``. Defaults to ``None``.
""" """
@ -433,8 +437,8 @@ else:
return _TransformMetadata(transformer) return _TransformMetadata(transformer)
class Range: class Range:
"""A type annotation that can be applied to a parameter to require a numeric type """A type annotation that can be applied to a parameter to require a numeric or string
to fit within the range provided. type to fit within the range provided.
During type checking time this is equivalent to :obj:`typing.Annotated` so type checkers understand During type checking time this is equivalent to :obj:`typing.Annotated` so type checkers understand
the intent of the code. the intent of the code.
@ -480,8 +484,10 @@ else:
opt_type = AppCommandOptionType.integer opt_type = AppCommandOptionType.integer
elif obj_type is float: elif obj_type is float:
opt_type = AppCommandOptionType.number opt_type = AppCommandOptionType.number
elif obj_type is str:
opt_type = AppCommandOptionType.string
else: else:
raise TypeError(f'expected int or float as range type, received {obj_type!r} instead') raise TypeError(f'expected int, float, or str as range type, received {obj_type!r} instead')
transformer = _make_range_transformer( transformer = _make_range_transformer(
opt_type, opt_type,
@ -736,7 +742,7 @@ def annotation_to_parameter(annotation: Any, parameter: inspect.Parameter) -> Co
result.choices = choices result.choices = choices
# These methods should be duck typed # These methods should be duck typed
if type in (AppCommandOptionType.number, AppCommandOptionType.integer): if type in (AppCommandOptionType.number, AppCommandOptionType.string, AppCommandOptionType.integer):
result.min_value = inner.min_value() result.min_value = inner.min_value()
result.max_value = inner.max_value() result.max_value = inner.max_value()

Loading…
Cancel
Save