LEW21 / pydbus

Pythonic DBus library
GNU Lesser General Public License v2.1
331 stars 75 forks source link

TypeError: argument value: Expected GLib.Variant, but got xxx #52

Closed benallard closed 6 years ago

benallard commented 7 years ago

This might be a duplicate of #27, but I didn't understood it good enough to be sure.

Traceback (most recent call last):
  File "/home/debian/galileo/galileo/galileo/main.py", line 284, in main
    }[config.mode](config)
  File "/home/debian/galileo/galileo/galileo/main.py", line 192, in sync
    for tracker in syncAllTrackers(config):
  File "/home/debian/galileo/galileo/galileo/main.py", line 48, in syncAllTrackers
    trackers = [t for t in fitbit.discover(FitBitUUID)]
  File "/home/debian/galileo/galileo/galileo/main.py", line 48, in <listcomp>
    trackers = [t for t in fitbit.discover(FitBitUUID)]
  File "/home/debian/galileo/galileo/galileo/ble/pydbus.py", line 68, in discover
    self.adapter.SetDiscoveryFilter({'UUIDs': [service], 'Transport': 'le'})
  File "/home/debian/galileo/lib/python3.4/site-packages/pydbus/proxy_method.py", line 75, in __call__
    0, timeout_to_glib(timeout), None).unpack()
  File "/home/debian/galileo/lib/python3.4/site-packages/gi/overrides/GLib.py", line 240, in __new__
    (v, rest_format, _) = creator._create(format_string, [value])
  File "/home/debian/galileo/lib/python3.4/site-packages/gi/overrides/GLib.py", line 129, in _create
    return self._create_tuple(format, args)
  File "/home/debian/galileo/lib/python3.4/site-packages/gi/overrides/GLib.py", line 164, in _create_tuple
    (v, format, _) = self._create(format, args[0][i:])
  File "/home/debian/galileo/lib/python3.4/site-packages/gi/overrides/GLib.py", line 132, in _create
    return self._create_dict(format, args)
  File "/home/debian/galileo/lib/python3.4/site-packages/gi/overrides/GLib.py", line 191, in _create_dict
    (val_v, rest_format, _) = self._create(rest_format, [v])
  File "/home/debian/galileo/lib/python3.4/site-packages/gi/overrides/GLib.py", line 123, in _create
    v = constructor(args[0])
TypeError: argument value: Expected GLib.Variant, but got list

This happened while calling org.bluez.Adapter1.SetDiscoveryFilter with a raw dict. The dict got translated correctly, but not its fields.

LEW21 commented 7 years ago

DBus introspection data does not provide information about expected types of values in dictionaries using multiple types inside. So you need to create Variants yourself.

from pydbus import Variant

Variant('as', [service])
Variant('s', 'le')

Here's more info about the type descriptions: https://developer.gnome.org/glib/stable/gvariant-format-strings.html


Theoretically pydbus could infer those types automatically based on Python types, but it might lead to problems when talking with a daemon written in a statically-typed language that expects other types than pydbus has inferred.

On the other hand, people usually guess these things, and do not check them in the docs, so automatic guesser might do a better job than they do.

benallard commented 7 years ago

It's a bit disturbing to have to go back to the raw type for that one call only. All the rest is working fine, except this, giving the feeling that it's broken.

I actually fixed it on my side the way you advised me to use. So it's working fine now, just feeling a bit awkward.