Skip to content

Commit

Permalink
fix: avoid incorrect warning about broken localizations in cogs (#1133)
Browse files Browse the repository at this point in the history
  • Loading branch information
shiftinv committed Dec 8, 2023
1 parent 85cf393 commit 7d14837
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/1133.bugfix.rst
@@ -0,0 +1 @@
|commands| Fix erroneous :class:`LocalizationWarning`\s when using localized slash command parameters in cogs.
7 changes: 6 additions & 1 deletion disnake/i18n.py
Expand Up @@ -236,15 +236,20 @@ 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,
)
return None
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
Expand Down
18 changes: 18 additions & 0 deletions tests/ext/commands/test_base_core.py
Expand Up @@ -2,6 +2,7 @@

import pytest

import disnake
from disnake import Permissions
from disnake.ext import commands

Expand Down Expand Up @@ -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

0 comments on commit 7d14837

Please sign in to comment.