|
26 | 26 |
|
27 | 27 | import inspect
|
28 | 28 | import logging
|
| 29 | +import types |
29 | 30 | from enum import Enum
|
30 |
| -from typing import TYPE_CHECKING, Literal, Optional, Type, Union |
| 31 | +from typing import TYPE_CHECKING, Literal, Optional, Type, Union, get_args |
31 | 32 |
|
32 | 33 | from ..abc import GuildChannel, Mentionable
|
33 | 34 | from ..channel import (
|
@@ -193,6 +194,7 @@ def __init__(self, input_type: InputType = str, /, description: str | None = Non
|
193 | 194 | if self.name is not None:
|
194 | 195 | self.name = str(self.name)
|
195 | 196 | self._parameter_name = self.name # default
|
| 197 | + input_type = self._strip_none_type(input_type) |
196 | 198 | self._raw_type: InputType | tuple = input_type
|
197 | 199 |
|
198 | 200 | enum_choices = []
|
@@ -329,6 +331,26 @@ def __init__(self, input_type: InputType = str, /, description: str | None = Non
|
329 | 331 | if input_type is None:
|
330 | 332 | raise TypeError("input_type cannot be NoneType.")
|
331 | 333 |
|
| 334 | + @staticmethod |
| 335 | + def _strip_none_type(input_type): |
| 336 | + if input_type is type(None): |
| 337 | + raise TypeError("Option type cannot be only NoneType") |
| 338 | + |
| 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 | + ) |
| 345 | + filtered = tuple(t for t in args if t is not type(None)) |
| 346 | + if not filtered: |
| 347 | + raise TypeError("Option type cannot be only NoneType") |
| 348 | + if len(filtered) == 1: |
| 349 | + return filtered[0] |
| 350 | + return filtered |
| 351 | + |
| 352 | + return input_type |
| 353 | + |
332 | 354 | def to_dict(self) -> dict:
|
333 | 355 | as_dict = {
|
334 | 356 | "name": self.name,
|
|
0 commit comments