fix: TypeError when using Literal type hint with option decorator#3123
fix: TypeError when using Literal type hint with option decorator#3123FrederickStempfle wants to merge 2 commits intoPycord-Development:masterfrom
Conversation
|
Thanks for opening this pull request! This pull request can be checked-out with: git fetch origin pull/3123/head:pr-3123
git checkout pr-3123This pull request can be installed with: pip install git+https://github.com/Pycord-Development/pycord@refs/pull/3123/head |
|
Use the pull request template please: https://github.com/Pycord-Development/.github/blob/main/PULL_REQUEST_TEMPLATE.md |
|
|
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 Changelog: My bad, will add it. Test code: Here's what I tested with (same pattern as the existing 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.valueWithout the fix this throws Will push the title fix + changelog shortly. |
|
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 |
Summary
Fixes #3091
When a slash command option is typed as
Literal["jpeg", "png", ...]without explicitly passinginput_type, theOptioninit passes theLiteraldirectly toSlashCommandOptionType.from_datatype, which callsissubclass()on it and crashes sinceLiteralisn't a class.The fix resolves
Literaltypes to their base type (e.g.Literal["a", "b"]→str) early inOption.__init__, before it reachesfrom_datatype.Information
examples, ...).
Checklist
type: ignorecomments were used, a comment is also left explaining why.