Skip to content

Commit d0937be

Browse files
LumabotsLulalaby
andauthored
fix: improve handling of types in option input_type validation (#2893)
* fix: improve handling of Union and tuple types in option input_type validation * 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. --------- Co-authored-by: Lala Sabathil <[email protected]>
1 parent 0ed71b6 commit d0937be

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

discord/commands/options.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -372,19 +372,23 @@ def _strip_none_type(input_type):
372372
if input_type is type(None):
373373
raise TypeError("Option type cannot be only NoneType")
374374

375-
if isinstance(input_type, (types.UnionType, tuple)):
376-
args = (
377-
get_args(input_type)
378-
if isinstance(input_type, types.UnionType)
379-
else input_type
380-
)
375+
args = ()
376+
if isinstance(input_type, types.UnionType):
377+
args = get_args(input_type)
378+
elif getattr(input_type, "__origin__", None) is Union:
379+
args = get_args(input_type)
380+
elif isinstance(input_type, tuple):
381+
args = input_type
382+
383+
if args:
381384
filtered = tuple(t for t in args if t is not type(None))
382385
if not filtered:
383386
raise TypeError("Option type cannot be only NoneType")
384387
if len(filtered) == 1:
385388
return filtered[0]
386-
return filtered
387-
389+
if all(getattr(t, "__origin__", None) is Literal for t in filtered):
390+
return Union[filtered]
391+
return Union[filtered]
388392
return input_type
389393

390394
def to_dict(self) -> dict:

0 commit comments

Comments
 (0)