Skip to content

Commit e5d1616

Browse files
committed
Fix 3.9 non-existent Union type
1 parent c1801c0 commit e5d1616

File tree

1 file changed

+34
-30
lines changed

1 file changed

+34
-30
lines changed

disnake/ext/commands/params.py

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
Generic,
2727
List,
2828
Literal,
29+
Mapping,
2930
NoReturn,
3031
Optional,
3132
Sequence,
@@ -36,6 +37,8 @@
3637
get_origin,
3738
)
3839

40+
from typing_extensions import Concatenate, ParamSpec, Self, TypeAlias, TypeGuard
41+
3942
import disnake
4043
from disnake.app_commands import Option, OptionChoice
4144
from disnake.channel import _channel_type_factory
@@ -53,11 +56,10 @@
5356
from . import errors
5457
from .converter import CONVERTER_MAPPING
5558

56-
T_ = TypeVar("T_")
59+
T = TypeVar("T")
60+
P = ParamSpec("P")
5761

5862
if TYPE_CHECKING:
59-
from typing_extensions import Concatenate, ParamSpec, Self, TypeGuard
60-
6163
from disnake.app_commands import Choices
6264
from disnake.i18n import LocalizationValue, LocalizedOptional
6365
from disnake.types.interactions import ApplicationCommandOptionChoiceValue
@@ -69,35 +71,28 @@
6971

7072
AnySlashCommand = Union[InvokableSlashCommand, SubCommand]
7173

72-
P = ParamSpec("P")
73-
7474
InjectionCallback = Union[
75-
Callable[Concatenate[CogT, P], T_],
76-
Callable[P, T_],
75+
Callable[Concatenate[CogT, P], T],
76+
Callable[P, T],
7777
]
7878
AnyAutocompleter = Union[
7979
Sequence[Any],
8080
Callable[Concatenate[ApplicationCommandInteraction, str, P], Any],
8181
Callable[Concatenate[CogT, ApplicationCommandInteraction, str, P], Any],
8282
]
8383

84-
TChoice = TypeVar("TChoice", bound=ApplicationCommandOptionChoiceValue)
85-
else:
86-
P = TypeVar("P")
87-
8884

8985
if sys.version_info >= (3, 10):
9086
from types import EllipsisType, UnionType
91-
elif TYPE_CHECKING:
92-
EllipsisType = type(Ellipsis)
93-
UnionType = NoReturn
87+
88+
UnionTypes = (Union, UnionType)
9489

9590
else:
96-
UnionType = object()
97-
EllipsisType = type(Ellipsis)
91+
# using 'type' and not 'object', as 'type' is disjoint with 'int | float'
92+
EllipsisType: TypeAlias = type
93+
UnionTypes = (Union,)
9894

99-
T = TypeVar("T", bound=Any)
100-
TypeT = TypeVar("TypeT", bound=Type[Any])
95+
TypeT = TypeVar("TypeT", bound=type)
10196
BotT = TypeVar("BotT", bound="disnake.Client", covariant=True)
10297

