From 7d14837fbb94c4355bd3b59358d2bea6e999d3ba Mon Sep 17 00:00:00 2001 From: shiftinv <8530778+shiftinv@users.noreply.github.com> Date: Fri, 8 Dec 2023 15:55:11 +0100 Subject: [PATCH 1/2] fix: avoid incorrect warning about broken localizations in cogs (#1133) --- changelog/1133.bugfix.rst | 1 + disnake/i18n.py | 7 ++++++- tests/ext/commands/test_base_core.py | 18 ++++++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 changelog/1133.bugfix.rst diff --git a/changelog/1133.bugfix.rst b/changelog/1133.bugfix.rst new file mode 100644 index 0000000000..fdf45729eb --- /dev/null +++ b/changelog/1133.bugfix.rst @@ -0,0 +1 @@ +|commands| Fix erroneous :class:`LocalizationWarning`\s when using localized slash command parameters in cogs. diff --git a/disnake/i18n.py b/disnake/i18n.py index c2781a9eb8..9a9ca32560 100644 --- a/disnake/i18n.py +++ b/disnake/i18n.py @@ -236,8 +236,9 @@ def _copy(self) -> LocalizationValue: def data(self) -> Optional[Dict[str, str]]: """Optional[Dict[:class:`str`, :class:`str`]]: A dict with a locale -> localization mapping, if available.""" if self._data is MISSING: + # This will happen when `_link(store)` hasn't been called yet, which *shouldn't* occur under normal circumstances. warnings.warn( - f"value ('{self._key}') was never localized, this is likely a library bug", + f"Localization value ('{self._key}') was never linked to bot; this may be a library bug.", LocalizationWarning, stacklevel=2, ) @@ -245,6 +246,10 @@ def data(self) -> Optional[Dict[str, str]]: return self._data def __eq__(self, other) -> bool: + # if both are pending, compare keys instead + if self._data is MISSING and other._data is MISSING: + return self._key == other._key + d1 = self.data d2 = other.data # consider values equal if they're both falsy, or actually equal diff --git a/tests/ext/commands/test_base_core.py b/tests/ext/commands/test_base_core.py index ccb15f42fc..734353c5a4 100644 --- a/tests/ext/commands/test_base_core.py +++ b/tests/ext/commands/test_base_core.py @@ -2,6 +2,7 @@ import pytest +import disnake from disnake import Permissions from disnake.ext import commands @@ -82,3 +83,20 @@ async def overwrite_decorator_below(self, _) -> None: assert Cog.overwrite_decorator_below.default_member_permissions == Permissions(64) assert Cog().overwrite_decorator_below.default_member_permissions == Permissions(64) + + +def test_localization_copy() -> None: + class Cog(commands.Cog): + @commands.slash_command() + async def cmd( + self, + inter, + param: int = commands.Param(name=disnake.Localized("param", key="PARAM")), + ) -> None: + ... + + # Ensure the command copy that happens on cog init doesn't raise a LocalizationWarning for the options. + cog = Cog() + + with pytest.warns(disnake.LocalizationWarning): + assert cog.get_slash_commands()[0].options[0].name_localizations.data is None From f780cf5d7320210f022eba0d0e4e8b2820db2baa Mon Sep 17 00:00:00 2001 From: shiftinv <8530778+shiftinv@users.noreply.github.com> Date: Sat, 9 Dec 2023 11:57:18 +0100 Subject: [PATCH 2/2] feat(typing): update send method types to support `StandardSticker` (#1134) --- changelog/1134.misc.rst | 1 + disnake/abc.py | 14 +++++++------- disnake/channel.py | 14 +++++++------- disnake/webhook/async_.py | 6 +++--- 4 files changed, 18 insertions(+), 17 deletions(-) create mode 100644 changelog/1134.misc.rst diff --git a/changelog/1134.misc.rst b/changelog/1134.misc.rst new file mode 100644 index 0000000000..664f4a332d --- /dev/null +++ b/changelog/1134.misc.rst @@ -0,0 +1 @@ +Add :class:`StandardSticker` to ``stickers`` parameter type annotation of :meth:`Messageable.send` and :meth:`ForumChannel.create_thread`. diff --git a/disnake/abc.py b/disnake/abc.py index a6f84163ac..fda31ec5fb 100644 --- a/disnake/abc.py +++ b/disnake/abc.py @@ -42,7 +42,7 @@ from .partial_emoji import PartialEmoji from .permissions import PermissionOverwrite, Permissions from .role import Role -from .sticker import GuildSticker, StickerItem +from .sticker import GuildSticker, StandardSticker, StickerItem from .ui.action_row import components_to_dict from .utils import _overload_with_permissions from .voice_client import VoiceClient, VoiceProtocol @@ -1429,7 +1429,7 @@ async def send( tts: bool = ..., embed: Embed = ..., file: File = ..., - stickers: Sequence[Union[GuildSticker, StickerItem]] = ..., + stickers: Sequence[Union[GuildSticker, StandardSticker, StickerItem]] = ..., delete_after: float = ..., nonce: Union[str, int] = ..., suppress_embeds: bool = ..., @@ -1450,7 +1450,7 @@ async def send( tts: bool = ..., embed: Embed = ..., files: List[File] = ..., - stickers: Sequence[Union[GuildSticker, StickerItem]] = ..., + stickers: Sequence[Union[GuildSticker, StandardSticker, StickerItem]] = ..., delete_after: float = ..., nonce: Union[str, int] = ..., suppress_embeds: bool = ..., @@ -1471,7 +1471,7 @@ async def send( tts: bool = ..., embeds: List[Embed] = ..., file: File = ..., - stickers: Sequence[Union[GuildSticker, StickerItem]] = ..., + stickers: Sequence[Union[GuildSticker, StandardSticker, StickerItem]] = ..., delete_after: float = ..., nonce: Union[str, int] = ..., suppress_embeds: bool = ..., @@ -1492,7 +1492,7 @@ async def send( tts: bool = ..., embeds: List[Embed] = ..., files: List[File] = ..., - stickers: Sequence[Union[GuildSticker, StickerItem]] = ..., + stickers: Sequence[Union[GuildSticker, StandardSticker, StickerItem]] = ..., delete_after: float = ..., nonce: Union[str, int] = ..., suppress_embeds: bool = ..., @@ -1514,7 +1514,7 @@ async def send( embeds: Optional[List[Embed]] = None, file: Optional[File] = None, files: Optional[List[File]] = None, - stickers: Optional[Sequence[Union[GuildSticker, StickerItem]]] = None, + stickers: Optional[Sequence[Union[GuildSticker, StandardSticker, StickerItem]]] = None, delete_after: Optional[float] = None, nonce: Optional[Union[str, int]] = None, suppress_embeds: Optional[bool] = None, @@ -1567,7 +1567,7 @@ async def send( files: List[:class:`.File`] A list of files to upload. Must be a maximum of 10. This cannot be mixed with the ``file`` parameter. - stickers: Sequence[Union[:class:`.GuildSticker`, :class:`.StickerItem`]] + stickers: Sequence[Union[:class:`.GuildSticker`, :class:`.StandardSticker`, :class:`.StickerItem`]] A list of stickers to upload. Must be a maximum of 3. .. versionadded:: 2.0 diff --git a/disnake/channel.py b/disnake/channel.py index 00598927ca..042611b563 100644 --- a/disnake/channel.py +++ b/disnake/channel.py @@ -74,7 +74,7 @@ from .message import AllowedMentions, Message, PartialMessage from .role import Role from .state import ConnectionState - from .sticker import GuildSticker, StickerItem + from .sticker import GuildSticker, StandardSticker, StickerItem from .threads import AnyThreadArchiveDuration, ThreadType from .types.channel import ( CategoryChannel as CategoryChannelPayload, @@ -3791,7 +3791,7 @@ async def create_thread( file: File = ..., suppress_embeds: bool = ..., flags: MessageFlags = ..., - stickers: Sequence[Union[GuildSticker, StickerItem]] = ..., + stickers: Sequence[Union[GuildSticker, StandardSticker, StickerItem]] = ..., allowed_mentions: AllowedMentions = ..., view: View = ..., components: Components = ..., @@ -3812,7 +3812,7 @@ async def create_thread( files: List[File] = ..., suppress_embeds: bool = ..., flags: MessageFlags = ..., - stickers: Sequence[Union[GuildSticker, StickerItem]] = ..., + stickers: Sequence[Union[GuildSticker, StandardSticker, StickerItem]] = ..., allowed_mentions: AllowedMentions = ..., view: View = ..., components: Components = ..., @@ -3833,7 +3833,7 @@ async def create_thread( file: File = ..., suppress_embeds: bool = ..., flags: MessageFlags = ..., - stickers: Sequence[Union[GuildSticker, StickerItem]] = ..., + stickers: Sequence[Union[GuildSticker, StandardSticker, StickerItem]] = ..., allowed_mentions: AllowedMentions = ..., view: View = ..., components: Components = ..., @@ -3854,7 +3854,7 @@ async def create_thread( files: List[File] = ..., suppress_embeds: bool = ..., flags: MessageFlags = ..., - stickers: Sequence[Union[GuildSticker, StickerItem]] = ..., + stickers: Sequence[Union[GuildSticker, StandardSticker, StickerItem]] = ..., allowed_mentions: AllowedMentions = ..., view: View = ..., components: Components = ..., @@ -3876,7 +3876,7 @@ async def create_thread( files: List[File] = MISSING, suppress_embeds: bool = MISSING, flags: MessageFlags = MISSING, - stickers: Sequence[Union[GuildSticker, StickerItem]] = MISSING, + stickers: Sequence[Union[GuildSticker, StandardSticker, StickerItem]] = MISSING, allowed_mentions: AllowedMentions = MISSING, view: View = MISSING, components: Components[MessageUIComponent] = MISSING, @@ -3940,7 +3940,7 @@ async def create_thread( files: List[:class:`.File`] A list of files to upload. Must be a maximum of 10. This cannot be mixed with the ``file`` parameter. - stickers: Sequence[Union[:class:`.GuildSticker`, :class:`.StickerItem`]] + stickers: Sequence[Union[:class:`.GuildSticker`, :class:`.StandardSticker`, :class:`.StickerItem`]] A list of stickers to upload. Must be a maximum of 3. allowed_mentions: :class:`.AllowedMentions` Controls the mentions being processed in this message. If this is diff --git a/disnake/webhook/async_.py b/disnake/webhook/async_.py index 4bd6f4b2d1..851a88dfee 100644 --- a/disnake/webhook/async_.py +++ b/disnake/webhook/async_.py @@ -63,7 +63,7 @@ from ..mentions import AllowedMentions from ..message import Attachment from ..state import ConnectionState - from ..sticker import GuildSticker, StickerItem + from ..sticker import GuildSticker, StandardSticker, StickerItem from ..types.message import Message as MessagePayload from ..types.webhook import Webhook as WebhookPayload from ..ui.action_row import Components @@ -491,7 +491,7 @@ def handle_message_parameters_dict( components: Optional[Components[MessageUIComponent]] = MISSING, allowed_mentions: Optional[AllowedMentions] = MISSING, previous_allowed_mentions: Optional[AllowedMentions] = None, - stickers: Sequence[Union[GuildSticker, StickerItem]] = MISSING, + stickers: Sequence[Union[GuildSticker, StandardSticker, StickerItem]] = MISSING, thread_name: Optional[str] = None, ) -> DictPayloadParameters: if files is not MISSING and file is not MISSING: @@ -578,7 +578,7 @@ def handle_message_parameters( components: Optional[Components[MessageUIComponent]] = MISSING, allowed_mentions: Optional[AllowedMentions] = MISSING, previous_allowed_mentions: Optional[AllowedMentions] = None, - stickers: Sequence[Union[GuildSticker, StickerItem]] = MISSING, + stickers: Sequence[Union[GuildSticker, StandardSticker, StickerItem]] = MISSING, thread_name: Optional[str] = None, ) -> PayloadParameters: params = handle_message_parameters_dict(