Skip to content

Commit a69e354

Browse files
LumabotsLulalaby
authored andcommitted
fix: improve handling of types in option input_type validation (Pycord-Development#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]> (cherry picked from commit d0937be)
1 parent 5051530 commit a69e354

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
@@ -336,19 +336,23 @@ def _strip_none_type(input_type):
336336
if input_type is type(None):
337337
raise TypeError("Option type cannot be only NoneType")
338338

339-
if isinstance(input_type, (types.UnionType, tuple)):
340-
args = (
341-
get_args(input_type)
342-
if isinstance(input_type, types.UnionType)
343-
else input_type
344-
)
339+
args = ()
340+
if isinstance(input_type, types.UnionType):
341+
args = get_args(input_type)
342+
elif getattr(input_type, "__origin__", None) is Union:
343+
args = get_args(input_type)
344+
elif isinstance(input_type, tuple):
345+
args = input_type
346+
347+
if args:
345348
filtered = tuple(t for t in args if t is not type(None))
346349
if not filtered:
347350
raise TypeError("Option type cannot be only NoneType")
348351
if len(filtered) == 1:
349352
return filtered[0]
350-
return filtered
351-
353+
if all(getattr(t, "__origin__", None) is Literal for t in filtered):
354+
return Union[filtered]
355+
return Union[filtered]
352356
return input_type
353357

354358
def to_dict(self) -> dict:

0 commit comments

Comments
 (0)