From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-10.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,HTML_MESSAGE,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id CEBD1C432BE for ; Thu, 5 Aug 2021 19:57:43 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 12D3860EBB for ; Thu, 5 Aug 2021 19:57:42 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 12D3860EBB Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=nongnu.org Received: from localhost ([::1]:56866 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mBjUr-0007ln-Up for qemu-devel@archiver.kernel.org; Thu, 05 Aug 2021 15:57:41 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:49216) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mBjU2-0006wB-4S for qemu-devel@nongnu.org; Thu, 05 Aug 2021 15:56:50 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:60592) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mBjTy-0008Oz-Vq for qemu-devel@nongnu.org; Thu, 05 Aug 2021 15:56:49 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1628193405; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=aa83T238cA1616R2/VIWmapWTIsTw36rEyZa9gZ5S4M=; b=O65p8zLp796b/a2lH0S2M0YpmtI3mdxL1UqMalfHk4hmLEJ/6FZf3+PVXLp7d21nderKFv IT3aS94YxHGg0eSHyApI38usQe5UNSQ+KSD21DzNrRk/rR7oUgkLOgNGkjfUcmBtTFT5/e wKiIzZ/hakjSeH3Ez00whwrfiIcQ0xM= Received: from mail-oi1-f199.google.com (mail-oi1-f199.google.com [209.85.167.199]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-576-OGbzIu71McqYOX1a5GEEfQ-1; Thu, 05 Aug 2021 15:56:43 -0400 X-MC-Unique: OGbzIu71McqYOX1a5GEEfQ-1 Received: by mail-oi1-f199.google.com with SMTP id be10-20020a056808218ab02902671f63963eso1530094oib.18 for ; Thu, 05 Aug 2021 12:56:43 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=aa83T238cA1616R2/VIWmapWTIsTw36rEyZa9gZ5S4M=; b=S9+Dpl06YJ0r0tR07MsqnTJQmMKZ0xR4xGYGNnYlhZ2dLOOx6EHFiCbtMLN3Cdcgf0 XkLgfMZsYhrcPSqzp7SjBteHcK3EwK8xFs5aH0uD5qXDUGDhUMUE05jZqv4GMxigV7XQ azBqFEeeVNHHe7Re7leONgHOletLTalnDCLDZ8/mPGcp5qzcfc9xMAln92MJ47DOreRc V6wrnFilgENg0p6b6zRiyHDsHLgHIX44EQKrlIobnSN/d7uel4aLv6H9ryt/2Grme1qx rrPxKLG3eGnrRk7n99K3JGFHcEGBG3HIqdx2Qs66XSqtSKcJ+VbLVgBiTFfGbL7HNpOq /w7Q== X-Gm-Message-State: AOAM530WwcgZsrOlXjwrnFCBEOOw8zNaLvJ8EEeGn9gZzrjZA2oQVcFW aGOr2CT/53D4u28VNxxzKXreYW/ZZnivC2j6G1FYG7NTT7N6+HyPre2uVUIzxcmU7Gr5h9yDZjw SU23CPYeBc4oT27YdccvSB2AJao/7sb0= X-Received: by 2002:a9d:17e8:: with SMTP id j95mr4769667otj.263.1628193402192; Thu, 05 Aug 2021 12:56:42 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwbKL1Z6ZlWy8oXMm/ixD1+iTpzkkQ9GZc0Q+vhvUyuZDFc6t2uJRiqReogjxjxRR8x26HWk81QG7zKG+64mA4= X-Received: by 2002:a9d:17e8:: with SMTP id j95mr4769648otj.263.1628193401994; Thu, 05 Aug 2021 12:56:41 -0700 (PDT) MIME-Version: 1.0 References: <20210730201846.5147-1-niteesh.gs@gmail.com> <20210730201846.5147-7-niteesh.gs@gmail.com> In-Reply-To: <20210730201846.5147-7-niteesh.gs@gmail.com> From: John Snow Date: Thu, 5 Aug 2021 15:56:31 -0400 Message-ID: Subject: Re: [PATCH v3 06/13] python/aqmp-tui: Added type annotations for aqmp-tui To: G S Niteesh Babu Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=jsnow@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: multipart/alternative; boundary="0000000000005a182705c8d551b8" Received-SPF: pass client-ip=170.10.133.124; envelope-from=jsnow@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -34 X-Spam_score: -3.5 X-Spam_bar: --- X-Spam_report: (-3.5 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.699, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, HTML_MESSAGE=0.001, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Eduardo Habkost , Kashyap Chamarthy , Markus Armbruster , Wainer Moschetta , qemu-devel , Stefan Hajnoczi , Cleber Rosa , Eric Blake Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" --0000000000005a182705c8d551b8 Content-Type: text/plain; charset="UTF-8" On Fri, Jul 30, 2021 at 4:19 PM G S Niteesh Babu wrote: > This patch adds type annotations for aqmp-tui using > the mypy library. > > Awesome, thanks for taking a swing at this. Looks like it wasn't as bad as I was fearing. > Signed-off-by: G S Niteesh Babu > --- > python/qemu/aqmp/aqmp_tui.py | 79 ++++++++++++++++++++---------------- > python/setup.cfg | 3 -- > 2 files changed, 43 insertions(+), 39 deletions(-) > > diff --git a/python/qemu/aqmp/aqmp_tui.py b/python/qemu/aqmp/aqmp_tui.py > index ec9eba0aa7..ab9ada793a 100644 > --- a/python/qemu/aqmp/aqmp_tui.py > +++ b/python/qemu/aqmp/aqmp_tui.py > @@ -9,8 +9,15 @@ > import argparse > import asyncio > import logging > -from logging import Handler > +from logging import Handler, LogRecord > import signal > +from typing import ( > + Any, > + List, > + Optional, > + Tuple, > + Union, > +) > > import urwid > import urwid_readline > @@ -22,13 +29,13 @@ > from .util import create_task, pretty_traceback > > > -UPDATE_MSG = 'UPDATE_MSG' > +UPDATE_MSG: str = 'UPDATE_MSG' > > # Using root logger to enable all loggers under qemu and asyncio > LOGGER = logging.getLogger() > > > -def format_json(msg): > +def format_json(msg: str) -> str: > """ > Formats given multiline JSON message into a single line message. > Converting into single line is more asthetically pleasing when looking > @@ -43,17 +50,17 @@ def format_json(msg): > > > class App(QMPClient): > - def __init__(self, address): > + def __init__(self, address: Union[str, Tuple[str, int]]) -> None: > urwid.register_signal(type(self), UPDATE_MSG) > self.window = Window(self) > self.address = address > - self.aloop = None > + self.aloop: Optional[Any] = None # FIXME: Use more concrete type. > I ran into this in util.py; you want Optional[asyncio.AbstractEventLoop] here. > super().__init__() > > - def add_to_history(self, msg): > + def add_to_history(self, msg: str) -> None: > urwid.emit_signal(self, UPDATE_MSG, msg) > > - def _cb_outbound(self, msg): > + def _cb_outbound(self, msg: Message) -> Message: > # FIXME: I think the ideal way to omit these messages during > in-TUI > # logging will be to add a filter to the logger. We can use regex > to > # filter out messages starting with 'Request:' or 'Response:' but > I > @@ -67,25 +74,25 @@ def _cb_outbound(self, msg): > self.add_to_history('<-- ' + str(msg)) > return msg > > - def _cb_inbound(self, msg): > + def _cb_inbound(self, msg: Message) -> Message: > handler = LOGGER.handlers[0] > if not isinstance(handler, TUILogHandler): > LOGGER.debug('Response: %s', str(msg)) > self.add_to_history('--> ' + str(msg)) > return msg > > - async def wait_for_events(self): > + async def wait_for_events(self) -> None: > async for event in self.events: > self.handle_event(event) > > - async def _send_to_server(self, raw_msg): > + async def _send_to_server(self, raw_msg: str) -> None: > # FIXME: Format the raw_msg in history view to one line. It is not > # pleasing to see multiple lines JSON object with an error > statement. > try: > msg = Message(bytes(raw_msg, encoding='utf-8')) > # Format multiline json into a single line JSON, since it is > more > # pleasing to look along with err message in TUI. > - raw_msg = self.format_json(raw_msg) > + raw_msg = format_json(raw_msg) > await self._raw(msg, assign_id='id' not in msg) > except (ValueError, TypeError) as err: > LOGGER.info('Invalid message: %s', str(err)) > @@ -102,18 +109,18 @@ def _cb_inbound(self, msg): > LOGGER.error('Exception from _send_to_server: %s', str(err)) > raise err > > - def cb_send_to_server(self, msg): > + def cb_send_to_server(self, msg: str) -> None: > create_task(self._send_to_server(msg)) > > - def unhandled_input(self, key): > + def unhandled_input(self, key: str) -> None: > if key == 'esc': > self.kill_app() > > - def kill_app(self): > + def kill_app(self) -> None: > # TODO: Work on the disconnect logic > create_task(self._kill_app()) > > - async def _kill_app(self): > + async def _kill_app(self) -> None: > # It is ok to call disconnect even in disconnect state > try: > await self.disconnect() > @@ -124,7 +131,7 @@ def kill_app(self): > raise err > raise urwid.ExitMainLoop() > > - def handle_event(self, event): > + def handle_event(self, event: Message) -> None: > # FIXME: Consider all states present in qapi/run-state.json > if event['event'] == 'SHUTDOWN': > self._set_status('Server shutdown') > @@ -139,7 +146,7 @@ def _get_formatted_address(self) -> str: > addr = f'{host}:{port}' > return addr > > - async def connect_server(self): > + async def connect_server(self) -> None: > try: > await self.connect(self.address) > addr = self._get_formatted_address() > @@ -148,7 +155,7 @@ def _get_formatted_address(self) -> str: > LOGGER.info('connect_server: ConnectError %s', str(err)) > self._set_status('Server shutdown') > > - def run(self, debug=False): > + def run(self, debug: bool = False) -> None: > self.aloop = asyncio.get_event_loop() > self.aloop.set_debug(debug) > > @@ -176,7 +183,7 @@ class StatusBar(urwid.Text): > """ > A simple Text widget that currently only shows connection status. > """ > - def __init__(self, text=''): > + def __init__(self, text: str = ''): > super().__init__(text, align='right') > > > @@ -185,14 +192,14 @@ class Editor(urwid_readline.ReadlineEdit): > Support urwid_readline features along with > history support which lacks in urwid_readline > """ > - def __init__(self, master): > + def __init__(self, master: App) -> None: > super().__init__(caption='> ', multiline=True) > self.master = master > - self.history = [] > - self.last_index = -1 > - self.show_history = False > + self.history: List[str] = [] > + self.last_index: int = -1 > + self.show_history: bool = False > > - def keypress(self, size, key): > + def keypress(self, size: Tuple[int, int], key: str) -> Optional[str]: > # TODO: Add some logic for down key and clean up logic if > possible. > # Returning None means the key has been handled by this widget > # which otherwise is propogated to the parent widget to be > @@ -223,7 +230,7 @@ def keypress(self, size, key): > else: > self.show_history = False > self.last_index = 0 > - return super().keypress(size, key) > + return super().keypress(size, key) # type: ignore > try using cast(Optional[str], super().keypress(size, key)) instead of the type: ignore. It doesn't make a gigantic difference, really, but it looks "narrower" in scope and will probably confuse *me* less in the future. > return None > > > @@ -231,7 +238,7 @@ class EditorWidget(urwid.Filler): > """ > Wraps CustomEdit > """ > - def __init__(self, master): > + def __init__(self, master: App) -> None: > super().__init__(Editor(master), valign='top') > > > @@ -239,12 +246,12 @@ class HistoryBox(urwid.ListBox): > """ > Shows all the QMP message transmitted/received > """ > - def __init__(self, master): > + def __init__(self, master: App) -> None: > self.master = master > self.history = urwid.SimpleFocusListWalker([]) > super().__init__(self.history) > > - def add_to_history(self, history): > + def add_to_history(self, history: str) -> None: > self.history.append(urwid.Text(history)) > if self.history: > self.history.set_focus(len(self.history) - 1) > @@ -254,7 +261,7 @@ class HistoryWindow(urwid.Frame): > """ > Composes the HistoryBox and EditorWidget > """ > - def __init__(self, master): > + def __init__(self, master: App) -> None: > self.master = master > self.editor_widget = EditorWidget(master) > self.editor = urwid.LineBox(self.editor_widget) > @@ -264,7 +271,7 @@ def __init__(self, master): > super().__init__(self.body) > urwid.connect_signal(self.master, UPDATE_MSG, > self.cb_add_to_history) > > - def cb_add_to_history(self, msg): > + def cb_add_to_history(self, msg: str) -> None: > self.history.add_to_history(msg) > > > @@ -275,7 +282,7 @@ class Window(urwid.Frame): > future when we will have multiple windows and want to the switch > between > them and display overlays > """ > - def __init__(self, master): > + def __init__(self, master: App) -> None: > self.master = master > footer = StatusBar() > body = HistoryWindow(master) > @@ -283,18 +290,18 @@ def __init__(self, master): > > > class TUILogHandler(Handler): > - def __init__(self, tui): > + def __init__(self, tui: App) -> None: > super().__init__() > self.tui = tui > > - def emit(self, record): > + def emit(self, record: LogRecord) -> None: > level = record.levelname > msg = record.getMessage() > msg = f'[{level}]: {msg}' > self.tui.add_to_history(msg) > > > -def main(): > +def main() -> None: > parser = argparse.ArgumentParser(description='AQMP TUI') > parser.add_argument('qmp_server', help='Address of the QMP server' > '< UNIX socket path | TCP addr:port >') > @@ -311,7 +318,7 @@ def main(): > try: > address = QEMUMonitorProtocol.parse_address(args.qmp_server) > except QMPBadPortError as err: > - parser.error(err) > + parser.error(str(err)) > > app = App(address) > > @@ -330,4 +337,4 @@ def main(): > > > if __name__ == '__main__': > - main() # type: ignore > + main() > diff --git a/python/setup.cfg b/python/setup.cfg > index 8cd9ac0d81..11c6240aba 100644 > --- a/python/setup.cfg > +++ b/python/setup.cfg > @@ -83,9 +83,6 @@ namespace_packages = True > allow_subclassing_any = True > > [mypy-qemu.aqmp.aqmp_tui] > -disallow_untyped_defs = False > -disallow_incomplete_defs = False > -check_untyped_defs = False > # urwid and urwid_readline have no type stubs: > allow_subclassing_any = True > > -- > 2.17.1 > > Please squash this into the first patch, but it looks good, thanks! --js --0000000000005a182705c8d551b8 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


=
On Fri, Jul 30, 2021 at 4:19 PM G S N= iteesh Babu <niteesh.gs@gmail.co= m> wrote:
This patch adds type annotations for aqmp-tui using
the mypy library.


Awesome, thanks for taking a swing at = this. Looks like it wasn't as bad as I was fearing.
=C2= =A0
Signed-off-by: G S Niteesh Babu <niteesh.gs@gmail.com>
---
=C2=A0python/qemu/aqmp/aqmp_tui.py | 79 ++++++++++++++++++++---------------= -
=C2=A0python/setup.cfg=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0|=C2= =A0 3 --
=C2=A02 files changed, 43 insertions(+), 39 deletions(-)

diff --git a/python/qemu/aqmp/aqmp_tui.py b/python/qemu/aqmp/aqmp_tui.py index ec9eba0aa7..ab9ada793a 100644
--- a/python/qemu/aqmp/aqmp_tui.py
+++ b/python/qemu/aqmp/aqmp_tui.py
@@ -9,8 +9,15 @@
=C2=A0import argparse
=C2=A0import asyncio
=C2=A0import logging
-from logging import Handler
+from logging import Handler, LogRecord
=C2=A0import signal
+from typing import (
+=C2=A0 =C2=A0 Any,
+=C2=A0 =C2=A0 List,
+=C2=A0 =C2=A0 Optional,
+=C2=A0 =C2=A0 Tuple,
+=C2=A0 =C2=A0 Union,
+)

=C2=A0import urwid
=C2=A0import urwid_readline
@@ -22,13 +29,13 @@
=C2=A0from .util import create_task, pretty_traceback


-UPDATE_MSG =3D 'UPDATE_MSG'
+UPDATE_MSG: str =3D 'UPDATE_MSG'

=C2=A0# Using root logger to enable all loggers under qemu and asyncio
=C2=A0LOGGER =3D logging.getLogger()


-def format_json(msg):
+def format_json(msg: str) -> str:
=C2=A0 =C2=A0 =C2=A0"""
=C2=A0 =C2=A0 =C2=A0Formats given multiline JSON message into a single line= message.
=C2=A0 =C2=A0 =C2=A0Converting into single line is more asthetically pleasi= ng when looking
@@ -43,17 +50,17 @@ def format_json(msg):


=C2=A0class App(QMPClient):
-=C2=A0 =C2=A0 def __init__(self, address):
+=C2=A0 =C2=A0 def __init__(self, address: Union[str, Tuple[str, int]]) -&g= t; None:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0urwid.register_signal(type(self), UPDATE_= MSG)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0self.window =3D Window(self)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0self.address =3D address
-=C2=A0 =C2=A0 =C2=A0 =C2=A0 self.aloop =3D None
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 self.aloop: Optional[Any] =3D None=C2=A0 # FIX= ME: Use more concrete type.

I ran into = this in util.py; you want Optional[asyncio.AbstractEventLoop] here.
=C2=A0
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0super().__init__()

-=C2=A0 =C2=A0 def add_to_history(self, msg):
+=C2=A0 =C2=A0 def add_to_history(self, msg: str) -> None:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0urwid.emit_signal(self, UPDATE_MSG, msg)<= br>
-=C2=A0 =C2=A0 def _cb_outbound(self, msg):
+=C2=A0 =C2=A0 def _cb_outbound(self, msg: Message) -> Message:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0# FIXME: I think the ideal way to omit th= ese messages during in-TUI
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0# logging will be to add a filter to the = logger. We can use regex to
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0# filter out messages starting with '= Request:' or 'Response:' but I
@@ -67,25 +74,25 @@ def _cb_outbound(self, msg):
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0self.add_to_history('<-- ' + s= tr(msg))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0return msg

-=C2=A0 =C2=A0 def _cb_inbound(self, msg):
+=C2=A0 =C2=A0 def _cb_inbound(self, msg: Message) -> Message:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0handler =3D LOGGER.handlers[0]
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0if not isinstance(handler, TUILogHandler)= :
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0LOGGER.debug('Response:= %s', str(msg))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0self.add_to_history('--> ' + s= tr(msg))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0return msg

-=C2=A0 =C2=A0 async def wait_for_events(self):
+=C2=A0 =C2=A0 async def wait_for_events(self) -> None:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0async for event in self.events:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0self.handle_event(event)
-=C2=A0 =C2=A0 async def _send_to_server(self, raw_msg):
+=C2=A0 =C2=A0 async def _send_to_server(self, raw_msg: str) -> None: =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0# FIXME: Format the raw_msg in history vi= ew to one line. It is not
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0# pleasing to see multiple lines JSON obj= ect with an error statement.
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0try:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0msg =3D Message(bytes(raw_m= sg, encoding=3D'utf-8'))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0# Format multiline json int= o a single line JSON, since it is more
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0# pleasing to look along wi= th err message in TUI.
-=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 raw_msg =3D self.format_json(raw= _msg)
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 raw_msg =3D format_json(raw_msg)=
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0await self._raw(msg, assign= _id=3D'id' not in msg)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0except (ValueError, TypeError) as err: =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0LOGGER.info('Invalid me= ssage: %s', str(err))
@@ -102,18 +109,18 @@ def _cb_inbound(self, msg):
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0LOGGER.error('Exception= from _send_to_server: %s', str(err))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0raise err

-=C2=A0 =C2=A0 def cb_send_to_server(self, msg):
+=C2=A0 =C2=A0 def cb_send_to_server(self, msg: str) -> None:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0create_task(self._send_to_server(msg))
-=C2=A0 =C2=A0 def unhandled_input(self, key):
+=C2=A0 =C2=A0 def unhandled_input(self, key: str) -> None:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0if key =3D=3D 'esc':
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0self.kill_app()

-=C2=A0 =C2=A0 def kill_app(self):
+=C2=A0 =C2=A0 def kill_app(self) -> None:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0# TODO: Work on the disconnect logic
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0create_task(self._kill_app())

-=C2=A0 =C2=A0 async def _kill_app(self):
+=C2=A0 =C2=A0 async def _kill_app(self) -> None:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0# It is ok to call disconnect even in dis= connect state
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0try:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0await self.disconnect()
@@ -124,7 +131,7 @@ def kill_app(self):
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0raise err
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0raise urwid.ExitMainLoop()

-=C2=A0 =C2=A0 def handle_event(self, event):
+=C2=A0 =C2=A0 def handle_event(self, event: Message) -> None:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0# FIXME: Consider all states present in q= api/run-state.json
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0if event['event'] =3D=3D 'SHU= TDOWN':
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0self._set_status('Serve= r shutdown')
@@ -139,7 +146,7 @@ def _get_formatted_address(self) -> str:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0addr =3D f'{host}:{port= }'
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0return addr

-=C2=A0 =C2=A0 async def connect_server(self):
+=C2=A0 =C2=A0 async def connect_server(self) -> None:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0try:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0await self.connect(self.add= ress)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0addr =3D self._get_formatte= d_address()
@@ -148,7 +155,7 @@ def _get_formatted_address(self) -> str:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0LOGGER.info('connect_se= rver: ConnectError %s', str(err))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0self._set_status('Serve= r shutdown')

-=C2=A0 =C2=A0 def run(self, debug=3DFalse):
+=C2=A0 =C2=A0 def run(self, debug: bool =3D False) -> None:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0self.aloop =3D asyncio.get_event_loop() =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0self.aloop.set_debug(debug)

@@ -176,7 +183,7 @@ class StatusBar(urwid.Text):
=C2=A0 =C2=A0 =C2=A0"""
=C2=A0 =C2=A0 =C2=A0A simple Text widget that currently only shows connecti= on status.
=C2=A0 =C2=A0 =C2=A0"""
-=C2=A0 =C2=A0 def __init__(self, text=3D''):
+=C2=A0 =C2=A0 def __init__(self, text: str =3D ''):
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0super().__init__(text, align=3D'right= ')


@@ -185,14 +192,14 @@ class Editor(urwid_readline.ReadlineEdit):
=C2=A0 =C2=A0 =C2=A0Support urwid_readline features along with
=C2=A0 =C2=A0 =C2=A0history support which lacks in urwid_readline
=C2=A0 =C2=A0 =C2=A0"""
-=C2=A0 =C2=A0 def __init__(self, master):
+=C2=A0 =C2=A0 def __init__(self, master: App) -> None:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0super().__init__(caption=3D'> '= ;, multiline=3DTrue)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0self.master =3D master
-=C2=A0 =C2=A0 =C2=A0 =C2=A0 self.history =3D []
-=C2=A0 =C2=A0 =C2=A0 =C2=A0 self.last_index =3D -1
-=C2=A0 =C2=A0 =C2=A0 =C2=A0 self.show_history =3D False
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 self.history: List[str] =3D []
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 self.last_index: int =3D -1
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 self.show_history: bool =3D False

-=C2=A0 =C2=A0 def keypress(self, size, key):
+=C2=A0 =C2=A0 def keypress(self, size: Tuple[int, int], key: str) -> Op= tional[str]:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0# TODO: Add some logic for down key and c= lean up logic if possible.
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0# Returning None means the key has been h= andled by this widget
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0# which otherwise is propogated to the pa= rent widget to be
@@ -223,7 +230,7 @@ def keypress(self, size, key):
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0else:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0self.show_history =3D False=
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0self.last_index =3D 0
-=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 return super().keypress(size, ke= y)
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 return super().keypress(size, ke= y)=C2=A0 # type: ignore

try using cast(= Optional[str], super().keypress(size, key)) instead of the type: ignore. It= doesn't make a gigantic difference, really, but it looks "narrowe= r" in scope and will probably confuse *me* less in the future.
=C2=A0
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0return None


@@ -231,7 +238,7 @@ class EditorWidget(urwid.Filler):
=C2=A0 =C2=A0 =C2=A0"""
=C2=A0 =C2=A0 =C2=A0Wraps CustomEdit
=C2=A0 =C2=A0 =C2=A0"""
-=C2=A0 =C2=A0 def __init__(self, master):
+=C2=A0 =C2=A0 def __init__(self, master: App) -> None:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0super().__init__(Editor(master), valign= =3D'top')


@@ -239,12 +246,12 @@ class HistoryBox(urwid.ListBox):
=C2=A0 =C2=A0 =C2=A0"""
=C2=A0 =C2=A0 =C2=A0Shows all the QMP message transmitted/received
=C2=A0 =C2=A0 =C2=A0"""
-=C2=A0 =C2=A0 def __init__(self, master):
+=C2=A0 =C2=A0 def __init__(self, master: App) -> None:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0self.master =3D master
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0self.history =3D urwid.SimpleFocusListWal= ker([])
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0super().__init__(self.history)

-=C2=A0 =C2=A0 def add_to_history(self, history):
+=C2=A0 =C2=A0 def add_to_history(self, history: str) -> None:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0self.history.append(urwid.Text(history))<= br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0if self.history:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0self.history.set_focus(len(= self.history) - 1)
@@ -254,7 +261,7 @@ class HistoryWindow(urwid.Frame):
=C2=A0 =C2=A0 =C2=A0"""
=C2=A0 =C2=A0 =C2=A0Composes the HistoryBox and EditorWidget
=C2=A0 =C2=A0 =C2=A0"""
-=C2=A0 =C2=A0 def __init__(self, master):
+=C2=A0 =C2=A0 def __init__(self, master: App) -> None:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0self.master =3D master
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0self.editor_widget =3D EditorWidget(maste= r)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0self.editor =3D urwid.LineBox(self.editor= _widget)
@@ -264,7 +271,7 @@ def __init__(self, master):
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0super().__init__(self.body)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0urwid.connect_signal(self.master, UPDATE_= MSG, self.cb_add_to_history)

-=C2=A0 =C2=A0 def cb_add_to_history(self, msg):
+=C2=A0 =C2=A0 def cb_add_to_history(self, msg: str) -> None:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0self.history.add_to_history(msg)


@@ -275,7 +282,7 @@ class Window(urwid.Frame):
=C2=A0 =C2=A0 =C2=A0future when we will have multiple windows and want to t= he switch between
=C2=A0 =C2=A0 =C2=A0them and display overlays
=C2=A0 =C2=A0 =C2=A0"""
-=C2=A0 =C2=A0 def __init__(self, master):
+=C2=A0 =C2=A0 def __init__(self, master: App) -> None:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0self.master =3D master
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0footer =3D StatusBar()
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0body =3D HistoryWindow(master)
@@ -283,18 +290,18 @@ def __init__(self, master):


=C2=A0class TUILogHandler(Handler):
-=C2=A0 =C2=A0 def __init__(self, tui):
+=C2=A0 =C2=A0 def __init__(self, tui: App) -> None:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0super().__init__()
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0self.tui =3D tui

-=C2=A0 =C2=A0 def emit(self, record):
+=C2=A0 =C2=A0 def emit(self, record: LogRecord) -> None:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0level =3D record.levelname
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0msg =3D record.getMessage()
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0msg =3D f'[{level}]: {msg}'
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0self.tui.add_to_history(msg)


-def main():
+def main() -> None:
=C2=A0 =C2=A0 =C2=A0parser =3D argparse.ArgumentParser(description=3D'A= QMP TUI')
=C2=A0 =C2=A0 =C2=A0parser.add_argument('qmp_server', help=3D'A= ddress of the QMP server'
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0'< UNIX socket path | TCP addr:port >')
@@ -311,7 +318,7 @@ def main():
=C2=A0 =C2=A0 =C2=A0try:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0address =3D QEMUMonitorProtocol.parse_add= ress(args.qmp_server)
=C2=A0 =C2=A0 =C2=A0except QMPBadPortError as err:
-=C2=A0 =C2=A0 =C2=A0 =C2=A0 parser.error(err)
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 parser.error(str(err))

=C2=A0 =C2=A0 =C2=A0app =3D App(address)

@@ -330,4 +337,4 @@ def main():


=C2=A0if __name__ =3D=3D '__main__':
-=C2=A0 =C2=A0 main()=C2=A0 # type: ignore
+=C2=A0 =C2=A0 main()
diff --git a/python/setup.cfg b/python/setup.cfg
index 8cd9ac0d81..11c6240aba 100644
--- a/python/setup.cfg
+++ b/python/setup.cfg
@@ -83,9 +83,6 @@ namespace_packages =3D True
=C2=A0allow_subclassing_any =3D True

=C2=A0[mypy-qemu.aqmp.aqmp_tui]
-disallow_untyped_defs =3D False
-disallow_incomplete_defs =3D False
-check_untyped_defs =3D False
=C2=A0# urwid and urwid_readline have no type stubs:
=C2=A0allow_subclassing_any =3D True

--
2.17.1


Please squash this into the first patch, but it looks goo= d, thanks!

--js
--0000000000005a182705c8d551b8--