Browse Source

chore: fix is_persistent and default to sys.maxsize instead of 0 on sorting key

pull/10166/head
DA-344 3 weeks ago
parent
commit
4ca483efdb
  1. 9
      discord/ui/action_row.py
  2. 8
      discord/ui/container.py
  3. 6
      discord/ui/section.py

9
discord/ui/action_row.py

@ -23,6 +23,7 @@ DEALINGS IN THE SOFTWARE.
"""
from __future__ import annotations
import sys
from typing import (
TYPE_CHECKING,
Any,
@ -202,6 +203,9 @@ class ActionRow(Item[V]):
def is_dispatchable(self) -> bool:
return any(c.is_dispatchable() for c in self.children)
def is_persistent(self) -> bool:
return self.is_dispatchable() and all(c.is_persistent() for c in self.children)
def _update_children_view(self, view: LayoutView) -> None:
for child in self._children:
child._view = view # pyright: ignore[reportAttributeAccessIssue]
@ -330,8 +334,9 @@ class ActionRow(Item[V]):
def to_component_dict(self) -> Dict[str, Any]:
components = []
for item in self._children:
components.append(item.to_component_dict())
key = lambda i: i._rendered_row or i._row or sys.maxsize
for child in sorted(self._children, key=key):
components.append(child.to_component_dict())
base = {
'type': self.type.value,

8
discord/ui/container.py

@ -25,6 +25,7 @@ from __future__ import annotations
import copy
import os
import sys
from typing import (
TYPE_CHECKING,
Any,
@ -210,6 +211,9 @@ class Container(Item[V]):
def is_dispatchable(self) -> bool:
return bool(self.__dispatchable)
def is_persistent(self) -> bool:
return self.is_dispatchable() and all(c.is_persistent() for c in self.children)
def __init_subclass__(cls) -> None:
super().__init_subclass__()
@ -263,7 +267,9 @@ class Container(Item[V]):
def to_components(self) -> List[Dict[str, Any]]:
components = []
for child in sorted(self._children, key=lambda i: i._rendered_row or 0):
key = lambda i: i._rendered_row or i._row or sys.maxsize
for child in sorted(self._children, key=key):
components.append(child.to_component_dict())
return components

6
discord/ui/section.py

@ -23,6 +23,7 @@ DEALINGS IN THE SOFTWARE.
"""
from __future__ import annotations
import sys
from typing import TYPE_CHECKING, Any, Dict, Generator, List, Literal, Optional, TypeVar, Union, ClassVar
from .item import Item
@ -115,6 +116,9 @@ class Section(Item[V]):
def is_dispatchable(self) -> bool:
return self.accessory.is_dispatchable()
def is_persistent(self) -> bool:
return self.is_dispatchable() and self.accessory.is_persistent()
def walk_children(self) -> Generator[Item[V], None, None]:
"""An iterator that recursively walks through all the children of this section.
and it's children, if applicable.
@ -239,7 +243,7 @@ class Section(Item[V]):
c.to_component_dict()
for c in sorted(
self._children,
key=lambda i: i._rendered_row or 0,
key=lambda i: i._rendered_row or sys.maxsize,
)
],
'accessory': self.accessory.to_component_dict(),

Loading…
Cancel
Save