From ad9a27c0a352a5cc7c7d56214fb65c257fffacda Mon Sep 17 00:00:00 2001 From: arielle Date: Tue, 30 Sep 2025 18:50:28 -0400 Subject: [PATCH 1/3] feat: improve error messages for annotation issues in command parameters Reduce the length of the traceback and provide a helpful error message when a user forgets to import a type used in a command parameter annotation outside of a TYPE_CHECKING block. --- disnake/ext/commands/core.py | 9 ++++++++- disnake/ext/commands/slash_core.py | 16 ++++++++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/disnake/ext/commands/core.py b/disnake/ext/commands/core.py index fbbc9cea3a..9ebf5731bc 100644 --- a/disnake/ext/commands/core.py +++ b/disnake/ext/commands/core.py @@ -391,7 +391,14 @@ def callback(self, function: CommandCallback[CogT, Any, P, T]) -> None: except AttributeError: globalns = {} - params = get_signature_parameters(function, globalns, skip_standard_params=True) + try: + params = get_signature_parameters(function, globalns, skip_standard_params=True) + except NameError as e: + raise NameError( + "Stringified params annotations must have their reference imported outside of a TYPE_CHECKING block: " + + e.args[0] + ) from None + for param in params.values(): if param.annotation is Greedy: msg = "Unparameterized Greedy[...] is disallowed in signature." diff --git a/disnake/ext/commands/slash_core.py b/disnake/ext/commands/slash_core.py index 7a813d5257..e845abc44a 100644 --- a/disnake/ext/commands/slash_core.py +++ b/disnake/ext/commands/slash_core.py @@ -306,7 +306,13 @@ def __init__( ) if options is None: - options = expand_params(self) + try: + options = expand_params(self) + except NameError as e: + raise NameError( + "Could not expand parameters. Please check all annotations are imported outside of TYPE_CHECKING blocks. " + + e.args[0] + ) from e self.docstring = utils.parse_docstring(func) desc_loc = Localized._cast(description, False) @@ -473,7 +479,13 @@ def __init__( ) if options is None: - options = expand_params(self) + try: + options = expand_params(self) + except NameError as e: + raise NameError( + "Could not expand parameter options. Please check all annotations are imported outside of TYPE_CHECKING blocks. " + + e.args[0] + ) from e self.docstring = utils.parse_docstring(func) desc_loc = Localized._cast(description, False) From 4901a6239a7c1d61f71c701fee2402a98a62ef03 Mon Sep 17 00:00:00 2001 From: arielle Date: Tue, 30 Sep 2025 18:55:07 -0400 Subject: [PATCH 2/3] chore: add changelog --- changelog/1411.feature.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog/1411.feature.rst diff --git a/changelog/1411.feature.rst b/changelog/1411.feature.rst new file mode 100644 index 0000000000..2836440816 --- /dev/null +++ b/changelog/1411.feature.rst @@ -0,0 +1 @@ +Improve error messaging when a command's annotation is stringified. From 3953a4b8eb93b210af784bc6dcc9c8b80e6dd6e1 Mon Sep 17 00:00:00 2001 From: onerandomusername Date: Sat, 4 Oct 2025 12:15:49 -0400 Subject: [PATCH 3/3] fix: dedupe error message --- disnake/ext/commands/slash_core.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/disnake/ext/commands/slash_core.py b/disnake/ext/commands/slash_core.py index e845abc44a..4049928c62 100644 --- a/disnake/ext/commands/slash_core.py +++ b/disnake/ext/commands/slash_core.py @@ -312,7 +312,7 @@ def __init__( raise NameError( "Could not expand parameters. Please check all annotations are imported outside of TYPE_CHECKING blocks. " + e.args[0] - ) from e + ) from None self.docstring = utils.parse_docstring(func) desc_loc = Localized._cast(description, False) @@ -485,7 +485,7 @@ def __init__( raise NameError( "Could not expand parameter options. Please check all annotations are imported outside of TYPE_CHECKING blocks. " + e.args[0] - ) from e + ) from None self.docstring = utils.parse_docstring(func) desc_loc = Localized._cast(description, False)