libvirt-python: API change List → (Named)Tuple?
hahn at univention.de
Mon Jul 27 06:32:29 UTC 2020
Am 25.07.20 um 23:45 schrieb Philipp Hahn:
> Am 27.04.20 um 15:44 schrieb Philipp Hahn:
>> I'm working on adding PEP 484 type hints
>> <https://www.python.org/dev/peps/pep-0484/> to the Python binding of
> I just opened a merge request
> <https://gitlab.com/libvirt/libvirt-python/-/merge_requests/9> for my
> code at <https://gitlab.com/pmhahn/libvirt-python/-/tree/typing>.
While working on that I stumbled over some annoyances in the current
Python API: There are many methods which return a List[int], for example:
There are more function returning similar information as plain List:
The worst offender is `virNodeGetInfo`, which returns `Tuple[str,
int*7]`: The problem for type annotation is that `List` is unlimited in
the number of elements, that is you cannot specify the number of entries
the list must or will contain.
Also all elements of a list must have the same (super-)type; for "str"
and "int" of "virNodeGetInfo()" that would be "Any", which is not very
A better type for those `List`s would be `Tuple`, which has a fixed
length and can have different types for each position.
But that would be an API change: In most cases users of those functions
should not notice the difference as indexing tuples or lists is the same.
It would break code where someone would do something like this:
ret = virFunction_returning_list()
ret += [1, 2, 3]
which breaks if that function would return a `Tuple` in the future:
> $ python -c '() + '
> Traceback (most recent call last):
> File "<string>", line 1, in <module>
> TypeError: can only concatenate tuple (not "list") to tuple
Even better then plain `Tuple`s would be
which would allow us to use the return value of `virNodeGetInfo()` as this:
> from collections import namedtuple
> import libvirt
> virNodeInfo = namedtuple("virNodeInfo", ["model", "memory", "cpus", "mhz", "nodes", "sockets", "cores", "threads"])
> c = libvirt.open('test:///default')
> info = virNodeInfo(*c.getInfo())
> # virNodeInfo(model='i686', memory=3072, cpus=16, mhz=1400, nodes=2, sockets=2, cores=2, threads=2)
This could be improved even more by using
which allows to add type information:
> from typing import NamedTuple
> virNodeInfo = NamedTuple("virNodeInfo", [("model", str), ("memory", int), ("cpus", int), ("mhz", int), ("nodes", int), ("sockets", int), ("cores", int), ("threads", int)])
or with Python 3.6 the more readable form
> class virNodeInfo(NamedTuple):
> model: str
> memory: int
> cpus: int
> mhz: int
> nodes: int
> sockets: int
> cores: int
> threads: int
IMHO that would be a real usability improvement as I have to lookup that
information every time I have to use those functions myself.
Indexing with `namedtuple` and `NamedTuple` still works, so you can
still use `info` above to get the model.
What do you think of that?
When would be a good time to make such a change?
More information about the libvir-list