From 35d1a6cd64131ca40602ee2f464cfdf3ff75503b Mon Sep 17 00:00:00 2001 From: Lumouille <144063653+Lumabots@users.noreply.github.com> Date: Sat, 30 Aug 2025 18:27:34 +0200 Subject: [PATCH 1/2] fix: improve handling of Union and tuple types in option input_type validation --- discord/commands/options.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/discord/commands/options.py b/discord/commands/options.py index a88807f893..311cded5bf 100644 --- a/discord/commands/options.py +++ b/discord/commands/options.py @@ -372,12 +372,15 @@ def _strip_none_type(input_type): if input_type is type(None): raise TypeError("Option type cannot be only NoneType") - if isinstance(input_type, (types.UnionType, tuple)): - args = ( - get_args(input_type) - if isinstance(input_type, types.UnionType) - else input_type - ) + args = () + if isinstance(input_type, types.UnionType): + args = get_args(input_type) + elif getattr(input_type, "__origin__", None) is Union: + args = get_args(input_type) + elif isinstance(input_type, tuple): + args = input_type + + if args: filtered = tuple(t for t in args if t is not type(None)) if not filtered: raise TypeError("Option type cannot be only NoneType") From 8141994937eec31af27f055ce2412653f62a77f0 Mon Sep 17 00:00:00 2001 From: Lala Sabathil Date: Sat, 30 Aug 2025 18:58:49 +0200 Subject: [PATCH 2/2] fix: Option type handling for multiple Literals Updates Option type resolution to return a Union of Literals when all filtered types are Literal, ensuring correct type annotation for command options. --- discord/commands/options.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/discord/commands/options.py b/discord/commands/options.py index 311cded5bf..eb8ec6ecfc 100644 --- a/discord/commands/options.py +++ b/discord/commands/options.py @@ -386,8 +386,9 @@ def _strip_none_type(input_type): raise TypeError("Option type cannot be only NoneType") if len(filtered) == 1: return filtered[0] - return filtered - + if all(getattr(t, "__origin__", None) is Literal for t in filtered): + return Union[filtered] + return Union[filtered] return input_type def to_dict(self) -> dict: