From ed9badcddfc04270963dfe488132e93422d54c0c Mon Sep 17 00:00:00 2001 From: Rapptz Date: Mon, 26 Apr 2021 21:34:01 -0400 Subject: [PATCH] Make Item and Button generic over the underlying view --- discord/ui/button.py | 5 +++-- discord/ui/item.py | 9 +++++---- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/discord/ui/button.py b/discord/ui/button.py index 8ff4ce744..0d156a962 100644 --- a/discord/ui/button.py +++ b/discord/ui/button.py @@ -41,7 +41,7 @@ __all__ = ( ) if TYPE_CHECKING: - from ..components import Component + from .view import View _custom_emoji = re.compile(r'a)?:?(?P[A-Za-z0-9\_]+):(?P[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') +V = TypeVar('V', bound='View', covariant=True) -class Button(Item): +class Button(Item[V]): """Represents a UI button. .. versionadded:: 2.0 diff --git a/discord/ui/item.py b/discord/ui/item.py index dc6c91a0f..bce289aac 100644 --- a/discord/ui/item.py +++ b/discord/ui/item.py @@ -24,7 +24,7 @@ DEALINGS IN THE SOFTWARE. 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 @@ -38,10 +38,11 @@ if TYPE_CHECKING: from ..components import Component I = TypeVar('I', bound='Item') +V = TypeVar('V', bound='View', covariant=True) 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. The current UI items supported are: @@ -52,7 +53,7 @@ class Item: __item_repr_attributes__: Tuple[str, ...] = ('group_id',) def __init__(self): - self._view: Optional[View] = None + self._view: Optional[V] = None self.group_id: Optional[int] = None def to_component_dict(self) -> Dict[str, Any]: @@ -77,7 +78,7 @@ class Item: return f'<{self.__class__.__name__} {attrs}>' @property - def view(self) -> Optional[View]: + def view(self) -> Optional[V]: """Optional[:class:`View`]: The underlying view for this item.""" return self._view