python/qemu: Add mypy type annotations

These should all be purely annotations with no changes in behavior at
all. You need to be in the python folder, but you should be able to
confirm that these annotations are correct (or at least self-consistent)
by running `mypy --strict qemu`.

Signed-off-by: John Snow <jsnow@redhat.com>
Reviewed-by: Kevin Wolf <kwolf@redhat.com>
Message-id: 20201006235817.3280413-12-jsnow@redhat.com
Signed-off-by: John Snow <jsnow@redhat.com>
This commit is contained in:
John Snow 2020-10-06 19:58:08 -04:00
parent 090744d552
commit f12a282ff4
4 changed files with 101 additions and 75 deletions

View file

@ -27,6 +27,7 @@ from typing import (
)
from .machine import QEMUMachine
from .qmp import SocketAddrT
class QEMUQtestProtocol:
@ -43,7 +44,8 @@ class QEMUQtestProtocol:
No conection is estabalished by __init__(), this is done
by the connect() or accept() methods.
"""
def __init__(self, address, server=False):
def __init__(self, address: SocketAddrT,
server: bool = False):
self._address = address
self._sock = self._get_sock()
self._sockfile: Optional[TextIO] = None
@ -51,14 +53,14 @@ class QEMUQtestProtocol:
self._sock.bind(self._address)
self._sock.listen(1)
def _get_sock(self):
def _get_sock(self) -> socket.socket:
if isinstance(self._address, tuple):
family = socket.AF_INET
else:
family = socket.AF_UNIX
return socket.socket(family, socket.SOCK_STREAM)
def connect(self):
def connect(self) -> None:
"""
Connect to the qtest socket.
@ -67,7 +69,7 @@ class QEMUQtestProtocol:
self._sock.connect(self._address)
self._sockfile = self._sock.makefile(mode='r')
def accept(self):
def accept(self) -> None:
"""
Await connection from QEMU.
@ -76,7 +78,7 @@ class QEMUQtestProtocol:
self._sock, _ = self._sock.accept()
self._sockfile = self._sock.makefile(mode='r')
def cmd(self, qtest_cmd):
def cmd(self, qtest_cmd: str) -> str:
"""
Send a qtest command on the wire.
@ -87,14 +89,16 @@ class QEMUQtestProtocol:
resp = self._sockfile.readline()
return resp
def close(self):
"""Close this socket."""
def close(self) -> None:
"""
Close this socket.
"""
self._sock.close()
if self._sockfile:
self._sockfile.close()
self._sockfile = None
def settimeout(self, timeout):
def settimeout(self, timeout: Optional[float]) -> None:
"""Set a timeout, in seconds."""
self._sock.settimeout(timeout)
@ -118,7 +122,7 @@ class QEMUQtestMachine(QEMUMachine):
super().__init__(binary, args, name=name, test_dir=test_dir,
socket_scm_helper=socket_scm_helper,
sock_dir=sock_dir)
self._qtest = None
self._qtest: Optional[QEMUQtestProtocol] = None
self._qtest_path = os.path.join(sock_dir, name + "-qtest.sock")
@property
@ -130,7 +134,7 @@ class QEMUQtestMachine(QEMUMachine):
])
return args
def _pre_launch(self):
def _pre_launch(self) -> None:
super()._pre_launch()
self._qtest = QEMUQtestProtocol(self._qtest_path, server=True)
@ -139,7 +143,7 @@ class QEMUQtestMachine(QEMUMachine):
super()._post_launch()
self._qtest.accept()
def _post_shutdown(self):
def _post_shutdown(self) -> None:
super()._post_shutdown()
self._remove_if_exists(self._qtest_path)