diff --git a/changelog/1411.feature.rst b/changelog/1411.feature.rst new file mode 100644 index 0000000000..4ff63b13e2 --- /dev/null +++ b/changelog/1411.feature.rst @@ -0,0 +1 @@ +Improve error messaging when a command parameter's annotation cannot be resolved at runtime. diff --git a/disnake/ext/commands/core.py b/disnake/ext/commands/core.py index b04e5efbae..880c2d4975 100644 --- a/disnake/ext/commands/core.py +++ b/disnake/ext/commands/core.py @@ -391,7 +391,16 @@ 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: + 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: msg = "Unparameterized Greedy[...] is disallowed in signature." diff --git a/disnake/ext/commands/slash_core.py b/disnake/ext/commands/slash_core.py index 8a39a8b476..6b46102873 100644 --- a/disnake/ext/commands/slash_core.py +++ b/disnake/ext/commands/slash_core.py @@ -306,7 +306,15 @@ def __init__( ) if options is None: - options = expand_params(self) + try: + options = expand_params(self) + except NameError as e: + 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) @@ -473,7 +481,15 @@ def __init__( ) if options is None: - options = expand_params(self) + try: + options = expand_params(self) + except NameError as e: + 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)