Skip to content

Commit cf7dc16

Browse files
Lumabotspre-commit-ci[bot]Lulalaby
authored andcommitted
fix: handling of Optional[...] in command option type resolution (Pycord-Development#2852)
Signed-off-by: Lumouille <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Lala Sabathil <[email protected]> (cherry picked from commit 2cf71df)
1 parent 2abb5dd commit cf7dc16

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ These changes are available on the `master` branch, but have not yet been releas
157157
([#2843](https://github.com/Pycord-Development/pycord/pull/2843))
158158
- Fixed `TypeError` when using `@option` with certain annotations and along with
159159
`channel_types`. ([#2835](https://github.com/Pycord-Development/pycord/pull/2835))
160+
- Fixed `TypeError` when using `Optional[...]` or `... | None` in command option type.
161+
([#2852](https://github.com/Pycord-Development/pycord/pull/2852))
160162
- Fixed type-hinting for `PermissionOverwrite.update`.
161163
([#2878](https://github.com/Pycord-Development/pycord/pull/2878))
162164
- Fixed `AttributeError` when accessing `AuditLogEntry.changes` more than once.

discord/commands/options.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@
2626

2727
import inspect
2828
import logging
29+
import types
2930
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
3132

3233
from ..abc import GuildChannel, Mentionable
3334
from ..channel import (
@@ -193,6 +194,7 @@ def __init__(self, input_type: InputType = str, /, description: str | None = Non
193194
if self.name is not None:
194195
self.name = str(self.name)
195196
self._parameter_name = self.name # default
197+
input_type = self._strip_none_type(input_type)
196198
self._raw_type: InputType | tuple = input_type
197199

198200
enum_choices = []
@@ -329,6 +331,26 @@ def __init__(self, input_type: InputType = str, /, description: str | None = Non
329331
if input_type is None:
330332
raise TypeError("input_type cannot be NoneType.")
331333

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+
332354
def to_dict(self) -> dict:
333355
as_dict = {
334356
"name": self.name,

0 commit comments

Comments
 (0)