Add an array property to a GObject in Python

G’day!

Sorry if this is dead obvious:

In Python, how would I add a property to a GObject to hold an array of strings?

For example:

class HasAquaticMammals(GObject.Object):
    def __init__(self):
        super().__init__()
        self._list_of_mammals = ["Dolphin", "Whale", "Squid"]

       @GObject.Property(type=???) # What type here?
        def aquatic_mammals(self):
            return self._list_of_mammals

OK, guessed it!

@GObject.Property(type=GObject.ValueArray)

No, ValueArray is a deprecated type.

You want GObject.Strv, which is a vector of strings.

Ahh! So like this in Python:

@GObject.Property(type=GObject.TYPE_STRV)
    def previous_names(self) -> list[str]:
        return self._previous_names

Incidentally, I have a suite of tests, one of which failed after changing the type:

    def test_previous_names_with_different_types(self):
        species = SpeciesModel()
        species.previous_names = ["abc", 123]

Glad it failed!

Yes; GValueArray is an array of values wrapped in a GValue, which can contain any type representable by the GObject type system. This means you can have an heterogeneous set of values of any type, as opposed to a homogeneous set of values with the same type.