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