Skip to content

Slash command option types are broken by stringized annotations #513

Closed
@toBeOfUse

Description

@toBeOfUse

Summary

When specifying slash command option types in a module that uses from __future__ import annotations, which stringizes annotations, a crash occurs.

Reproduction Steps

First of all, the current examples for the slash commands doesn't tell you to use type annotations for slash command option parameters, which is required to make them work. After adding an annotation to create an int-type option, my bot sadly crashed because it called issubclass('int', str) in SlashCommandOptionType.from_datatype in discord/enums.py. Clearly, that function expected int not 'int'. I believe that the SlashCommandOptionType.from_datatype method is receiving parameters obtained by the python inspect.signature(callable) function, which unstringizes parameters from modules that use from __future__ import annotations only if its eval_str keyword argument is explicitly set to True, which it isn't currently. EDIT: actually, the eval_str keyword argument appears to be only available in Python 3.10 and not Python 3.9, which might necessitate a manual workaround for earlier versions. Also, re-reading the examples, it was my mistake to use a raw int type annotation instead of an Option type annotation in the first place, although it Almost worked; not sure if that is intended to be supported or not. Anyway, the problem still exists if you use Option type annotations properly.

Minimal Reproducible Code

from __future__ import annotations
import discord
from discord.commands import Option

bot = discord.Bot()
@bot.slash_command()
async def hello(ctx, number: Option(int)):
    """Say hello to the bot"""
    await ctx.respond(f"Hello number {number}!")

bot.run("TOKEN")

Expected Results

I expected to make a slash command with a typed option.

Actual Results

Crashed. Full traceback:

Traceback (most recent call last):
  File "C:\Users\Extreme PC\.virtualenvs\mitchbot-YqVhPTxY\lib\site-packages\discord\client.py", line 352, in _run_event
    await coro(*args, **kwargs)
  File "C:\Users\Extreme PC\Code\mitchbot\MitchBot.py", line 37, in on_ready
    add_responses(self)
  File "C:\Users\Extreme PC\Code\mitchbot\responders.py", line 193, in add_responses
    async def get_nicknames(
  File "C:\Users\Extreme PC\.virtualenvs\mitchbot-YqVhPTxY\lib\site-packages\discord\bot.py", line 512, in decorator
    result = command(**kwargs)(func)
  File "C:\Users\Extreme PC\.virtualenvs\mitchbot-YqVhPTxY\lib\site-packages\discord\commands\commands.py", line 1110, in decorator
    return cls(func, **attrs)
  File "C:\Users\Extreme PC\.virtualenvs\mitchbot-YqVhPTxY\lib\site-packages\discord\commands\commands.py", line 393, in __init__
    self.options: List[Option] = kwargs.get('options') or self._parse_options(params)
  File "C:\Users\Extreme PC\.virtualenvs\mitchbot-YqVhPTxY\lib\site-packages\discord\commands\commands.py", line 449, in _parse_options
    option = Option(option, "No description provided")
  File "C:\Users\Extreme PC\.virtualenvs\mitchbot-YqVhPTxY\lib\site-packages\discord\commands\commands.py", line 618, in __init__
    _type = SlashCommandOptionType.from_datatype(input_type)
  File "C:\Users\Extreme PC\.virtualenvs\mitchbot-YqVhPTxY\lib\site-packages\discord\enums.py", line 643, in from_datatype
    if issubclass(datatype, str):
TypeError: issubclass() arg 1 must be a class

Intents

I am not passing a discord.Intents class to my client.

System Information

  • Python v3.9.1-final
  • py-cord v2.0.0-alpha
    • py-cord pkg_resources: v2.0.0a4477+g57ed00a0
  • aiohttp v3.8.1
  • system info: Windows 10 10.0.19041

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

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions