Skip to content

TypeError when typehinting command argument as typing.Optional using the @discord.Option decorator #2834

@akosjdlk-60

Description

@akosjdlk-60

Summary

title

Reproduction Steps

see mrc

Minimal Reproducible Code

import discord
import os
from dotenv import load_dotenv
load_dotenv()

bot = discord.Bot(intents=discord.Intents.all())


@bot.slash_command(name="test", description="Test command")
@discord.option(name="option", description="Test option", channel_types=[discord.ChannelType.text], required=False)
async def test(ctx: discord.ApplicationContext, option: discord.TextChannel | None = None):
    if option:
        await ctx.respond(f"Test command with channel: {option.name}")
    else:
        await ctx.respond("Test command without a channel.")

bot.run(os.getenv("TOKEN"))

Expected Results

No error

Actual Results

Traceback (most recent call last):
File "/home/akos/amox/tests/a.py", line 10, in
@discord.option(name="option", description="Test option", channel_types=[discord.ChannelType.text], required=False)
~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/akos/amox/.venv/lib/python3.13/site-packages/discord/commands/options.py", line 438, in decorator
func.annotations[resolved_name] = Option(itype, name=name, **kwargs)
~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/akos/amox/.venv/lib/python3.13/site-packages/discord/commands/options.py", line 243, in init
raise exc
File "/home/akos/amox/.venv/lib/python3.13/site-packages/discord/commands/options.py", line 238, in init
self.input_type = SlashCommandOptionType.from_datatype(input_type)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^
File "/home/akos/amox/.venv/lib/python3.13/site-packages/discord/enums.py", line 810, in from_datatype
return cls.from_datatype(datatype.args) # type: ignore
~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^
File "/home/akos/amox/.venv/lib/python3.13/site-packages/discord/enums.py", line 795, in from_datatype
datatypes = [cls.from_datatype(op) for op in datatype]
~~~~~~~~~~~~~~~~~^^^^
File "/home/akos/amox/.venv/lib/python3.13/site-packages/discord/enums.py", line 852, in from_datatype
raise TypeError(
^^^^^^^^^^^^^^^^
f"Invalid class {datatype} used as an input type for an Option"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
) # TODO: Improve the error message
^
TypeError: Invalid class <class 'NoneType'> used as an input type for an Option

Intents

any

System Information

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

https://discord.com/channels/881207955029110855/1132206148309749830/1398342377042612376

Metadata

Metadata

Assignees

No one assigned

    Labels

    unconfirmed bugA bug report that needs triaging

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions