Skip to content

Commit 7308d08

Browse files
committed
🏷️ Better typing
1 parent 53036a4 commit 7308d08

File tree

1 file changed

+30
-14
lines changed

1 file changed

+30
-14
lines changed

discord/commands/options.py

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import inspect
2828
import logging
2929
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
3131

3232
from ..abc import GuildChannel, Mentionable
3333
from ..channel import (
@@ -39,7 +39,7 @@
3939
Thread,
4040
VoiceChannel,
4141
)
42-
from ..commands import ApplicationContext
42+
from ..commands import ApplicationContext, AutocompleteContext
4343
from ..enums import ChannelType
4444
from ..enums import Enum as DiscordEnum
4545
from ..enums import SlashCommandOptionType
@@ -111,6 +111,11 @@ def __init__(self, thread_type: Literal["public", "private", "news"]):
111111
self._type = type_map[thread_type]
112112

113113

114+
AutocompleteReturnType = Union[
115+
Iterable["OptionChoice"], Iterable[str], Iterable[int], Iterable[float]
116+
]
117+
118+
114119
class Option:
115120
"""Represents a selectable option for a slash command.
116121
@@ -146,15 +151,6 @@ class Option:
146151
max_length: Optional[:class:`int`]
147152
The maximum length of the string that can be entered. Must be between 1 and 6000 (inclusive).
148153
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.
158154
channel_types: list[:class:`discord.ChannelType`] | None
159155
A list of channel types that can be selected in this option.
160156
Only applies to Options with an :attr:`input_type` of :class:`discord.SlashCommandOptionType.channel`.
@@ -272,7 +268,7 @@ def __init__(
272268
)
273269
self.default = kwargs.pop("default", None)
274270

275-
self._autocomplete: Callable[[Any], Any, Any] | None = None
271+
self._autocomplete = None
276272
self.autocomplete = kwargs.pop("autocomplete", None)
277273
if len(enum_choices) > 25:
278274
self.choices: list[OptionChoice] = []
@@ -392,11 +388,31 @@ def __repr__(self):
392388
return f"<discord.commands.{self.__class__.__name__} name={self.name}>"
393389

394390
@property
395-
def autocomplete(self) -> Callable[[Any], Any, Any] | None:
391+
def autocomplete(self):
396392
return self._autocomplete
397393

398394
@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+
"""
400416
self._autocomplete = value
401417
# this is done here so it does not have to be computed every time the autocomplete is invoked
402418
if self._autocomplete is not None:

0 commit comments

Comments
 (0)