From b33090c2b521ecffabb697f002d1fce3ba38e0f2 Mon Sep 17 00:00:00 2001 From: Jonah Lawrence Date: Fri, 28 Jan 2022 11:52:23 -0700 Subject: [PATCH 1/5] feat: support format_page returning list of embeds --- nextcord/ext/menus/constants.py | 2 +- nextcord/ext/menus/menu_pages.py | 13 +++++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/nextcord/ext/menus/constants.py b/nextcord/ext/menus/constants.py index 801a525..284586b 100644 --- a/nextcord/ext/menus/constants.py +++ b/nextcord/ext/menus/constants.py @@ -15,7 +15,7 @@ SendKwargsType = Dict[str, Union[str, nextcord.Embed, nextcord.ui.View, None]] # type definition for possible page formats -PageFormatType = Union[str, nextcord.Embed, SendKwargsType] +PageFormatType = Union[str, nextcord.Embed, list[nextcord.Embed], SendKwargsType] # type definition for emoji parameters EmojiType = Union[str, nextcord.Emoji, nextcord.PartialEmoji] diff --git a/nextcord/ext/menus/menu_pages.py b/nextcord/ext/menus/menu_pages.py index d945dce..f70f4ec 100644 --- a/nextcord/ext/menus/menu_pages.py +++ b/nextcord/ext/menus/menu_pages.py @@ -84,9 +84,18 @@ async def _get_kwargs_from_page(self, page: List[Any]) -> SendKwargsType: if isinstance(value, dict): return value elif isinstance(value, str): - return {"content": value, "embed": None} + return {"content": value} elif isinstance(value, nextcord.Embed): - return {"embed": value, "content": None} + return {"embed": value} + elif isinstance(value, list) and all( + isinstance(v, nextcord.Embed) for v in value + ): + return {"embeds": value} + raise TypeError( + "Expected {0!r} not {1.__class__!r}.".format( + (dict, str, nextcord.Embed, list[nextcord.Embed]), value + ) + ) async def show_page(self, page_number: int): """|coro| From 7657287cb431eca5db8030f98997d0b96fb712ae Mon Sep 17 00:00:00 2001 From: Jonah Lawrence Date: Fri, 28 Jan 2022 11:57:23 -0700 Subject: [PATCH 2/5] fix: type annotation for Python 3.8 --- nextcord/ext/menus/menu_pages.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nextcord/ext/menus/menu_pages.py b/nextcord/ext/menus/menu_pages.py index f70f4ec..f7a181f 100644 --- a/nextcord/ext/menus/menu_pages.py +++ b/nextcord/ext/menus/menu_pages.py @@ -93,7 +93,7 @@ async def _get_kwargs_from_page(self, page: List[Any]) -> SendKwargsType: return {"embeds": value} raise TypeError( "Expected {0!r} not {1.__class__!r}.".format( - (dict, str, nextcord.Embed, list[nextcord.Embed]), value + (dict, str, nextcord.Embed, List[nextcord.Embed]), value ) ) From 0d72519481dd2853b2daa68ac4a2200c41d62bf6 Mon Sep 17 00:00:00 2001 From: Jonah Lawrence Date: Fri, 28 Jan 2022 11:58:23 -0700 Subject: [PATCH 3/5] fix: type annotation for Python 3.8 --- nextcord/ext/menus/constants.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nextcord/ext/menus/constants.py b/nextcord/ext/menus/constants.py index 284586b..de494f6 100644 --- a/nextcord/ext/menus/constants.py +++ b/nextcord/ext/menus/constants.py @@ -1,5 +1,5 @@ import logging -from typing import Dict, Union +from typing import Dict, List, Union import nextcord @@ -15,7 +15,7 @@ SendKwargsType = Dict[str, Union[str, nextcord.Embed, nextcord.ui.View, None]] # type definition for possible page formats -PageFormatType = Union[str, nextcord.Embed, list[nextcord.Embed], SendKwargsType] +PageFormatType = Union[str, nextcord.Embed, List[nextcord.Embed], SendKwargsType] # type definition for emoji parameters EmojiType = Union[str, nextcord.Emoji, nextcord.PartialEmoji] From 421d65db11b730a75ebe6eb666c94151e3e83c86 Mon Sep 17 00:00:00 2001 From: Jonah Lawrence Date: Sun, 30 Jan 2022 18:42:49 -0700 Subject: [PATCH 4/5] docs: Document change to format_page --- docs/ext/menus/reaction_menus.rst | 3 ++- nextcord/ext/menus/page_source.py | 12 ++++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/docs/ext/menus/reaction_menus.rst b/docs/ext/menus/reaction_menus.rst index e23a60c..8303450 100644 --- a/docs/ext/menus/reaction_menus.rst +++ b/docs/ext/menus/reaction_menus.rst @@ -131,5 +131,6 @@ For the sake of example, here’s a basic list source that is paginated: await pages.start(ctx) The :meth:`PageSource.format_page` can return either a :class:`str` for content, -:class:`nextcord.Embed` for an embed, or a :class:`dict` to pass into the kwargs +:class:`nextcord.Embed` for an embed, :class:`List[nextcord.Embed]` for +sending multiple embeds, or a :class:`dict` to pass into the kwargs of :meth:`nextcord.Message.edit`. diff --git a/nextcord/ext/menus/page_source.py b/nextcord/ext/menus/page_source.py index f9cd896..18ed600 100644 --- a/nextcord/ext/menus/page_source.py +++ b/nextcord/ext/menus/page_source.py @@ -112,7 +112,7 @@ async def format_page(self, menu: Menu, page: Any) -> PageFormatType: This method must return one of the following types. - If this method returns a ``str`` then it is interpreted as returning + If this method returns a :class:`str` then it is interpreted as returning the ``content`` keyword argument in :meth:`nextcord.Message.edit` and :meth:`nextcord.abc.Messageable.send`. @@ -120,10 +120,14 @@ async def format_page(self, menu: Menu, page: Any) -> PageFormatType: as returning the ``embed`` keyword argument in :meth:`nextcord.Message.edit` and :meth:`nextcord.abc.Messageable.send`. - If this method returns a ``dict`` then it is interpreted as the + If this method returns a :class:`List[nextcord.Embed]` then it is interpreted + as returning the ``embeds`` keyword argument in :meth:`nextcord.Message.edit` + and :meth:`nextcord.abc.Messageable.send`. + + If this method returns a :class:`dict` then it is interpreted as the keyword-arguments that are used in both :meth:`nextcord.Message.edit` - and :meth:`nextcord.abc.Messageable.send`. The two of interest are - ``embed`` and ``content``. + and :meth:`nextcord.abc.Messageable.send`. A few of interest are: + ``content``, ``embed``, ``embeds``, ``file``, ``files``. Parameters ------------ From 1ab8a287c9deba1851d487290a37f1391a7528a2 Mon Sep 17 00:00:00 2001 From: Jonah Lawrence Date: Sun, 30 Jan 2022 18:47:08 -0700 Subject: [PATCH 5/5] docs: Document TypeError --- nextcord/ext/menus/menu_pages.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/nextcord/ext/menus/menu_pages.py b/nextcord/ext/menus/menu_pages.py index f7a181f..62c6ac8 100644 --- a/nextcord/ext/menus/menu_pages.py +++ b/nextcord/ext/menus/menu_pages.py @@ -77,6 +77,13 @@ async def _get_kwargs_from_page(self, page: List[Any]) -> SendKwargsType: """|coro| Calls :meth:`PageSource.format_page` and returns a dict of send kwargs + + Raises + -------- + TypeError + The return value of :meth:`PageSource.format_page` was not a + :class:`str`, :class:`nextcord.Embed`, :class:`List[nextcord.Embed]`, + or :class:`dict`. """ value: PageFormatType = await nextcord.utils.maybe_coroutine( self._source.format_page, self, page @@ -332,6 +339,13 @@ async def show_page(self, page_number: int): async def _get_kwargs_from_page(self, page: List[Any]) -> SendKwargsType: """|coro| Calls :meth:`PageSource.format_page` and returns a dict of send kwargs + + Raises + -------- + TypeError + The return value of :meth:`PageSource.format_page` was not a + :class:`str`, :class:`nextcord.Embed`, :class:`List[nextcord.Embed]`, + or :class:`dict`. """ kwargs = await super()._get_kwargs_from_page(page) # add view to kwargs if it's not already there