{"record":{"id":"31ee9de677726d4c","repo":"slint-ui/slint","slug":"callback-name-cannot-be-used-with-a-callback-d","errorCode":null,"errorMessage":"Callback '{name}' cannot be used with a callback decorator for an async function, as it is not declared in Slint component","messagePattern":"Callback '(.+?)' cannot be used with a callback decorator for an async function, as it is not declared in Slint component","errorType":"exception","errorClass":"AttributeError","httpStatus":null,"severity":"error","filePath":"api/python/slint/slint/__init__.py","lineNumber":188,"sourceCode":"                is_async = getattr(value, \"slint.async\", False)\n                if is_async:\n                    if \"global_name\" in callback_info:\n                        global_name = callback_info[\"global_name\"]\n                        is_void = compdef.global_callback_returns_void(\n                            global_name, name\n                        )\n                        if is_void is None:\n                            raise AttributeError(\n                                f\"Callback '{name}' in global '{global_name}' cannot be used with a callback decorator for an async function, as it is not declared in Slint component\"\n                            )\n                        if not is_void:\n                            raise RuntimeError(\n                                f\"Callback '{name}' in global '{global_name}' cannot be used with a callback decorator for an async function, as it doesn't return void\"\n                            )\n                    else:\n                        is_void = compdef.callback_returns_void(name)\n                        if is_void is None:\n                            raise AttributeError(\n                                f\"Callback '{name}' cannot be used with a callback decorator for an async function, as it is not declared in Slint component\"\n                            )\n                        if not is_void:\n                            raise RuntimeError(\n                                f\"Callback '{name}' cannot be used with a callback decorator for an async function, as it doesn't return void\"\n                            )\n\n                def mk_callback(\n                    self: Any, callback: typing.Callable[..., Any]\n                ) -> typing.Callable[..., Any]:\n                    def invoke(*args: Any, **kwargs: Any) -> Any:\n                        return callback(self, *args, **kwargs)\n\n                    return invoke\n\n                if \"global_name\" in callback_info:\n                    self.__instance__.set_global_callback(\n                        callback_info[\"global_name\"], name, mk_callback(self, value)","sourceCodeStart":170,"sourceCodeEnd":206,"githubUrl":"https://github.com/slint-ui/slint/blob/a9ea814a5813e7460fa1a867924872095a4d678d/api/python/slint/slint/__init__.py#L170-L206","documentation":"The component-level (non-global) variant of the async callback validation: when a subclass method decorated with @slint.callback is async and has no global_name, cls_init calls compdef.callback_returns_void(name); a None return means the component itself declares no callback with that name, and this AttributeError is raised at instantiation.","triggerScenarios":"`@slint.callback` + `async def` on a subclass method whose name is not a `callback ...;` declared directly on the root component in the .slint file — typo, rename drift, dash/underscore mismatch, or a non-callback helper that got decorated.","commonSituations":"Callback renamed in .slint without updating Python; stale loaded .slint after edits; decorator accidentally applied to an internal method; forgetting that `handle-click` becomes `handle_click` in Python.","solutions":["Declare the callback on the component in .slint: `component App { callback quit(); ... }`.","Match the Python method name to the Slint name (underscores for dashes).","Re-run slint.load_file() after editing the .slint file before creating instances.","Remove @slint.callback from methods that are not invoked by Slint."],"exampleFix":"# before (main.slint)\nexport component App { callback clicked(); }\n\n@slint.callback\nasync def quitted(self): ...  # 'quitted' not declared -> AttributeError\n\n# after\nexport component App {\n    callback clicked();\n    callback quitted();\n}","handlingStrategy":"try-catch","validationCode":"# Before instantiating, assert every component-level async @slint.callback is declared\ninstance_probe = slint.load_file(\"app.slint\")\ncomp = getattr(instance_probe, \"App\")\n# instantiate the base class once to reach the definition\nprobe = comp()\ndeclared = {c.replace(\"-\", \"_\") for c in probe.__instance__.definition.callbacks}\nfor name in my_async_callback_names:\n    assert name in declared, f\"callback '{name}' not declared in .slint\"","typeGuard":null,"tryCatchPattern":"try:\n    app = App()\nexcept AttributeError as e:\n    raise SystemExit(f\"Undeclared callback: {e}. Declare `callback <name>(...)` on the component and reload via slint.load_file.\") from e","preventionTips":["Re-run slint.load_file after every .slint edit before creating subclass instances.","Use the exact callback name (underscores for dashes) on decorated methods.","Never decorate non-callback helper methods with @slint.callback."],"tags":["python","slint","callbacks","async","decorator"],"backgroundTag":"undeclared-callback-binding","analyzedSha":"a9ea814a5813e7460fa1a867924872095a4d678d","analyzedAt":"2026-08-16T22:39:12.830Z","schemaVersion":2},"datasetVersion":"2026-08-16T23:17:17.608Z"}