From ad9a27c0a352a5cc7c7d56214fb65c257fffacda Mon Sep 17 00:00:00 2001 From: arielle Date: Tue, 30 Sep 2025 18:50:28 -0400 Subject: [PATCH 1/5] 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/5] 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/5] 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) From edbbce14c3b954efda7c9163a0d05e00e48e880e Mon Sep 17 00:00:00 2001 From: onerandomusername Date: Sat, 18 Oct 2025 04:14:13 -0400 Subject: [PATCH 4/5] update messaging --- disnake/ext/commands/core.py | 10 ++++++---- disnake/ext/commands/slash_core.py | 20 ++++++++++++-------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/disnake/ext/commands/core.py b/disnake/ext/commands/core.py index 9ebf5731bc..3fbca08c80 100644 --- a/disnake/ext/commands/core.py +++ b/disnake/ext/commands/core.py @@ -394,10 +394,12 @@ def callback(self, function: CommandCallback[CogT, Any, P, T]) -> None: 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 + msg = ( + str(e) + + ", please check all annotations are defined outside of TYPE_CHECKING blocks." + ) + # todo: add name kw only argument once we use py310+ + raise NameError(msg) from None for param in params.values(): if param.annotation is Greedy: diff --git a/disnake/ext/commands/slash_core.py b/disnake/ext/commands/slash_core.py index 4049928c62..0c8539c996 100644 --- a/disnake/ext/commands/slash_core.py +++ b/disnake/ext/commands/slash_core.py @@ -309,10 +309,12 @@ def __init__( 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 None + msg = ( + str(e) + + ", please check all annotations are defined outside of TYPE_CHECKING blocks." + ) + # todo: add name kw only argument once we use py310+ + raise NameError(msg) from None self.docstring = utils.parse_docstring(func) desc_loc = Localized._cast(description, False) @@ -482,10 +484,12 @@ def __init__( 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 None + msg = ( + str(e) + + ", please check all annotations are defined outside of TYPE_CHECKING blocks." + ) + # todo: add name kw only argument once we use py310+ + raise NameError(msg) from None self.docstring = utils.parse_docstring(func) desc_loc = Localized._cast(description, False) From 8ccfe0561c83da192ce283c7e844a804de3dbc2f Mon Sep 17 00:00:00 2001 From: Zenith Date: Sat, 18 Oct 2025 05:13:53 -0400 Subject: [PATCH 5/5] Update changelog/1411.feature.rst Co-authored-by: vi <8530778+shiftinv@users.noreply.github.com> Signed-off-by: Zenith --- changelog/1411.feature.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog/1411.feature.rst b/changelog/1411.feature.rst index 2836440816..4ff63b13e2 100644 --- a/changelog/1411.feature.rst +++ b/changelog/1411.feature.rst @@ -1 +1 @@ -Improve error messaging when a command's annotation is stringified. +Improve error messaging when a command parameter's annotation cannot be resolved at runtime.