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: remove decorator check in InvokableUserCommand #1252

Merged
merged 5 commits into from
Nov 26, 2024
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/1252.bugfix.rst
Original file line number Diff line number Diff line change
@@ -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.
8 changes: 1 addition & 7 deletions disnake/ext/commands/ctx_menus_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
47 changes: 32 additions & 15 deletions tests/ext/commands/test_base_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
...
Expand All @@ -29,52 +43,55 @@ 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:
...

# 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):
@commands.slash_command(default_member_permissions=64)
@meta.decorator(default_member_permissions=64)
@commands.default_member_permissions(32)
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:
...

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)

Expand Down
Loading