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

AttributeError: type object '<type>' has no attribute 'value' when using typing.Annotated #2223

Closed
3 tasks done
nexy7574 opened this issue Sep 2, 2023 · 0 comments · Fixed by #2243
Closed
3 tasks done
Labels
unconfirmed bug A bug report that needs triaging

Comments

@nexy7574
Copy link
Contributor

nexy7574 commented Sep 2, 2023

Summary

When using typing.Annotated with discord.Option, the former argument is not respected, and instead raises an error unless it is of type discord.SlashCommandOptionType.

Reproduction Steps

  1. Use typing.Annotated to annotate an argument
  2. Set the first value of Annotated to int (or any other supported type, but I only tested with int and bool)
  3. Set the second value of Annotated to discord.Option
  4. Set the type of discord.Option to the same one you put for Annotated (i.e. int, bool, etc)
  5. Fill in the rest of discord.Option values (I use name, description, required=False, and default=3 in my instance)
  6. Start the bot, ensuring the command is added
  7. Watch as the sync task fails in on_connect due to to_dict() raising AttributeError

Minimal Reproducible Code

import typing
import discord
from discord.ext import commands

bot = commands.Bot()


@bot.slash_command()
async def my_command(ctx: discord.ApplicationContext, my_option: typing.Annotated[int, discord.Option(int, name="my-option", description="This is my option!")]):
    await ctx.respond("Hey, %d is my option!" % my_option)

Expected Results

I expected the first value of Annotated to be ignored, really, prioritising the one passed to discord.Option itself. However, regardless, the type is not converted to a suitable discord.SlashCommandOptionType in Option.to_dict.

Actual Results

A traceback is thrown while trying to sync commands:

Sep 02 02:30:53 pi python3[318641]: Ignoring exception in on_connect
 Traceback (most recent call last):
   File "/home/pi/Bots/server-bot/venv/lib/python3.11/site-packages/discord/client.py", line 399, in _run_event
     await coro(*args, **kwargs)
   File "/home/pi/Bots/server-bot/venv/lib/python3.11/site-packages/discord/bot.py", line 1182, in on_connect
     await self.sync_commands()
   File "/home/pi/Bots/server-bot/venv/lib/python3.11/site-packages/discord/bot.py", line 757, in sync_commands
     app_cmds = await self.register_commands(
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/home/pi/Bots/server-bot/venv/lib/python3.11/site-packages/discord/bot.py", line 591, in register_commands
     data = [cmd["command"].to_dict() for cmd in filtered_deleted]
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/home/pi/Bots/server-bot/venv/lib/python3.11/site-packages/discord/bot.py", line 591, in <listcomp>
     data = [cmd["command"].to_dict() for cmd in filtered_deleted]
             ^^^^^^^^^^^^^^^^^^^^^^^^
   File "/home/pi/Bots/server-bot/venv/lib/python3.11/site-packages/discord/commands/core.py", line 866, in to_dict
     "options": [o.to_dict() for o in self.options],
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/home/pi/Bots/server-bot/venv/lib/python3.11/site-packages/discord/commands/core.py", line 866, in <listcomp>
     "options": [o.to_dict() for o in self.options],
                 ^^^^^^^^^^^
   File "/home/pi/Bots/server-bot/venv/lib/python3.11/site-packages/discord/commands/options.py", line 336, in to_dict
     "type": self.input_type.value,
             ^^^^^^^^^^^^^^^^^^^^^
 AttributeError: type object 'int' has no attribute 'value'

Intents

All

System Information

Not important, however, repro on two machines:
Production (server):

- Python v3.11.0-final
- py-cord v2.4.1-final
- aiohttp v3.8.5
- system info: Linux 6.1.21-v8+ #1642 SMP PREEMPT Mon Apr  3 17:24:16 BST 2023

and dev (workstation):

DeprecationWarning: pkg_resources is deprecated as an API. See https://setuptools.pypa.io/en/latest/pkg_resources.html
  import pkg_resources
- Python v3.11.5-final
- py-cord v2.4.1-final
- aiohttp v3.8.5
- system info: Linux 6.4.12-arch1-1 #1 SMP PREEMPT_DYNAMIC Thu, 24 Aug 2023 00:38:14 +0000

Checklist

  • I have searched the open issues for duplicates.
  • I have shown the entire traceback, if possible.
  • I have removed my token from display, if visible.

Additional Context

Conversation in Basic Pycord Help thread:

I am not using the development release, I installed straight from GitHub. I updated before making this issue, and the problem persists.

@nexy7574 nexy7574 added the unconfirmed bug A bug report that needs triaging label Sep 2, 2023
@Dorukyum Dorukyum linked a pull request Nov 26, 2023 that will close this issue
9 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
unconfirmed bug A bug report that needs triaging
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant