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. 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..4049928c62 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 None 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 None self.docstring = utils.parse_docstring(func) desc_loc = Localized._cast(description, False)