Skip to content

Comments

fix: TypeError when using Literal type hint with option decorator#3123

Closed
FrederickStempfle wants to merge 2 commits intoPycord-Development:masterfrom
FrederickStempfle:fix/literal-option-type
Closed

fix: TypeError when using Literal type hint with option decorator#3123
FrederickStempfle wants to merge 2 commits intoPycord-Development:masterfrom
FrederickStempfle:fix/literal-option-type

Conversation

@FrederickStempfle
Copy link

@FrederickStempfle FrederickStempfle commented Feb 23, 2026

Summary

Fixes #3091

When a slash command option is typed as Literal["jpeg", "png", ...] without explicitly passing input_type, the Option init passes the Literal directly to SlashCommandOptionType.from_datatype, which calls issubclass() on it and crashes since Literal isn't a class.

The fix resolves Literal types to their base type (e.g. Literal["a", "b"]str) early in Option.__init__, before it reaches from_datatype.

Information

  • This PR fixes an issue.
  • This PR adds something new (e.g. new method or parameters).
  • This PR is a breaking change (e.g. methods or parameters removed/renamed).
  • This PR is not a code change (e.g. documentation, README, typehinting,
    examples, ...).

Checklist

  • I have searched the open pull requests for duplicates.
  • If code changes were made then they have been tested.
    • I have updated the documentation to reflect the changes.
  • If type: ignore comments were used, a comment is also left explaining why.
  • I have updated the changelog to include these changes.

@FrederickStempfle FrederickStempfle requested a review from a team as a code owner February 23, 2026 19:56
@pycord-app
Copy link

pycord-app bot commented Feb 23, 2026

Thanks for opening this pull request!
Please make sure you have read the Contributing Guidelines and Code of Conduct.

This pull request can be checked-out with:

git fetch origin pull/3123/head:pr-3123
git checkout pr-3123

This pull request can be installed with:

pip install git+https://github.com/Pycord-Development/pycord@refs/pull/3123/head

@plun1331
Copy link
Member

Use the pull request template please: https://github.com/Pycord-Development/.github/blob/main/PULL_REQUEST_TEMPLATE.md

@plun1331 plun1331 added bug Something isn't working priority: low Low Priority hold: changelog This pull request is missing a changelog entry hold: testing This pull request requires further testing labels Feb 23, 2026
@plun1331
Copy link
Member

  • Your checks are failing
  • A changelog is needed
  • Please test the fix and provide test code

@plun1331 plun1331 added bug Something isn't working and removed bug Something isn't working labels Feb 23, 2026
@FrederickStempfle
Copy link
Author

Hey, thanks for the review.

Checks: Those CI failures are infrastructure-related, not from this PR. They're failing the same way on master and every other open PR right now (jobs finishing in 3-4s with empty step logs). The Semantic Title one is on me though, I'll update the title to fix: format.

Changelog: My bad, will add it.

Test code: Here's what I tested with (same pattern as the existing test_typing_annotated.py tests):

from typing import Literal
from discord.commands.core import SlashCommand
from discord import Bot, SlashCommandOptionType
from discord.commands import option

@option("fmt", choices=["jpeg", "png", "gif", "webp", "tiff", "bmp"])
async def convert(ctx, fmt: Literal["jpeg", "png", "gif", "webp", "tiff", "bmp"]):
    await ctx.respond(fmt)

cmd = SlashCommand(convert)
bot = Bot()
bot.add_application_command(cmd)

opt = cmd.to_dict()["options"][0]
assert opt["type"] == SlashCommandOptionType.string.value

Without the fix this throws TypeError: issubclass() arg 1 must be a class in from_datatype, with the fix it resolves the Literal to str and passes.

Will push the title fix + changelog shortly.

@FrederickStempfle FrederickStempfle changed the title Fix TypeError when using Literal type hint with option decorator fix: TypeError when using Literal type hint with option decorator Feb 23, 2026
@plun1331
Copy link
Member

After some internal discussion, we're closing this as it appears it was made using AI. If we're wrong, feel free to let us know in our Discord: https://discord.gg/pycord

@plun1331 plun1331 closed this Feb 23, 2026
@Pycord-Development Pycord-Development locked as spam and limited conversation to collaborators Feb 23, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

bug Something isn't working hold: changelog This pull request is missing a changelog entry hold: testing This pull request requires further testing priority: low Low Priority

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Passing choices= to an option and typing it as Literal fails

2 participants