slint-ui/slint · error · AttributeError

Callback ' ' in global ' ' cannot be used with a callback…

Error message

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

What it means

In the Python API, the async-callback decorator checks the declared global callback via compdef.global_callback_returns_void. When the lookup returns None the callback is not declared on the Slint global, so setting an async handler is impossible and an AttributeError is raised.

Solutions

  1. Declare the callback on the global in the .slint file (callback <name>(...) -> void).
  2. Check the callback and global names for typos against the .slint markup.
  3. Ensure the decorated function returns nothing (void); non-void callbacks use the non-async path.
  4. Recompile/refresh the component instance before applying decorators.

Example fix

// before (.slint global missing the callback)
# Python: @slint.callback('my_global', 'do_thing')
// after — add to .slint global:
global MyGlobal { callback do-thing(); }
Defensive patterns

Strategy: validation

Validate before calling

names = {c['name'] for c in compdef.properties}  # or inspect compiled callbacks
callbacks = {info['name'] for info in compdef.callback_infos('my_global')}
assert 'do_thing' in callbacks, 'callback not declared in Slint component'

Type guard

def callback_declared(compdef, global_name: str, name: str) -> bool:
    return compdef.global_callback_returns_void(global_name, name) is not None

Try / catch

try:
    @slint.callback('my_global', 'do_thing')
    async def handler(self, *args):
        ...
except AttributeError as e:
    logging.error('callback not declared in .slint: %s', e)

Prevention

When it happens

Trigger: Using @slint.callback (async variant) with a global name whose callback does not exist in the .slint component, typically due to a typo in name or global name.

Common situations: Renaming a callback in .slint without updating Python, wrong global alias, decorator applied before the component was compiled with that global.

Understand the failure class

Background: "Not found" and "does not exist" errors: why "Task not found", "No such folder", and "Can't find" fire when a lookup comes back empty — this error's family across 14 libraries.

Related errors


AI-assisted analysis of slint-ui/slint@bb937076de (2026-09-16). Data as JSON: /api/errors/2184201ee64edf09. Report an issue: GitHub.

Appendix: source

Thrown at api/python/slint/slint/__init__.py:178

def _build_class(
    compdef: native.ComponentDefinition,
) -> typing.Callable[..., Component]:
    def cls_init(self: Component, **kwargs: Any) -> Any:
        self.__instance__ = compdef.create()
        for name, value in self.__class__.__dict__.items():
            if hasattr(value, "slint.callback"):
                callback_info = getattr(value, "slint.callback")
                name = callback_info["name"]

                is_async = getattr(value, "slint.async", False)
                if is_async:
                    if "global_name" in callback_info:
                        global_name = callback_info["global_name"]
                        is_void = compdef.global_callback_returns_void(
                            global_name, name
                        )
                        if is_void is None:
                            raise AttributeError(
                                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"
                            )
                        if not is_void:
                            raise RuntimeError(
                                f"Callback '{name}' in global '{global_name}' cannot be used with a callback decorator for an async function, as it doesn't return void"
                            )
                    else:
                        is_void = compdef.callback_returns_void(name)
                        if is_void is None:
                            raise AttributeError(
                                f"Callback '{name}' cannot be used with a callback decorator for an async function, as it is not declared in Slint component"
                            )
                        if not is_void:
                            raise RuntimeError(
                                f"Callback '{name}' cannot be used with a callback decorator for an async function, as it doesn't return void"
                            )

                def mk_callback(

View on GitHub (pinned to bb937076de)