python-sdbus / python-sdbus-networkmanager

python-sdbus binds for NetworkManager
GNU Lesser General Public License v2.1
30 stars 6 forks source link

TypeError when calling NetworkConnectionSettings.get_settings() #64

Closed pmeye closed 8 months ago

pmeye commented 8 months ago

I'm trying to follow the example for changing connections, but when I run this code I get the error below:

from sdbus_block.networkmanager import (
    NetworkManagerSettings,
    NetworkConnectionSettings
)
import sdbus

system_bus = sdbus.sd_bus_open_system()
sdbus.set_default_bus(system_bus)

nm_settings = NetworkManagerSettings()

bridge_connection = nm_settings.get_connections_by_id("br0")
bridge_settings = NetworkConnectionSettings(bridge_connection)
bridge_properties = bridge_settings.get_settings()

Error message:

Traceback (most recent call last):
  File "/var/home/user/test/src/defaultroute-test.py", line 15, in <module>
    bridge_properties = bridge_settings.get_settings()
  File "/usr/local/lib64/python3.9/site-packages/sdbus/dbus_proxy_sync_method.py", line 98, in __call__
    return self._call_dbus_sync(*rebuilt_args)
  File "/usr/local/lib64/python3.9/site-packages/sdbus/dbus_proxy_sync_method.py", line 71, in _call_dbus_sync
    new_call_message = self.interface._attached_bus. \
TypeError: argument 2 must be str, not list

I'm a big noob and struggling to figure it out myself, any help would be greatly appreciated.

igo95862 commented 8 months ago

Hello @pmeye ,

The first issue I see is that get_connections_by_id returns a list of strings but you treat it as a single string. The reason is that in NetworkManager connections can have the same id. The get_connections_by_id is not actually a part of NetworkManager D-Bus API but a helper method defined in sdbus-networkmanager.

I would recommend running a type checker like mypy. It will instantly tell you what the problem is:

pmeye_example.py:13: error: Argument 1 to "NetworkConnectionSettings" has incompatible type "list[str]"; expected "str"  [arg-type]
Found 1 error in 1 file (checked 1 source file)
pmeye commented 8 months ago

Embarassingly simple, thanks a lot and thanks for the recommendation! My Python tooling seems to need some work.