Skip to content

Commit 6ec28e1

Browse files
committed
🏷️ Boring typing stuff
1 parent c2b4011 commit 6ec28e1

File tree

1 file changed

+30
-20
lines changed

1 file changed

+30
-20
lines changed

discord/commands/options.py

+30-20
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@
2626

2727
import inspect
2828
import logging
29+
from collections.abc import Awaitable, Callable, Iterable
2930
from enum import Enum
30-
from typing import TYPE_CHECKING, Iterable, Literal, Optional, Type, Union
31+
from typing import TYPE_CHECKING, Any, Literal, Optional, Type, TypeVar, Union
3132

3233
from ..abc import GuildChannel, Mentionable
3334
from ..channel import (
@@ -46,6 +47,7 @@
4647
from ..utils import MISSING, basic_autocomplete
4748

4849
if TYPE_CHECKING:
50+
from ..cog import Cog
4951
from ..ext.commands import Converter
5052
from ..member import Member
5153
from ..message import Attachment
@@ -71,6 +73,24 @@
7173
Type[DiscordEnum],
7274
]
7375

76+
AutocompleteReturnType = Union[
77+
Iterable["OptionChoice"], Iterable[str], Iterable[int], Iterable[float]
78+
]
79+
T = TypeVar("T", bound=AutocompleteReturnType)
80+
MaybeAwaitable = Union[T, Awaitable[T]]
81+
AutocompleteFunction = Union[
82+
Callable[[AutocompleteContext], MaybeAwaitable[AutocompleteReturnType]],
83+
Callable[[Cog, AutocompleteContext], MaybeAwaitable[AutocompleteReturnType]],
84+
Callable[
85+
[AutocompleteContext, Any], MaybeAwaitable[AutocompleteReturnType]
86+
], # pyright: ignore [reportExplicitAny]
87+
Callable[
88+
[Cog, AutocompleteContext, Any], # pyright: ignore [reportExplicitAny]
89+
MaybeAwaitable[AutocompleteReturnType],
90+
],
91+
]
92+
93+
7494
__all__ = (
7595
"ThreadOption",
7696
"Option",
@@ -111,11 +131,6 @@ def __init__(self, thread_type: Literal["public", "private", "news"]):
111131
self._type = type_map[thread_type]
112132

113133

114-
AutocompleteReturnType = Union[
115-
Iterable["OptionChoice"], Iterable[str], Iterable[int], Iterable[float]
116-
]
117-
118-
119134
class Option:
120135
"""Represents a selectable option for a slash command.
121136
@@ -268,7 +283,7 @@ def __init__(
268283
)
269284
self.default = kwargs.pop("default", None)
270285

271-
self._autocomplete = None
286+
self._autocomplete: AutocompleteFunction | None = None
272287
self.autocomplete = kwargs.pop("autocomplete", None)
273288
if len(enum_choices) > 25:
274289
self.choices: list[OptionChoice] = []
@@ -388,22 +403,17 @@ def __repr__(self):
388403
return f"<discord.commands.{self.__class__.__name__} name={self.name}>"
389404

390405
@property
391-
def autocomplete(self):
406+
def autocomplete(self) -> AutocompleteFunction | None:
392407
"""
393408
The autocomplete handler for the option. Accepts a callable (sync or async)
394-
that takes a single required argument of :class:`AutocompleteContext`.
409+
that takes a single required argument of :class:`AutocompleteContext` or two arguments
410+
of :class:`discord.Cog` (being the command's cog) and :class:`AutocompleteContext`.
395411
The callable must return an iterable of :class:`str` or :class:`OptionChoice`.
396412
Alternatively, :func:`discord.utils.basic_autocomplete` may be used in place of the callable.
397413
398414
Returns
399415
-------
400-
Union[
401-
Callable[[Self, AutocompleteContext, Any], AutocompleteReturnType],
402-
Callable[[AutocompleteContext, Any], AutocompleteReturnType],
403-
Callable[[Self, AutocompleteContext, Any], Awaitable[AutocompleteReturnType]],
404-
Callable[[AutocompleteContext, Any], Awaitable[AutocompleteReturnType]],
405-
None
406-
]
416+
Optional[AutocompleteFunction]
407417
408418
.. versionchanged:: 2.7
409419
@@ -413,17 +423,17 @@ def autocomplete(self):
413423
return self._autocomplete
414424

415425
@autocomplete.setter
416-
def autocomplete(self, value) -> None:
426+
def autocomplete(self, value: AutocompleteFunction | None) -> None:
417427
self._autocomplete = value
418428
# this is done here so it does not have to be computed every time the autocomplete is invoked
419429
if self._autocomplete is not None:
420-
self._autocomplete._is_instance_method = (
430+
self._autocomplete._is_instance_method = ( # pyright: ignore [reportFunctionMemberAccess]
421431
sum(
422432
1
423433
for param in inspect.signature(
424-
self.autocomplete
434+
self._autocomplete
425435
).parameters.values()
426-
if param.default == param.empty
436+
if param.default == param.empty # pyright: ignore[reportAny]
427437
and param.kind not in (param.VAR_POSITIONAL, param.VAR_KEYWORD)
428438
)
429439
== 2

0 commit comments

Comments
 (0)