From 1a96124511a10a57614b1b28749092ca8b35b14c Mon Sep 17 00:00:00 2001 From: shiftinv Date: Tue, 26 Nov 2024 15:51:58 +0100 Subject: [PATCH 1/4] test: test all slash/user/message command decorators --- tests/ext/commands/test_base_core.py | 43 +++++++++++++++++++--------- 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/tests/ext/commands/test_base_core.py b/tests/ext/commands/test_base_core.py index 734353c5a4..653474abe2 100644 --- a/tests/ext/commands/test_base_core.py +++ b/tests/ext/commands/test_base_core.py @@ -7,19 +7,33 @@ from disnake.ext import commands +class DecoratorMeta: + def __init__(self, type: str) -> None: + self.decorator = { + "slash": commands.slash_command, + "user": commands.user_command, + "message": commands.message_command, + }[type] + self.attr_key = f"{type}_command_attrs" + + class TestDefaultPermissions: - def test_decorator(self) -> None: + @pytest.fixture(params=["slash", "user", "message"]) + def meta(self, request): + return DecoratorMeta(request.param) + + def test_decorator(self, meta: DecoratorMeta) -> None: class Cog(commands.Cog): - @commands.slash_command(default_member_permissions=64) + @meta.decorator(default_member_permissions=64) async def cmd(self, _) -> None: ... @commands.default_member_permissions(64) - @commands.slash_command() + @meta.decorator() async def above(self, _) -> None: ... - @commands.slash_command() + @meta.decorator() @commands.default_member_permissions(64) async def below(self, _) -> None: ... @@ -29,13 +43,13 @@ async def below(self, _) -> None: assert c.above.default_member_permissions == Permissions(64) assert c.below.default_member_permissions == Permissions(64) - def test_decorator_overwrite(self) -> None: + def test_decorator_overwrite(self, meta: DecoratorMeta) -> None: # putting the decorator above should fail with pytest.raises(ValueError, match="Cannot set `default_member_permissions`"): class Cog(commands.Cog): @commands.default_member_permissions(32) - @commands.slash_command(default_member_permissions=64) + @meta.decorator(default_member_permissions=64) async def above(self, _) -> None: ... @@ -44,7 +58,7 @@ async def above(self, _) -> None: # and while this *should* probably fail, we're just testing # for regressions for now) class Cog2(commands.Cog): - @commands.slash_command(default_member_permissions=64) + @meta.decorator(default_member_permissions=64) @commands.default_member_permissions(32) async def below(self, _) -> None: ... @@ -52,22 +66,24 @@ async def below(self, _) -> None: for c in (Cog2, Cog2()): assert c.below.default_member_permissions == Permissions(32) - def test_attrs(self) -> None: - class Cog(commands.Cog, slash_command_attrs={"default_member_permissions": 32}): - @commands.slash_command() + def test_attrs(self, meta: DecoratorMeta) -> None: + kwargs = {meta.attr_key: {"default_member_permissions": 32}} + + class Cog(commands.Cog, **kwargs): + @meta.decorator() async def no_overwrite(self, _) -> None: ... - @commands.slash_command(default_member_permissions=64) + @meta.decorator(default_member_permissions=64) async def overwrite(self, _) -> None: ... @commands.default_member_permissions(64) - @commands.slash_command() + @meta.decorator() async def overwrite_decorator_above(self, _) -> None: ... - @commands.slash_command() + @meta.decorator() @commands.default_member_permissions(64) async def overwrite_decorator_below(self, _) -> None: ... @@ -75,6 +91,7 @@ async def overwrite_decorator_below(self, _) -> None: assert Cog.no_overwrite.default_member_permissions is None assert Cog().no_overwrite.default_member_permissions == Permissions(32) + # all of these should overwrite the cog-level attr assert Cog.overwrite.default_member_permissions == Permissions(64) assert Cog().overwrite.default_member_permissions == Permissions(64) From 16f3f65c0ec7be7fd4cabc6eb2e5b56114146d73 Mon Sep 17 00:00:00 2001 From: shiftinv Date: Tue, 26 Nov 2024 15:54:13 +0100 Subject: [PATCH 2/4] fix: remove decorator check in `InvokableUserCommand` --- disnake/ext/commands/ctx_menus_core.py | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/disnake/ext/commands/ctx_menus_core.py b/disnake/ext/commands/ctx_menus_core.py index 0e84b5a89d..9f59f03fc5 100644 --- a/disnake/ext/commands/ctx_menus_core.py +++ b/disnake/ext/commands/ctx_menus_core.py @@ -86,15 +86,9 @@ def __init__( self.auto_sync: bool = True if auto_sync is None else auto_sync try: - default_perms: int = func.__default_member_permissions__ + default_member_permissions = func.__default_member_permissions__ except AttributeError: pass - else: - if default_member_permissions is not None: - raise ValueError( - "Cannot set `default_member_permissions` in both parameter and decorator" - ) - default_member_permissions = default_perms dm_permission = True if dm_permission is None else dm_permission From 2bcce7a2e3e667a46e842fbe48c727ee0a1b6280 Mon Sep 17 00:00:00 2001 From: shiftinv Date: Tue, 26 Nov 2024 15:54:24 +0100 Subject: [PATCH 3/4] chore: add fixme --- tests/ext/commands/test_base_core.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/ext/commands/test_base_core.py b/tests/ext/commands/test_base_core.py index 653474abe2..bdedeb0fe0 100644 --- a/tests/ext/commands/test_base_core.py +++ b/tests/ext/commands/test_base_core.py @@ -53,8 +53,8 @@ class Cog(commands.Cog): async def above(self, _) -> None: ... - # putting the decorator below shouldn't fail - # (this is a side effect of how command copying works, + # putting the decorator below shouldn't fail, for now + # FIXME: (this is a side effect of how command copying works, # and while this *should* probably fail, we're just testing # for regressions for now) class Cog2(commands.Cog): From 076d8e234bcf3ba79f1351f6f40edec6ab9690d9 Mon Sep 17 00:00:00 2001 From: shiftinv Date: Tue, 26 Nov 2024 16:06:42 +0100 Subject: [PATCH 4/4] docs: add changelog entry --- changelog/1252.bugfix.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog/1252.bugfix.rst diff --git a/changelog/1252.bugfix.rst b/changelog/1252.bugfix.rst new file mode 100644 index 0000000000..f73d8d537d --- /dev/null +++ b/changelog/1252.bugfix.rst @@ -0,0 +1 @@ +|commands| Fix incorrect exception when using the :func:`~ext.commands.default_member_permissions` decorator on a :func:`~ext.commands.user_command` while also using the cog-level ``user_command_attrs`` field.