From 660fa15263ed7a27fd26a8e90c9ba5fc5e6ea45f Mon Sep 17 00:00:00 2001 From: Lumouille <144063653+Lumabots@users.noreply.github.com> Date: Sun, 3 Aug 2025 22:01:24 +0200 Subject: [PATCH 1/7] Update options.py --- discord/commands/options.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/discord/commands/options.py b/discord/commands/options.py index 0425137aed..8ba5badfbe 100644 --- a/discord/commands/options.py +++ b/discord/commands/options.py @@ -27,7 +27,8 @@ import inspect import logging from enum import Enum -from typing import TYPE_CHECKING, Literal, Optional, Type, Union +import types +from typing import TYPE_CHECKING, Literal, Optional, Type, Union, get_args from ..abc import GuildChannel, Mentionable from ..channel import ( @@ -196,6 +197,7 @@ def __init__( if self.name is not None: self.name = str(self.name) self._parameter_name = self.name # default + input_type = self._strip_none_type(input_type) self._raw_type: InputType | tuple = input_type enum_choices = [] @@ -365,6 +367,26 @@ def __init__( if input_type is None: raise TypeError("input_type cannot be NoneType.") + @staticmethod + def _strip_none_type(input_type): + if input_type is type(None): + raise TypeError("Option type cannot be only NoneType") + + if isinstance(input_type, (types.UnionType, tuple)): + args = ( + get_args(input_type) + if isinstance(input_type, types.UnionType) + else input_type + ) + filtered = tuple(t for t in args if t is not type(None)) + if not filtered: + raise TypeError("Option type cannot be only NoneType") + if len(filtered) == 1: + return filtered[0] + return filtered + + return input_type + def to_dict(self) -> dict: as_dict = { "name": self.name, From 53df3a1100361bdc35ca93c1fc38182ffd3d084f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 3 Aug 2025 20:22:59 +0000 Subject: [PATCH 2/7] style(pre-commit): auto fixes from pre-commit.com hooks --- discord/commands/options.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/discord/commands/options.py b/discord/commands/options.py index 8ba5badfbe..a88807f893 100644 --- a/discord/commands/options.py +++ b/discord/commands/options.py @@ -26,8 +26,8 @@ import inspect import logging -from enum import Enum import types +from enum import Enum from typing import TYPE_CHECKING, Literal, Optional, Type, Union, get_args from ..abc import GuildChannel, Mentionable From af43f291cd7a97141d63c2bc263546d842356da6 Mon Sep 17 00:00:00 2001 From: Lumouille <144063653+Lumabots@users.noreply.github.com> Date: Sun, 3 Aug 2025 22:26:11 +0200 Subject: [PATCH 3/7] Update CHANGELOG.md --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 718642108d..bd6af26921 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -138,6 +138,8 @@ These changes are available on the `master` branch, but have not yet been releas ([#2843](https://github.com/Pycord-Development/pycord/pull/2843)) - Fixed `TypeError` when using `@option` with certain annotations and along with `channel_types`. ([#2835](https://github.com/Pycord-Development/pycord/pull/2835)) +- Fixed `TypeError` when using `Optional[...]` or `... | None` in command option type. +([#2852](https://github.com/Pycord-Development/pycord/pull/2852)) ### Changed From 1e81b9e5c15345a1fc96dbd05147d80be3fcb8c9 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 3 Aug 2025 23:37:38 +0000 Subject: [PATCH 4/7] style(pre-commit): auto fixes from pre-commit.com hooks --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bd6af26921..7dc3157727 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -139,7 +139,7 @@ These changes are available on the `master` branch, but have not yet been releas - Fixed `TypeError` when using `@option` with certain annotations and along with `channel_types`. ([#2835](https://github.com/Pycord-Development/pycord/pull/2835)) - Fixed `TypeError` when using `Optional[...]` or `... | None` in command option type. -([#2852](https://github.com/Pycord-Development/pycord/pull/2852)) + ([#2852](https://github.com/Pycord-Development/pycord/pull/2852)) ### Changed From 97af386f307be41c1bc8d3817467086fd2ab9860 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 30 Aug 2025 13:15:43 +0000 Subject: [PATCH 5/7] style(pre-commit): auto fixes from pre-commit.com hooks --- CHANGELOG.md | 1 - 1 file changed, 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f1fc3e033b..2cd0e337e6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -151,7 +151,6 @@ These changes are available on the `master` branch, but have not yet been releas - Fixed `AttributeError` when accessing `AuditLogEntry.changes` more than once. ([#2882])(https://github.com/Pycord-Development/pycord/pull/2882)) - ### Changed - Renamed `cover` property of `ScheduledEvent` and `cover` argument of From d467901ca6348f2926fc6ea2506d5ef93301c52c Mon Sep 17 00:00:00 2001 From: Lumouille <144063653+Lumabots@users.noreply.github.com> Date: Mon, 1 Sep 2025 22:47:38 +0200 Subject: [PATCH 6/7] Update options.py --- discord/commands/options.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/discord/commands/options.py b/discord/commands/options.py index a88807f893..1c53bb1852 100644 --- a/discord/commands/options.py +++ b/discord/commands/options.py @@ -369,6 +369,9 @@ def __init__( @staticmethod def _strip_none_type(input_type): + if isinstance(input_type, SlashCommandOptionType): + return input_type + if input_type is type(None): raise TypeError("Option type cannot be only NoneType") From dd04586f113f0cbf7dd2bd50ac10d02e19837a3f Mon Sep 17 00:00:00 2001 From: Lumouille <144063653+Lumabots@users.noreply.github.com> Date: Mon, 1 Sep 2025 23:03:57 +0200 Subject: [PATCH 7/7] fix(options): Improve input_type handling in Option class --- discord/commands/options.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/discord/commands/options.py b/discord/commands/options.py index 1c53bb1852..c3bfafe888 100644 --- a/discord/commands/options.py +++ b/discord/commands/options.py @@ -375,17 +375,21 @@ def _strip_none_type(input_type): if input_type is type(None): raise TypeError("Option type cannot be only NoneType") - if isinstance(input_type, (types.UnionType, tuple)): - args = ( - get_args(input_type) - if isinstance(input_type, types.UnionType) - else input_type - ) + args = () + if isinstance(input_type, types.UnionType): + args = get_args(input_type) + elif getattr(input_type, "__origin__", None) is Union: + args = get_args(input_type) + elif isinstance(input_type, tuple): + args = input_type + + if args: filtered = tuple(t for t in args if t is not type(None)) if not filtered: raise TypeError("Option type cannot be only NoneType") if len(filtered) == 1: return filtered[0] + return filtered return input_type