26
26
27
27
import inspect
28
28
import logging
29
+ from collections .abc import Awaitable , Callable , Iterable
29
30
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
31
32
32
33
from ..abc import GuildChannel , Mentionable
33
34
from ..channel import (
46
47
from ..utils import MISSING , basic_autocomplete
47
48
48
49
if TYPE_CHECKING :
50
+ from ..cog import Cog
49
51
from ..ext .commands import Converter
50
52
from ..member import Member
51
53
from ..message import Attachment
71
73
Type [DiscordEnum ],
72
74
]
73
75
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
+
74
94
__all__ = (
75
95
"ThreadOption" ,
76
96
"Option" ,
@@ -111,11 +131,6 @@ def __init__(self, thread_type: Literal["public", "private", "news"]):
111
131
self ._type = type_map [thread_type ]
112
132
113
133
114
- AutocompleteReturnType = Union [
115
- Iterable ["OptionChoice" ], Iterable [str ], Iterable [int ], Iterable [float ]
116
- ]
117
-
118
-
119
134
class Option :
120
135
"""Represents a selectable option for a slash command.
121
136
@@ -268,7 +283,7 @@ def __init__(
268
283
)
269
284
self .default = kwargs .pop ("default" , None )
270
285
271
- self ._autocomplete = None
286
+ self ._autocomplete : AutocompleteFunction | None = None
272
287
self .autocomplete = kwargs .pop ("autocomplete" , None )
273
288
if len (enum_choices ) > 25 :
274
289
self .choices : list [OptionChoice ] = []
@@ -388,22 +403,17 @@ def __repr__(self):
388
403
return f"<discord.commands.{ self .__class__ .__name__ } name={ self .name } >"
389
404
390
405
@property
391
- def autocomplete (self ):
406
+ def autocomplete (self ) -> AutocompleteFunction | None :
392
407
"""
393
408
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`.
395
411
The callable must return an iterable of :class:`str` or :class:`OptionChoice`.
396
412
Alternatively, :func:`discord.utils.basic_autocomplete` may be used in place of the callable.
397
413
398
414
Returns
399
415
-------
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]
407
417
408
418
.. versionchanged:: 2.7
409
419
@@ -413,17 +423,17 @@ def autocomplete(self):
413
423
return self ._autocomplete
414
424
415
425
@autocomplete .setter
416
- def autocomplete (self , value ) -> None :
426
+ def autocomplete (self , value : AutocompleteFunction | None ) -> None :
417
427
self ._autocomplete = value
418
428
# this is done here so it does not have to be computed every time the autocomplete is invoked
419
429
if self ._autocomplete is not None :
420
- self ._autocomplete ._is_instance_method = (
430
+ self ._autocomplete ._is_instance_method = ( # pyright: ignore [reportFunctionMemberAccess]
421
431
sum (
422
432
1
423
433
for param in inspect .signature (
424
- self .autocomplete
434
+ self ._autocomplete
425
435
).parameters .values ()
426
- if param .default == param .empty
436
+ if param .default == param .empty # pyright: ignore[reportAny]
427
437
and param .kind not in (param .VAR_POSITIONAL , param .VAR_KEYWORD )
428
438
)
429
439
== 2
0 commit comments