Browse Source

chore: Some changes, fixes, and typo corrections

pull/10166/head
DA-344 2 months ago
parent
commit
4c662a9c24
  1. 2
      discord/ui/action_row.py
  2. 4
      discord/ui/container.py
  3. 2
      discord/ui/file.py
  4. 8
      discord/ui/item.py
  5. 2
      discord/ui/section.py
  6. 28
      discord/ui/view.py

2
discord/ui/action_row.py

@ -99,7 +99,7 @@ class ActionRow(Item[V]):
__action_row_children_items__: ClassVar[List[ItemCallbackType[Any, Any]]] = []
__discord_ui_action_row__: ClassVar[bool] = True
__pending_view__: ClassVar[bool] = True
__discord_ui_update_view__: ClassVar[bool] = True
def __init__(self, *, id: Optional[int] = None) -> None:
super().__init__()

4
discord/ui/container.py

@ -84,7 +84,7 @@ class Container(Item[V]):
"""
__container_children_items__: ClassVar[List[Union[ItemCallbackType[Any, Any], Item[Any]]]] = []
__pending_view__: ClassVar[bool] = True
__discord_ui_update_view__: ClassVar[bool] = True
__discord_ui_container__: ClassVar[bool] = True
def __init__(
@ -157,7 +157,7 @@ class Container(Item[V]):
def _update_children_view(self, view) -> None:
for child in self._children:
child._view = view
if getattr(child, '__pending_view__', False):
if getattr(child, '__discord_ui_update_view__', False):
# if the item is an action row which child's view can be updated, then update it
child._update_children_view(view) # type: ignore

2
discord/ui/file.py

@ -47,7 +47,7 @@ class File(Item[V]):
Parameters
----------
media: Union[:class:`str`, :class:`.UnfurledMediaItem`]
This file's media. If this is a string itmust point to a local
This file's media. If this is a string it must point to a local
file uploaded within the parent view of this item, and must
meet the ``attachment://file-name.extension`` structure.
spoiler: :class:`bool`

8
discord/ui/item.py

@ -53,6 +53,14 @@ class Item(Generic[V]):
- :class:`discord.ui.Button`
- :class:`discord.ui.Select`
- :class:`discord.ui.TextInput`
- :class:`discord.ui.ActionRow`
- :class:`discord.ui.Container`
- :class:`discord.ui.File`
- :class:`discord.ui.MediaGallery`
- :class:`discord.ui.Section`
- :class:`discord.ui.Separator`
- :class:`discord.ui.TextDisplay`
- :class:`discord.ui.Thumbnail`
.. versionadded:: 2.0
"""

2
discord/ui/section.py

@ -64,7 +64,7 @@ class Section(Item[V]):
"""
__discord_ui_section__: ClassVar[bool] = True
__pending_view__: ClassVar[bool] = True
__discord_ui_update_view__: ClassVar[bool] = True
__slots__ = (
'_children',

28
discord/ui/view.py

@ -23,6 +23,8 @@ DEALINGS IN THE SOFTWARE.
"""
from __future__ import annotations
import warnings
from typing import (
Any,
Callable,
@ -183,10 +185,10 @@ class _ViewWeights:
class _ViewCallback:
__slots__ = ('view', 'callback', 'item')
def __init__(self, callback: ItemCallbackType[Any, Any], view: View, item: Item[View]) -> None:
def __init__(self, callback: ItemCallbackType[Any, Any], view: BaseView, item: Item[BaseView]) -> None:
self.callback: ItemCallbackType[Any, Any] = callback
self.view: View = view
self.item: Item[View] = item
self.view: BaseView = view
self.item: Item[BaseView] = item
def __call__(self, interaction: Interaction) -> Coroutine[Any, Any, Any]:
return self.callback(self.view, interaction, self.item)
@ -223,7 +225,7 @@ class BaseView:
parent = getattr(raw, '__discord_ui_parent__', None)
if parent and parent._view is None:
parent._view = self
if getattr(raw, '__pending_view__', False):
if getattr(raw, '__discord_ui_update_view__', False):
raw._update_children_view(self) # type: ignore
children.append(raw)
else:
@ -559,13 +561,15 @@ class BaseView:
return await self.__stopped
class View(BaseView): # NOTE: maybe add a deprecation warning in favour of LayoutView?
class View(BaseView):
"""Represents a UI view.
This object must be inherited to create a UI within Discord.
.. versionadded:: 2.0
.. deprecated:: 2.6
Parameters
-----------
timeout: Optional[:class:`float`]
@ -576,6 +580,10 @@ class View(BaseView): # NOTE: maybe add a deprecation warning in favour of Layo
__discord_ui_view__: ClassVar[bool] = True
def __init_subclass__(cls) -> None:
warnings.warn(
'discord.ui.View and subclasses are deprecated, use discord.ui.LayoutView instead',
DeprecationWarning,
)
super().__init_subclass__()
children: Dict[str, ItemCallbackType[Any, Any]] = {}
@ -691,10 +699,7 @@ class View(BaseView): # NOTE: maybe add a deprecation warning in favour of Layo
class LayoutView(BaseView):
"""Represents a layout view for components v2.
Unline :class:`View` this allows for components v2 to exist
within it.
"""Represents a layout view for components.
.. versionadded:: 2.6
@ -710,6 +715,7 @@ class LayoutView(BaseView):
def __init_subclass__(cls) -> None:
children: Dict[str, Item[Any]] = {}
callback_children: Dict[str, ItemCallbackType[Any, Any]] = {}
row = 0
@ -720,12 +726,12 @@ class LayoutView(BaseView):
children[name] = member
row += 1
elif hasattr(member, '__discord_ui_model_type__') and getattr(member, '__discord_ui_parent__', None):
children[name] = member
callback_children[name] = member
if len(children) > 10:
raise TypeError('LayoutView cannot have more than 10 top-level children')
cls.__view_children_items__ = list(children.values())
cls.__view_children_items__ = list(children.values()) + list(callback_children.values())
def _is_v2(self) -> bool:
return True

Loading…
Cancel
Save