diff --git a/discord/components.py b/discord/components.py index cb1a50976..976906638 100644 --- a/discord/components.py +++ b/discord/components.py @@ -79,6 +79,17 @@ if TYPE_CHECKING: ActionRowChildComponentType = Union['Button', 'SelectMenu', 'TextInput'] SectionComponentType = Union['TextDisplay', 'Button'] + MessageComponentType = Union[ + ActionRowChildComponentType, + SectionComponentType, + 'ActionRow', + 'SectionComponent', + 'ThumbnailComponent', + 'MediaGalleryComponent', + 'FileComponent', + 'SectionComponent', + 'Component', + ] __all__ = ( @@ -337,13 +348,9 @@ class SelectMenu(Component): self.placeholder: Optional[str] = data.get('placeholder') self.min_values: int = data.get('min_values', 1) self.max_values: int = data.get('max_values', 1) - self.options: List[SelectOption] = [ - SelectOption.from_dict(option) for option in data.get('options', []) - ] + self.options: List[SelectOption] = [SelectOption.from_dict(option) for option in data.get('options', [])] self.disabled: bool = data.get('disabled', False) - self.channel_types: List[ChannelType] = [ - try_enum(ChannelType, t) for t in data.get('channel_types', []) - ] + self.channel_types: List[ChannelType] = [try_enum(ChannelType, t) for t in data.get('channel_types', [])] self.default_values: List[SelectDefaultValue] = [ SelectDefaultValue.from_dict(d) for d in data.get('default_values', []) ] @@ -459,9 +466,7 @@ class SelectOption: elif isinstance(value, _EmojiTag): self._emoji = value._to_partial() else: - raise TypeError( - f'expected str, Emoji, or PartialEmoji, received {value.__class__.__name__} instead' - ) + raise TypeError(f'expected str, Emoji, or PartialEmoji, received {value.__class__.__name__} instead') else: self._emoji = None @@ -617,9 +622,7 @@ class SelectDefaultValue: @type.setter def type(self, value: SelectDefaultValueType) -> None: if not isinstance(value, SelectDefaultValueType): - raise TypeError( - f'expected SelectDefaultValueType, received {value.__class__.__name__} instead' - ) + raise TypeError(f'expected SelectDefaultValueType, received {value.__class__.__name__} instead') self._type = value @@ -733,7 +736,7 @@ class SectionComponent(Component): self.components.append(component) # type: ignore # should be the correct type here try: - self.accessory: Optional[Component] = _component_factory(data['accessory']) + self.accessory: Optional[Component] = _component_factory(data['accessory']) # type: ignore except KeyError: self.accessory = None @@ -788,7 +791,7 @@ class ThumbnailComponent(Component): def to_dict(self) -> ThumbnailComponentPayload: return { - 'media': self.media.to_dict(), # type: ignroe + 'media': self.media.to_dict(), # pyright: ignore[reportReturnType] 'description': self.description, 'spoiler': self.spoiler, 'type': self.type.value, @@ -938,9 +941,7 @@ class MediaGalleryItem: self._state: Optional[ConnectionState] = None @classmethod - def _from_data( - cls, data: MediaGalleryItemPayload, state: Optional[ConnectionState] - ) -> MediaGalleryItem: + def _from_data(cls, data: MediaGalleryItemPayload, state: Optional[ConnectionState]) -> MediaGalleryItem: media = data['media'] self = cls( media=media['url'], @@ -1089,7 +1090,7 @@ class Container(Component): self.spoiler: bool = data.get('spoiler', False) self._colour: Optional[Colour] try: - self._colour = Colour(data['accent_color']) + self._colour = Colour(data['accent_color']) # type: ignore except KeyError: self._colour = None @@ -1102,9 +1103,7 @@ class Container(Component): """Optional[:class:`Color`]: The container's accent color.""" -def _component_factory( - data: ComponentPayload, state: Optional[ConnectionState] = None -) -> Optional[Component]: +def _component_factory(data: ComponentPayload, state: Optional[ConnectionState] = None) -> Optional[Component]: if data['type'] == 1: return ActionRow(data) elif data['type'] == 2: @@ -1112,7 +1111,7 @@ def _component_factory( elif data['type'] == 4: return TextInput(data) elif data['type'] in (3, 5, 6, 7, 8): - return SelectMenu(data) + return SelectMenu(data) # type: ignore elif data['type'] == 9: return SectionComponent(data, state) elif data['type'] == 10: diff --git a/discord/message.py b/discord/message.py index c551138f7..c0a853ce3 100644 --- a/discord/message.py +++ b/discord/message.py @@ -96,7 +96,7 @@ if TYPE_CHECKING: from .types.gateway import MessageReactionRemoveEvent, MessageUpdateEvent from .abc import Snowflake from .abc import GuildChannel, MessageableChannel - from .components import ActionRow, ActionRowChildComponentType + from .components import MessageComponentType from .state import ConnectionState from .mentions import AllowedMentions from .user import User @@ -104,7 +104,6 @@ if TYPE_CHECKING: from .ui.view import View EmojiInputType = Union[Emoji, PartialEmoji, str] - MessageComponentType = Union[ActionRow, ActionRowChildComponentType] __all__ = ( diff --git a/discord/types/components.py b/discord/types/components.py index 68aa6156d..98201817a 100644 --- a/discord/types/components.py +++ b/discord/types/components.py @@ -59,7 +59,7 @@ class ButtonComponent(ComponentBase): sku_id: NotRequired[str] -class SelectOption(ComponentBase): +class SelectOption(TypedDict): label: str value: str default: bool diff --git a/discord/ui/section.py b/discord/ui/section.py index fece9b053..f2b6554ca 100644 --- a/discord/ui/section.py +++ b/discord/ui/section.py @@ -72,10 +72,8 @@ class Section(Item[V]): ) -> None: super().__init__() if len(children) > 3: - raise ValueError('maximum number of children exceeded') - self._children: List[Item[Any]] = [ - c if isinstance(c, Item) else TextDisplay(c) for c in children - ] + raise ValueError('maximum number of children exceeded') + self._children: List[Item[Any]] = [c if isinstance(c, Item) else TextDisplay(c) for c in children] self.accessory: Optional[Item[Any]] = accessory self.row = row @@ -150,7 +148,8 @@ class Section(Item[V]): @classmethod def from_component(cls, component: SectionComponent) -> Self: - from .view import _component_to_item # >circular import< + from .view import _component_to_item # >circular import< + return cls( children=[_component_to_item(c) for c in component.components], accessory=_component_to_item(component.accessory) if component.accessory else None, diff --git a/discord/ui/thumbnail.py b/discord/ui/thumbnail.py index ce178fb4c..05e68b881 100644 --- a/discord/ui/thumbnail.py +++ b/discord/ui/thumbnail.py @@ -37,9 +37,8 @@ if TYPE_CHECKING: V = TypeVar('V', bound='View', covariant=True) -__all__ = ( - 'Thumbnail', -) +__all__ = ('Thumbnail',) + class Thumbnail(Item[V]): """Represents a UI Thumbnail. diff --git a/discord/ui/view.py b/discord/ui/view.py index 92ec768fa..8dd7ca2d4 100644 --- a/discord/ui/view.py +++ b/discord/ui/view.py @@ -23,7 +23,7 @@ DEALINGS IN THE SOFTWARE. """ from __future__ import annotations -from typing import Any, Callable, ClassVar, Coroutine, Dict, Iterator, List, Optional, Sequence, TYPE_CHECKING, Tuple, Type, Union +from typing import Any, Callable, ClassVar, Coroutine, Dict, Iterator, List, Optional, Sequence, TYPE_CHECKING, Tuple, Type from functools import partial from itertools import groupby @@ -709,12 +709,7 @@ class ViewStore: item._view = view item._rendered_row = base_item._rendered_row item._refresh_state(interaction, interaction.data) # type: ignore -The relative row this text display belongs to. By default - items are arranged automatically into those rows. If you'd - like to control the relative positioning of the row then - passing an index is advised. For example, row=1 will show - up before row=2. Defaults to ``None``, which is automatic - ordering. The row number must be between 0 and 9 (i.e. zero indexed) + try: allow = await item.interaction_check(interaction) except Exception: