aboutsummaryrefslogtreecommitdiff
path: root/discord
diff options
context:
space:
mode:
authorRapptz <[email protected]>2021-04-26 21:34:01 -0400
committerRapptz <[email protected]>2021-05-27 00:53:14 -0400
commited9badcddfc04270963dfe488132e93422d54c0c (patch)
tree1b0d1f99c097d3fe40e8556980e69f75d55c1d90 /discord
parentChange the way callbacks are defined to allow deriving (diff)
downloaddiscord.py-ed9badcddfc04270963dfe488132e93422d54c0c.tar.xz
discord.py-ed9badcddfc04270963dfe488132e93422d54c0c.zip
Make Item and Button generic over the underlying view
Diffstat (limited to 'discord')
-rw-r--r--discord/ui/button.py5
-rw-r--r--discord/ui/item.py9
2 files changed, 8 insertions, 6 deletions
diff --git a/discord/ui/button.py b/discord/ui/button.py
index 8ff4ce74..0d156a96 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'<?(?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')
+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 dc6c91a0..bce289aa 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