Browse Source

Make Item and Button generic over the underlying view

pull/6961/head
Rapptz 4 years ago
parent
commit
ed9badcddf
  1. 5
      discord/ui/button.py
  2. 9
      discord/ui/item.py

5
discord/ui/button.py

@ -41,7 +41,7 @@ __all__ = (
) )
if TYPE_CHECKING: if TYPE_CHECKING:
from ..components import Component from .view import View
_custom_emoji = re.compile(r'<?(?P<animated>a)?:?(?P<name>[A-Za-z0-9\_]+):(?P<id>[0-9]{13,20})>?') _custom_emoji = re.compile(r'<?(?P<animated>a)?:?(?P<name>[A-Za-z0-9\_]+):(?P<id>[0-9]{13,20})>?')
@ -63,9 +63,10 @@ def _to_partial_emoji(obj: Union[str, PartialEmoji], *, _custom_emoji=_custom_em
B = TypeVar('B', bound='Button') B = TypeVar('B', bound='Button')
V = TypeVar('V', bound='View', covariant=True)
class Button(Item): class Button(Item[V]):
"""Represents a UI button. """Represents a UI button.
.. versionadded:: 2.0 .. versionadded:: 2.0

9
discord/ui/item.py

@ -24,7 +24,7 @@ DEALINGS IN THE SOFTWARE.
from __future__ import annotations from __future__ import annotations
from typing import Any, Callable, Coroutine, Dict, Optional, TYPE_CHECKING, Tuple, Type, TypeVar from typing import Any, Callable, Coroutine, Dict, Generic, Optional, TYPE_CHECKING, Tuple, Type, TypeVar
from ..interactions import Interaction from ..interactions import Interaction
@ -38,10 +38,11 @@ if TYPE_CHECKING:
from ..components import Component from ..components import Component
I = TypeVar('I', bound='Item') I = TypeVar('I', bound='Item')
V = TypeVar('V', bound='View', covariant=True)
ItemCallbackType = Callable[[Any, I, Interaction], Coroutine[Any, Any, Any]] ItemCallbackType = Callable[[Any, I, Interaction], Coroutine[Any, Any, Any]]
class Item: class Item(Generic[V]):
"""Represents the base UI item that all UI components inherit from. """Represents the base UI item that all UI components inherit from.
The current UI items supported are: The current UI items supported are:
@ -52,7 +53,7 @@ class Item:
__item_repr_attributes__: Tuple[str, ...] = ('group_id',) __item_repr_attributes__: Tuple[str, ...] = ('group_id',)
def __init__(self): def __init__(self):
self._view: Optional[View] = None self._view: Optional[V] = None
self.group_id: Optional[int] = None self.group_id: Optional[int] = None
def to_component_dict(self) -> Dict[str, Any]: def to_component_dict(self) -> Dict[str, Any]:
@ -77,7 +78,7 @@ class Item:
return f'<{self.__class__.__name__} {attrs}>' return f'<{self.__class__.__name__} {attrs}>'
@property @property
def view(self) -> Optional[View]: def view(self) -> Optional[V]:
"""Optional[:class:`View`]: The underlying view for this item.""" """Optional[:class:`View`]: The underlying view for this item."""
return self._view return self._view

Loading…
Cancel
Save