VariantType

Added in version 2.24.

class VariantType(**kwargs)
Constructors:

new(type_string:str) -> GLib.VariantType
new_array(element:GLib.VariantType) -> GLib.VariantType
new_dict_entry(key:GLib.VariantType, value:GLib.VariantType) -> GLib.VariantType
new_maybe(element:GLib.VariantType) -> GLib.VariantType
new_tuple(items:list) -> GLib.VariantType

Constructors

class VariantType
classmethod new(type_string: str) VariantType

Creates a new VariantType corresponding to the type string given by type_string. It is appropriate to call free() on the return value.

It is a programmer error to call this function with an invalid type string. Use string_is_valid() if you are unsure.

Added in version 2.24.

Parameters:

type_string – a valid GVariant type string

classmethod new_array(element: VariantType) VariantType

Constructs the type corresponding to an array of elements of the type type.

It is appropriate to call free() on the return value.

Parameters:

element – a VariantType

classmethod new_dict_entry(key: VariantType, value: VariantType) VariantType

Constructs the type corresponding to a dictionary entry with a key of type key and a value of type value.

It is appropriate to call free() on the return value.

Parameters:
classmethod new_maybe(element: VariantType) VariantType

Constructs the type corresponding to a maybe instance containing type type or Nothing.

It is appropriate to call free() on the return value.

Parameters:

element – a VariantType

classmethod new_tuple(items: Sequence[VariantType]) VariantType

Constructs a new tuple type, from items.

length is the number of items in items, or -1 to indicate that items is None-terminated.

It is appropriate to call free() on the return value.

Parameters:

items – an array of VariantType, one for each item

Methods

class VariantType
checked_(type_string: str) VariantType
Parameters:

type_string

dup_string() str

Returns a newly-allocated copy of the type string corresponding to type. The returned string is nul-terminated. It is appropriate to call free() on the return value.

element() VariantType

Determines the element type of an array or maybe type.

This function may only be used with array or maybe types.

equal(type2: VariantType) bool

Compares type1 and type2 for equality.

Only returns True if the types are exactly equal. Even if one type is an indefinite type and the other is a subtype of it, False will be returned if they are not exactly equal. If you want to check for subtypes, use is_subtype_of().

The argument types of type1 and type2 are only gpointer to allow use with HashTable without function pointer casting. For both arguments, a valid VariantType must be provided.

Parameters:

type2 – a VariantType

first() VariantType

Determines the first item type of a tuple or dictionary entry type.

This function may only be used with tuple or dictionary entry types, but must not be used with the generic tuple type %G_VARIANT_TYPE_TUPLE.

In the case of a dictionary entry type, this returns the type of the key.

None is returned in case of type being %G_VARIANT_TYPE_UNIT.

This call, together with next() provides an iterator interface over tuple and dictionary entry types.

free() None

Frees a VariantType that was allocated with copy(), new() or one of the container type constructor functions.

In the case that type is None, this function does nothing.

Since 2.24

get_string_length() int

Returns the length of the type string corresponding to the given type. This function must be used to determine the valid extent of the memory region returned by peek_string().

hash() int

Hashes type.

The argument type of type is only gpointer to allow use with HashTable without function pointer casting. A valid VariantType must be provided.

is_array() bool

Determines if the given type is an array type. This is true if the type string for type starts with an ‘a’.

This function returns True for any indefinite type for which every definite subtype is an array type – %G_VARIANT_TYPE_ARRAY, for example.

is_basic() bool

Determines if the given type is a basic type.

Basic types are booleans, bytes, integers, doubles, strings, object paths and signatures.

Only a basic type may be used as the key of a dictionary entry.

This function returns False for all indefinite types except %G_VARIANT_TYPE_BASIC.

is_container() bool

Determines if the given type is a container type.

Container types are any array, maybe, tuple, or dictionary entry types plus the variant type.

This function returns True for any indefinite type for which every definite subtype is a container – %G_VARIANT_TYPE_ARRAY, for example.

is_definite() bool

Determines if the given type is definite (ie: not indefinite).

A type is definite if its type string does not contain any indefinite type characters (‘*’, ‘?’, or ‘r’).

A Variant instance may not have an indefinite type, so calling this function on the result of get_type() will always result in True being returned. Calling this function on an indefinite type like %G_VARIANT_TYPE_ARRAY, however, will result in False being returned.

is_dict_entry() bool

Determines if the given type is a dictionary entry type. This is true if the type string for type starts with a ‘{‘.

This function returns True for any indefinite type for which every definite subtype is a dictionary entry type – %G_VARIANT_TYPE_DICT_ENTRY, for example.

is_maybe() bool

Determines if the given type is a maybe type. This is true if the type string for type starts with an ‘m’.

This function returns True for any indefinite type for which every definite subtype is a maybe type – %G_VARIANT_TYPE_MAYBE, for example.

is_subtype_of(supertype: VariantType) bool

Checks if type is a subtype of supertype.

This function returns True if type is a subtype of supertype. All types are considered to be subtypes of themselves. Aside from that, only indefinite types can have subtypes.

Parameters:

supertype – a VariantType

is_tuple() bool

Determines if the given type is a tuple type. This is true if the type string for type starts with a ‘(’ or if type is %G_VARIANT_TYPE_TUPLE.

This function returns True for any indefinite type for which every definite subtype is a tuple type – %G_VARIANT_TYPE_TUPLE, for example.

is_variant() bool

Determines if the given type is the variant type.

key() VariantType

Determines the key type of a dictionary entry type.

This function may only be used with a dictionary entry type. Other than the additional restriction, this call is equivalent to first().

n_items() int

Determines the number of items contained in a tuple or dictionary entry type.

This function may only be used with tuple or dictionary entry types, but must not be used with the generic tuple type %G_VARIANT_TYPE_TUPLE.

In the case of a dictionary entry type, this function will always return 2.

next() VariantType

Determines the next item type of a tuple or dictionary entry type.

type must be the result of a previous call to first() or next().

If called on the key type of a dictionary entry then this call returns the value type. If called on the value type of a dictionary entry then this call returns None.

For tuples, None is returned when type is the last item in a tuple.

string_get_depth_(type_string: str) int
Parameters:

type_string

string_is_valid(type_string: str) bool

Checks if type_string is a valid GVariant type string. This call is equivalent to calling string_scan() and confirming that the following character is a nul terminator.

Parameters:

type_string – a pointer to any string

string_scan(string: str, limit: str | None = None) tuple[bool, str]

Scan for a single complete and valid GVariant type string in string. The memory pointed to by limit (or bytes beyond it) is never accessed.

If a valid type string is found, endptr is updated to point to the first character past the end of the string that was found and True is returned.

If there is no valid type string starting at string, or if the type string does not end before limit then False is returned.

For the simple case of checking if a string is a valid type string, see string_is_valid().

Added in version 2.24.

Parameters:
  • string – a pointer to any string

  • limit – the end of string, or None

value() VariantType

Determines the value type of a dictionary entry type.

This function may only be used with a dictionary entry type.