10398
__all__ = (
@@ -131,7 +126,7 @@ def issubclass_(obj: Any, tp: Union[TypeT, Tuple[TypeT, ...]]) -> TypeGuard[Type
131126
if (origin := get_origin(obj)) is None:
132127
return False
133128

134-
if origin in (Union, UnionType):
129+
if origin in UnionTypes:
135130
# If we have a Union, try matching any of its args
136131
# (recursively, to handle possibly generic types inside this union)
137132
return any(issubclass_(o, tp) for o in obj.__args__)
@@ -141,7 +136,7 @@ def issubclass_(obj: Any, tp: Union[TypeT, Tuple[TypeT, ...]]) -> TypeGuard[Type
141136

142137
def remove_optionals(annotation: Any) -> Any:
143138
"""Remove unwanted optionals from an annotation."""
144-
if get_origin(annotation) in (Union, UnionType):
139+
if get_origin(annotation) in UnionTypes:
145140
args = tuple(i for i in annotation.__args__ if i not in (None, type(None)))
146141
if len(args) == 1:
147142
annotation = args[0]
@@ -208,7 +203,7 @@ def _unbound_range_to_str_len(
208203
return None, None
209204

210205

211-
class Injection(Generic[P, T_]):
206+
class Injection(Generic[P, T]):
212207
"""Represents a slash command injection.
213208
214209
.. versionadded:: 2.3
@@ -230,15 +225,15 @@ class Injection(Generic[P, T_]):
230225

231226
def __init__(
232227
self,
233-
function: InjectionCallback[CogT, P, T_],
228+
function: InjectionCallback[CogT, P, T],
234229
*,
235230
autocompleters: Optional[Dict[str, Callable]] = None,
236231
) -> None:
237232
if autocompleters is not None:
238233
for autocomp in autocompleters.values():
239234
classify_autocompleter(autocomp)
240235

241-
self.function: InjectionCallback[Any, P, T_] = function
236+
self.function: InjectionCallback[Any, P, T] = function
242237
self.autocompleters: Dict[str, Callable] = autocompleters or {}
243238
self._injected: Optional[Cog] = None
244239

@@ -252,7 +247,7 @@ def __get__(self, obj: Optional[Any], _: Type[Any]) -> Self:
252247

253248
return copy
254249

255-
def __call__(self, *args: P.args, **kwargs: P.kwargs) -> T_:
250+
def __call__(self, *args: P.args, **kwargs: P.kwargs) -> T:
256251
"""Calls the underlying function that the injection holds.
257252
258253
.. versionadded:: 2.6
@@ -265,11 +260,11 @@ def __call__(self, *args: P.args, **kwargs: P.kwargs) -> T_:
265260
@classmethod
266261
def register(
267262
cls,
268-
function: InjectionCallback[CogT, P, T_],
263+
function: InjectionCallback[CogT, P, T],
269264
annotation: Any,
270265
*,
271266
autocompleters: Optional[Dict[str, Callable]] = None,
272-
) -> Injection[P, T_]:
267+
) -> Injection[P, T]:
273268
self = cls(function, autocompleters=autocompleters)
274269
cls._registered[annotation] = self
275270
return self
@@ -536,7 +531,13 @@ class ParamInfo:
536531
.. versionadded:: 2.6
537532
"""
538533

539-
TYPES: ClassVar[Dict[Union[type, UnionType], int]] = {
534+
if sys.version_info >= (3, 10):
535+
TYPES: ClassVar[Mapping[Union[type, UnionType], int]]
536+
537+
else:
538+
TYPES: ClassVar[Mapping[Union[type, object], int]]
539+
540+
TYPES = { # noqa: RUF012
540541
str: OptionType.string.value,
541542
int: OptionType.integer.value,
542543
bool: OptionType.boolean.value,
@@ -821,7 +822,7 @@ def parse_annotation(self, annotation: Any, converter_mode: bool = False) -> boo
821822
or get_origin(annotation) is Literal
822823
):
823824
self._parse_enum(annotation)
824-
elif get_origin(annotation) in (Union, UnionType):
825+
elif get_origin(annotation) in UnionTypes:
825826
args = annotation.__args__
826827
if all(
827828
issubclass_(channel, (disnake.abc.GuildChannel, disnake.Thread)) for channel in args
@@ -1407,6 +1408,9 @@ def decorator(function: Callable[..., Any]) -> Injection:
14071408
return decorator
14081409

14091410

1411+
TChoice = TypeVar("TChoice", bound="ApplicationCommandOptionChoiceValue")
1412+
1413+
14101414
def option_enum(
14111415
choices: Union[Dict[str, TChoice], List[TChoice]], **kwargs: TChoice
14121416
) -> Type[TChoice]:
@@ -1455,10 +1459,10 @@ def converter_method(function: Any) -> ConverterMethod:
14551459

14561460

14571461
def register_injection(
1458-
function: InjectionCallback[CogT, P, T_],
1462+
function: InjectionCallback[CogT, P, T],
14591463
*,
14601464
autocompleters: Optional[Dict[str, Callable]] = None,
1461-
) -> Injection[P, T_]:
1465+
) -> Injection[P, T]:
14621466
"""A decorator to register a global injection.
14631467
14641468
.. versionadded:: 2.3

0 commit comments

Comments
 (0)