From a04786a2c304d1adc6883b98806fbbbd35121b7c Mon Sep 17 00:00:00 2001 From: Mihitoko Date: Fri, 13 Oct 2023 14:46:05 +0200 Subject: [PATCH 1/6] use SlashCommandOptionType when parsing options --- discord/commands/core.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/discord/commands/core.py b/discord/commands/core.py index d53bac0d71..c7bd4efd40 100644 --- a/discord/commands/core.py +++ b/discord/commands/core.py @@ -746,10 +746,12 @@ def _parse_options(self, params, *, check_params: bool = True) -> list[Option]: option = next(option_gen, Option()) # Handle Optional if self._is_typing_optional(type_hint): - option.input_type = get_args(type_hint)[0] + option.input_type = SlashCommandOptionType.from_datatype( + get_args(type_hint)[0] + ) option.default = None else: - option.input_type = type_hint + option.input_type = SlashCommandOptionType.from_datatype(type_hint) if self._is_typing_union(option): if self._is_typing_optional(option): From 2d78f7b538f825df6a38d3c30a9276d10869aa5b Mon Sep 17 00:00:00 2001 From: Mihitoko Date: Fri, 13 Oct 2023 14:46:24 +0200 Subject: [PATCH 2/6] add assertions to test right serialization of command --- tests/test_typing_annotated.py | 48 +++++++++++++++++++++++++++++----- 1 file changed, 42 insertions(+), 6 deletions(-) diff --git a/tests/test_typing_annotated.py b/tests/test_typing_annotated.py index 582bd4f8a0..0ea4125f02 100644 --- a/tests/test_typing_annotated.py +++ b/tests/test_typing_annotated.py @@ -1,10 +1,9 @@ from typing import Optional -import pytest from typing_extensions import Annotated import discord -from discord import ApplicationContext +from discord import SlashCommandOptionType from discord.commands.core import SlashCommand, slash_command @@ -15,6 +14,8 @@ async def echo(ctx, txt: Annotated[str, discord.Option()]): cmd = SlashCommand(echo) bot = discord.Bot() bot.add_application_command(cmd) + dict_result = cmd.to_dict() + assert dict_result.get("options")[0].get("type") == SlashCommandOptionType.string.value def test_typing_annotated_decorator(): @@ -24,6 +25,12 @@ def test_typing_annotated_decorator(): async def echo(ctx, txt: Annotated[str, discord.Option(description="Some text")]): await ctx.respond(txt) + dict_result = echo.to_dict() + + option = dict_result.get("options")[0] + assert option.get("type") == SlashCommandOptionType.string.value + assert option.get("description") == "Some text" + def test_typing_annotated_cog(): class echoCog(discord.Cog): @@ -33,12 +40,19 @@ def __init__(self, bot_) -> None: @slash_command() async def echo( - self, ctx, txt: Annotated[str, discord.Option(description="Some text")] + self, ctx, txt: Annotated[str, discord.Option(description="Some text")] ): await ctx.respond(txt) bot = discord.Bot() - bot.add_cog(echoCog(bot)) + cog = echoCog(bot) + bot.add_cog(cog) + + dict_result = cog.echo.to_dict() + + option = dict_result.get("options")[0] + assert option.get("type") == SlashCommandOptionType.string.value + assert option.get("description") == "Some text" def test_typing_annotated_cog_slashgroup(): @@ -51,12 +65,19 @@ def __init__(self, bot_) -> None: @grp.command() async def echo( - self, ctx, txt: Annotated[str, discord.Option(description="Some text")] + self, ctx, txt: Annotated[str, discord.Option(description="Some text")] ): await ctx.respond(txt) bot = discord.Bot() - bot.add_cog(echoCog(bot)) + cog = echoCog(bot) + bot.add_cog(cog) + + dict_result = cog.echo.to_dict() + + option = dict_result.get("options")[0] + assert option.get("type") == SlashCommandOptionType.string.value + assert option.get("description") == "Some text" def test_typing_annotated_optional(): @@ -67,6 +88,11 @@ async def echo(ctx, txt: Annotated[Optional[str], discord.Option()]): bot = discord.Bot() bot.add_application_command(cmd) + dict_result = cmd.to_dict() + + option = dict_result.get("options")[0] + assert option.get("type") == SlashCommandOptionType.string.value + def test_no_annotation(): async def echo(ctx, txt: str): @@ -76,6 +102,11 @@ async def echo(ctx, txt: str): bot = discord.Bot() bot.add_application_command(cmd) + dict_result = cmd.to_dict() + + option = dict_result.get("options")[0] + assert option.get("type") == SlashCommandOptionType.string.value + def test_annotated_no_option(): async def echo(ctx, txt: Annotated[str, "..."]): @@ -84,3 +115,8 @@ async def echo(ctx, txt: Annotated[str, "..."]): cmd = SlashCommand(echo) bot = discord.Bot() bot.add_application_command(cmd) + + dict_result = cmd.to_dict() + + option = dict_result.get("options")[0] + assert option.get("type") == SlashCommandOptionType.string.value From f8b6fb32e82c8a34107ee87bd05d1e7b2e9976e6 Mon Sep 17 00:00:00 2001 From: Mihitoko Date: Fri, 13 Oct 2023 14:53:57 +0200 Subject: [PATCH 3/6] update CHANGELOG --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index bd7b286bce..cbbafae8dd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -699,6 +699,8 @@ These changes are available on the `master` branch, but have not yet been releas ([#1350](https://github.com/Pycord-Development/pycord/pull/1350)) - Make `TextChannel._get_channel` async. ([#1358](https://github.com/Pycord-Development/pycord/pull/1358)) +- Fix `AttributeError` when serializing commands with Annotated typehint + ([#2243](https://github.com/Pycord-Development/pycord/pull/2243)) ## [2.0.0-beta.7] - 2022-04-09 From f4e2f2e54fc82baab83e425b68e15c6542f3170e Mon Sep 17 00:00:00 2001 From: Mihitoko Date: Sat, 14 Oct 2023 12:25:40 +0200 Subject: [PATCH 4/6] put change into right place in CHANGELOG --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cbbafae8dd..89046fd268 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -178,6 +178,8 @@ These changes are available on the `master` branch, but have not yet been releas ([#2192](https://github.com/Pycord-Development/pycord/pull/2192)) - Fixed `DMChannel.recipient` being `None` and consequently `User.dm_channel` also being `None`. ([#2219](https://github.com/Pycord-Development/pycord/pull/2219)) +- Fix `AttributeError` when serializing commands with Annotated typehint + ([#2243](https://github.com/Pycord-Development/pycord/pull/2243)) ## [2.4.1] - 2023-03-20 @@ -699,8 +701,6 @@ These changes are available on the `master` branch, but have not yet been releas ([#1350](https://github.com/Pycord-Development/pycord/pull/1350)) - Make `TextChannel._get_channel` async. ([#1358](https://github.com/Pycord-Development/pycord/pull/1358)) -- Fix `AttributeError` when serializing commands with Annotated typehint - ([#2243](https://github.com/Pycord-Development/pycord/pull/2243)) ## [2.0.0-beta.7] - 2022-04-09 From 38a0dab0e0c522756ed943dc3d23f42b0c454dbf Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 14 Oct 2023 10:26:17 +0000 Subject: [PATCH 5/6] style(pre-commit): auto fixes from pre-commit.com hooks --- tests/test_typing_annotated.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tests/test_typing_annotated.py b/tests/test_typing_annotated.py index 0ea4125f02..7091a241ff 100644 --- a/tests/test_typing_annotated.py +++ b/tests/test_typing_annotated.py @@ -15,7 +15,9 @@ async def echo(ctx, txt: Annotated[str, discord.Option()]): bot = discord.Bot() bot.add_application_command(cmd) dict_result = cmd.to_dict() - assert dict_result.get("options")[0].get("type") == SlashCommandOptionType.string.value + assert ( + dict_result.get("options")[0].get("type") == SlashCommandOptionType.string.value + ) def test_typing_annotated_decorator(): @@ -40,7 +42,7 @@ def __init__(self, bot_) -> None: @slash_command() async def echo( - self, ctx, txt: Annotated[str, discord.Option(description="Some text")] + self, ctx, txt: Annotated[str, discord.Option(description="Some text")] ): await ctx.respond(txt) @@ -65,7 +67,7 @@ def __init__(self, bot_) -> None: @grp.command() async def echo( - self, ctx, txt: Annotated[str, discord.Option(description="Some text")] + self, ctx, txt: Annotated[str, discord.Option(description="Some text")] ): await ctx.respond(txt) From 00ee593249ac813cfced5a904172c73e7aefad76 Mon Sep 17 00:00:00 2001 From: Dorukyum <53639936+Dorukyum@users.noreply.github.com> Date: Wed, 29 Nov 2023 20:40:23 +0300 Subject: [PATCH 6/6] Update CHANGELOG.md Signed-off-by: Dorukyum <53639936+Dorukyum@users.noreply.github.com> --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 89046fd268..fd1f76929a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -178,7 +178,7 @@ These changes are available on the `master` branch, but have not yet been releas ([#2192](https://github.com/Pycord-Development/pycord/pull/2192)) - Fixed `DMChannel.recipient` being `None` and consequently `User.dm_channel` also being `None`. ([#2219](https://github.com/Pycord-Development/pycord/pull/2219)) -- Fix `AttributeError` when serializing commands with Annotated typehint +- Fixed `AttributeError` when serializing commands with `Annotated` type hints. ([#2243](https://github.com/Pycord-Development/pycord/pull/2243)) ## [2.4.1] - 2023-03-20