Signals with return types

I’m following along from the documentation here:

https://pygobject.gnome.org/guide/api/signals.html

It uses the GObject.Signal decorator with a return_type of bool, but the handler returns None. Here’s an example I’ve cooked up myself:

class ReturnTypeSignaller(GObject.Object):
    def __init__(self):
        super().__init__()

    @GObject.Signal(return_type=bool)
    def signal_with_return_type(self) -> bool:
        pass

def test_signal_with_return_type():
    instance_type = None
    def handler(instance: ReturnTypeSignaller) -> bool:
        nonlocal instance_type
        instance_type = type(instance)
        return True

    signaller = ReturnTypeSignaller()
    signaller.connect("signal-with-return-type", handler)
    signaller.emit("signal-with-return-type")

    assert instance_type == ReturnTypeSignaller

Now the GTK documentation includes this overview:

It states:

A handler must match the type defined by the signal in both its arguments and return value (which is often void)

In my example above, returning a string, say, does not cause any errors or warnings. From this I gather that return types are not commonly used but probably show up in some use cases, perhaps more to do with the C API.

Can someone explain?

Thanks again,

J.R.

Hi,

Because in Python, strings are automatically converted to booleans (False if empty, else True).

How have you checked that?
You need to use a SignalAccumulator callback to check the handlers return value, and I don’t see one in your code…

Oh! I’ll look into that.

Many thanks,

J.R.

1 Like