Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: avoid incorrect warning about broken localizations in cogs #1133

Merged
merged 7 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/1133.bugfix.rst
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading