|
27 | 27 | import inspect |
28 | 28 | import logging |
29 | 29 | from enum import Enum |
30 | | -from typing import TYPE_CHECKING, Any, Callable, Literal, Optional, Type, Union |
| 30 | +from typing import TYPE_CHECKING, Iterable, Literal, Optional, Type, Union |
31 | 31 |
|
32 | 32 | from ..abc import GuildChannel, Mentionable |
33 | 33 | from ..channel import ( |
|
39 | 39 | Thread, |
40 | 40 | VoiceChannel, |
41 | 41 | ) |
42 | | -from ..commands import ApplicationContext |
| 42 | +from ..commands import ApplicationContext, AutocompleteContext |
43 | 43 | from ..enums import ChannelType |
44 | 44 | from ..enums import Enum as DiscordEnum |
45 | 45 | from ..enums import SlashCommandOptionType |
@@ -111,6 +111,11 @@ def __init__(self, thread_type: Literal["public", "private", "news"]): |
111 | 111 | self._type = type_map[thread_type] |
112 | 112 |
|
113 | 113 |
|
| 114 | +AutocompleteReturnType = Union[ |
| 115 | + Iterable["OptionChoice"], Iterable[str], Iterable[int], Iterable[float] |
| 116 | +] |
| 117 | + |
| 118 | + |
114 | 119 | class Option: |
115 | 120 | """Represents a selectable option for a slash command. |
116 | 121 |
|
@@ -146,15 +151,6 @@ class Option: |
146 | 151 | max_length: Optional[:class:`int`] |
147 | 152 | The maximum length of the string that can be entered. Must be between 1 and 6000 (inclusive). |
148 | 153 | Only applies to Options with an :attr:`input_type` of :class:`str`. |
149 | | - autocomplete: Optional[Callable[[:class:`.AutocompleteContext`], Awaitable[Union[Iterable[:class:`.OptionChoice`], Iterable[:class:`str`], Iterable[:class:`int`], Iterable[:class:`float`]]]]] |
150 | | - The autocomplete handler for the option. Accepts a callable (sync or async) |
151 | | - that takes a single argument of :class:`AutocompleteContext`. |
152 | | - The callable must return an iterable of :class:`str` or :class:`OptionChoice`. |
153 | | - Alternatively, :func:`discord.utils.basic_autocomplete` may be used in place of the callable. |
154 | | -
|
155 | | - .. note:: |
156 | | -
|
157 | | - Does not validate the input value against the autocomplete results. |
158 | 154 | channel_types: list[:class:`discord.ChannelType`] | None |
159 | 155 | A list of channel types that can be selected in this option. |
160 | 156 | Only applies to Options with an :attr:`input_type` of :class:`discord.SlashCommandOptionType.channel`. |
@@ -272,7 +268,7 @@ def __init__( |
272 | 268 | ) |
273 | 269 | self.default = kwargs.pop("default", None) |
274 | 270 |
|
275 | | - self._autocomplete: Callable[[Any], Any, Any] | None = None |
| 271 | + self._autocomplete = None |
276 | 272 | self.autocomplete = kwargs.pop("autocomplete", None) |
277 | 273 | if len(enum_choices) > 25: |
278 | 274 | self.choices: list[OptionChoice] = [] |
@@ -392,11 +388,31 @@ def __repr__(self): |
392 | 388 | return f"<discord.commands.{self.__class__.__name__} name={self.name}>" |
393 | 389 |
|
394 | 390 | @property |
395 | | - def autocomplete(self) -> Callable[[Any], Any, Any] | None: |
| 391 | + def autocomplete(self): |
396 | 392 | return self._autocomplete |
397 | 393 |
|
398 | 394 | @autocomplete.setter |
399 | | - def autocomplete(self, value: Callable[[Any], Any, Any] | None) -> None: |
| 395 | + def autocomplete(self, value) -> None: |
| 396 | + """ |
| 397 | + The autocomplete handler for the option. Accepts a callable (sync or async) |
| 398 | + that takes a single required argument of :class:`AutocompleteContext`. |
| 399 | + The callable must return an iterable of :class:`str` or :class:`OptionChoice`. |
| 400 | + Alternatively, :func:`discord.utils.basic_autocomplete` may be used in place of the callable. |
| 401 | +
|
| 402 | + Parameters |
| 403 | + ---------- |
| 404 | + value: Union[ |
| 405 | + Callable[[Self, AutocompleteContext, Any], AutocompleteReturnType], |
| 406 | + Callable[[AutocompleteContext, Any], AutocompleteReturnType], |
| 407 | + Callable[[Self, AutocompleteContext, Any], Awaitable[AutocompleteReturnType]], |
| 408 | + Callable[[AutocompleteContext, Any], Awaitable[AutocompleteReturnType]], |
| 409 | + ] |
| 410 | +
|
| 411 | + .. versionchanged:: 2.7 |
| 412 | +
|
| 413 | + .. note:: |
| 414 | + Does not validate the input value against the autocomplete results. |
| 415 | + """ |
400 | 416 | self._autocomplete = value |
401 | 417 | # this is done here so it does not have to be computed every time the autocomplete is invoked |
402 | 418 | if self._autocomplete is not None: |
|
0 commit comments