Skip to content

Commit 102502f

Browse files
committed
add default typings for Select
1 parent b2dce41 commit 102502f

File tree

4 files changed

+36
-9
lines changed

4 files changed

+36
-9
lines changed

discord/abc.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ class User(Snowflake, Protocol):
235235
name: str
236236
discriminator: str
237237
global_name: str | None
238-
avatar: Asset
238+
avatar: Asset | None
239239
bot: bool
240240

241241
@property
@@ -301,6 +301,7 @@ def is_member(self) -> bool:
301301
GCH = TypeVar("GCH", bound="GuildChannel")
302302

303303

304+
304305
class GuildChannel:
305306
"""An ABC that details the common operations on a Discord guild channel.
306307

discord/components.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,7 @@ def _handle_model(
597597
) -> SelectDefaultValue:
598598
# preventing >circular imports<
599599
from discord import Member, Object, Role, User, abc
600+
from discord.user import _UserTag
600601

601602
instances_mapping: dict[
602603
type, tuple[tuple[ComponentType, ...], SelectDefaultValueType]
@@ -613,7 +614,7 @@ def _handle_model(
613614
(ComponentType.user_select, ComponentType.mentionable_select),
614615
SelectDefaultValueType.user,
615616
),
616-
abc.User: (
617+
_UserTag: (
617618
(ComponentType.user_select, ComponentType.mentionable_select),
618619
SelectDefaultValueType.user,
619620
),

discord/ui/select.py

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import os
3030
import sys
3131
from collections.abc import Sequence
32+
from functools import partial
3233
from typing import TYPE_CHECKING, Any, Callable, Generic, TypeVar
3334

3435
from ..channel import _threaded_guild_channel_factory
@@ -53,6 +54,11 @@
5354
"role_select",
5455
"mentionable_select",
5556
"channel_select",
57+
"StringSelect",
58+
"UserSelect",
59+
"RoleSelect",
60+
"MentionableSelect",
61+
"ChannelSelect",
5662
)
5763

5864
if TYPE_CHECKING:
@@ -717,6 +723,25 @@ def uses_label(self) -> bool:
717723
return bool(self.label or self.description or (self.required is not None))
718724

719725

726+
if TYPE_CHECKING:
727+
StringSelect = Select[V, str]
728+
"""A typed alias for :class:`Select` for string values."""
729+
UserSelect = Select[V, User | Member]
730+
"""A typed alias for :class:`Select` for user-like values."""
731+
RoleSelect = Select[V, Role]
732+
"""A typed alias for :class:`Select` for role values."""
733+
MentionableSelect = Select[V, User | Member | Role]
734+
"""A typed alias for :class:`Select` for mentionable (role and user-like) values."""
735+
ChannelSelect = Select[V, GuildChannel | Thread]
736+
"""A typed alias for :class:`Select` for channel values."""
737+
else:
738+
StringSelect: Select[V, str] = partial(Select, select_type=ComponentType.string_select)
739+
UserSelect: Select[V, User | Member] = partial(Select, select_type=ComponentType.user_select)
740+
RoleSelect: Select[V, Role] = partial(Select, select_type=ComponentType.role_select)
741+
MentionableSelect: Select[V, Role | User | Member] = partial(Select, select_type=ComponentType.mentionable_select)
742+
ChannelSelect: Select[V, GuildChannel | Thread] = partial(Select, select_type=ComponentType.channel_select)
743+
744+
720745
_select_types = (
721746
ComponentType.string_select,
722747
ComponentType.user_select,
@@ -877,7 +902,7 @@ def string_select(
877902
disabled: bool = False,
878903
row: int | None = None,
879904
id: int | None = None,
880-
) -> Callable[[ItemCallbackType[Select[V, str]]], Select[V, str]]:
905+
) -> Callable[[ItemCallbackType[StringSelect[V]]], StringSelect[V]]:
881906
"""A shortcut for :meth:`discord.ui.select` with select type :attr:`discord.ComponentType.string_select`.
882907
883908
.. versionadded:: 2.3
@@ -905,7 +930,7 @@ def user_select(
905930
row: int | None = None,
906931
id: int | None = None,
907932
default_values: Sequence[SelectDefaultValue | Snowflake] | None = None,
908-
) -> Callable[[ItemCallbackType[Select[V, User | Member]]], Select[V, User | Member]]:
933+
) -> Callable[[ItemCallbackType[UserSelect[V]]], UserSelect[V]]:
909934
"""A shortcut for :meth:`discord.ui.select` with select type :attr:`discord.ComponentType.user_select`.
910935
911936
.. versionadded:: 2.3
@@ -933,7 +958,7 @@ def role_select(
933958
row: int | None = None,
934959
id: int | None = None,
935960
default_values: Sequence[SelectDefaultValue | Snowflake] | None = None,
936-
) -> Callable[[ItemCallbackType[Select[V, Role]]], Select[V, Role]]:
961+
) -> Callable[[ItemCallbackType[RoleSelect[V]]], RoleSelect[V]]:
937962
"""A shortcut for :meth:`discord.ui.select` with select type :attr:`discord.ComponentType.role_select`.
938963
939964
.. versionadded:: 2.3
@@ -962,7 +987,7 @@ def mentionable_select(
962987
id: int | None = None,
963988
default_values: Sequence[SelectDefaultValue | Snowflake] | None = None,
964989
) -> Callable[
965-
[ItemCallbackType[Select[V, Role | User | Member]]], Select[V, Role | User | Member]
990+
[ItemCallbackType[MentionableSelect[V]]], MentionableSelect[V],
966991
]:
967992
"""A shortcut for :meth:`discord.ui.select` with select type :attr:`discord.ComponentType.mentionable_select`.
968993
@@ -993,8 +1018,8 @@ def channel_select(
9931018
id: int | None = None,
9941019
default_values: Sequence[SelectDefaultValue | Snowflake] | None = None,
9951020
) -> Callable[
996-
[ItemCallbackType[Select[V, GuildChannel | Thread]]],
997-
Select[V, GuildChannel | Thread],
1021+
[ItemCallbackType[ChannelSelect[V]]],
1022+
ChannelSelect[V],
9981023
]:
9991024
"""A shortcut for :meth:`discord.ui.select` with select type :attr:`discord.ComponentType.channel_select`.
10001025

discord/ui/view.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
from ..components import TextDisplay as TextDisplayComponent
4747
from ..components import Thumbnail as ThumbnailComponent
4848
from ..components import _component_factory
49-
from ..utils import find, get
49+
from ..utils import find
5050
from .item import Item, ItemCallbackType
5151

5252
__all__ = ("View", "_component_to_item", "_walk_all_components")

0 commit comments

Comments
 (0)