Skip to content

Commit 660fa15

Browse files
committed
Update options.py
1 parent 7c598bc commit 660fa15

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

discord/commands/options.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
import inspect
2828
import logging
2929
from enum import Enum
30-
from typing import TYPE_CHECKING, Literal, Optional, Type, Union
30+
import types
31+
from typing import TYPE_CHECKING, Literal, Optional, Type, Union, get_args
3132

3233
from ..abc import GuildChannel, Mentionable
3334
from ..channel import (
@@ -196,6 +197,7 @@ def __init__(
196197
if self.name is not None:
197198
self.name = str(self.name)
198199
self._parameter_name = self.name # default
200+
input_type = self._strip_none_type(input_type)
199201
self._raw_type: InputType | tuple = input_type
200202

201203
enum_choices = []
@@ -365,6 +367,26 @@ def __init__(
365367
if input_type is None:
366368
raise TypeError("input_type cannot be NoneType.")
367369

370+
@staticmethod
371+
def _strip_none_type(input_type):
372+
if input_type is type(None):
373+
raise TypeError("Option type cannot be only NoneType")
374+
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+
)
381+
filtered = tuple(t for t in args if t is not type(None))
382+
if not filtered:
383+
raise TypeError("Option type cannot be only NoneType")
384+
if len(filtered) == 1:
385+
return filtered[0]
386+
return filtered
387+
388+
return input_type
389+
368390
def to_dict(self) -> dict:
369391
as_dict = {
370392
"name": self.name,

0 commit comments

Comments
 (0)