From nobody Tue Nov 18 19:47:56 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1612288411; cv=none; d=zohomail.com; s=zohoarc; b=CTznx3iLhMN+/pX4zrc1t55/MDSrsDd1tZNjGxMFDQn+UcSRIAaxzvFrda59bizr5v335X+ho3P8CqMOMYskz7lMWjfNapt2MAoFxoSolOk7AZBNn+FSt5RwEzLHu6GPlSCoIIZJ0Eg2FzwpVv3eOy3ZOPCX7IsJyr2emfOZetM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1612288411; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=YH/r0keMcLdV84njaSdaWHfrPxmjV8hvfdDHSpdMn8A=; b=iKqyk8xaAWE/T5nHlBxLURG6ORWcJSWNBa5gGtA/2ojwiS3n33thW39zlIpJyb+q/fNGqytA0TAgb8BeDj+zgb/ma6/T32Qh8Sb0ICxKPfCpZFQgblPg9c/BeyaEJsfb625U0pbnK+BhFNe+hlUOe2fp4qG2GHzpfUGVqas37bo= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 161228841100436.199870170210374; Tue, 2 Feb 2021 09:53:31 -0800 (PST) Received: from localhost ([::1]:33008 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1l6zrl-0002f4-U4 for importer@patchew.org; Tue, 02 Feb 2021 12:53:29 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:43904) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1l6zlb-0004sk-Cn for qemu-devel@nongnu.org; Tue, 02 Feb 2021 12:47:07 -0500 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:40981) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1l6zlY-0008D2-S1 for qemu-devel@nongnu.org; Tue, 02 Feb 2021 12:47:07 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-462-AOVPclDqN1yW1fhQUiWjgA-1; Tue, 02 Feb 2021 12:47:01 -0500 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 7AA5C106BAE4; Tue, 2 Feb 2021 17:47:00 +0000 (UTC) Received: from scv.redhat.com (ovpn-112-247.rdu2.redhat.com [10.10.112.247]) by smtp.corp.redhat.com (Postfix) with ESMTP id BB0CE5C1CF; Tue, 2 Feb 2021 17:46:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1612288024; 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: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=YH/r0keMcLdV84njaSdaWHfrPxmjV8hvfdDHSpdMn8A=; b=B6CnyJw8i96eRMT2pZQnAtFd5MCQ7/bZEizEXlKeMnN0jPDQ3EHYdmTPsiaDUY8VtQnhbT aCFnbJVNlMOVq25FBupKHrq4bL8p4xGGQm0I8ApQK0q3Ca9aYhtne00uw19P2ExzmUHSe6 SQvT5VWNNC/Q8OIoJbN8wfCv93M6xBY= X-MC-Unique: AOVPclDqN1yW1fhQUiWjgA-1 From: John Snow To: qemu-devel@nongnu.org, Markus Armbruster Subject: [PATCH v4 08/14] qapi/introspect.py: create a typed 'Annotated' data strutcure Date: Tue, 2 Feb 2021 12:46:45 -0500 Message-Id: <20210202174651.2274166-9-jsnow@redhat.com> In-Reply-To: <20210202174651.2274166-1-jsnow@redhat.com> References: <20210202174651.2274166-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 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-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=63.128.21.124; envelope-from=jsnow@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -31 X-Spam_score: -3.2 X-Spam_bar: --- X-Spam_report: (-3.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.386, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, 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: Michael Roth , John Snow , Eduardo Habkost , Cleber Rosa Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Presently, we use a tuple to attach a dict containing annotations (comments and compile-time conditionals) to a tree node. This is undesirable because dicts are difficult to strongly type; promoting it to a real class allows us to name the values and types of the annotations we are expecting. In terms of typing, the Annotated type serves as a generic container where the annotated node's type is preserved, allowing for greater specificity than we'd be able to provide without a generic. Signed-off-by: John Snow --- scripts/qapi/introspect.py | 77 ++++++++++++++++++++++---------------- 1 file changed, 44 insertions(+), 33 deletions(-) diff --git a/scripts/qapi/introspect.py b/scripts/qapi/introspect.py index b82efe16f6e..2b90a52f016 100644 --- a/scripts/qapi/introspect.py +++ b/scripts/qapi/introspect.py @@ -13,8 +13,12 @@ from typing import ( Any, Dict, + Generic, + Iterable, List, Optional, + Tuple, + TypeVar, Union, ) =20 @@ -51,15 +55,25 @@ _scalar =3D Union[str, bool, None] _nonscalar =3D Union[Dict[str, _stub], List[_stub]] _value =3D Union[_scalar, _nonscalar] -# TreeValue =3D Union[_value, 'Annotated[_value]'] +TreeValue =3D Union[_value, 'Annotated[_value]'] =20 =20 -def _make_tree(obj, ifcond, comment=3DNone): - extra =3D { - 'if': ifcond, - 'comment': comment, - } - return (obj, extra) +_NodeT =3D TypeVar('_NodeT', bound=3DTreeValue) + + +class Annotated(Generic[_NodeT]): + """ + Annotated generally contains a SchemaInfo-like type (as a dict), + But it also used to wrap comments/ifconds around scalar leaf values, + for the benefit of features and enums. + """ + # Remove after 3.7 adds @dataclass: + # pylint: disable=3Dtoo-few-public-methods + def __init__(self, value: _NodeT, ifcond: Iterable[str], + comment: Optional[str] =3D None): + self.value =3D value + self.comment: Optional[str] =3D comment + self.ifcond: Tuple[str, ...] =3D tuple(ifcond) =20 =20 def _tree_to_qlit(obj, level=3D0, suppress_first_indent=3DFalse): @@ -67,24 +81,20 @@ def _tree_to_qlit(obj, level=3D0, suppress_first_indent= =3DFalse): def indent(level): return level * 4 * ' ' =20 - if isinstance(obj, tuple): - ifobj, extra =3D obj - ifcond =3D extra.get('if') - comment =3D extra.get('comment') - + if isinstance(obj, Annotated): # NB: _tree_to_qlit is called recursively on the values of a key:v= alue # pair; those values can't be decorated with comments or condition= als. msg =3D "dict values cannot have attached comments or if-condition= als." assert not suppress_first_indent, msg =20 ret =3D '' - if comment: - ret +=3D indent(level) + '/* %s */\n' % comment - if ifcond: - ret +=3D gen_if(ifcond) - ret +=3D _tree_to_qlit(ifobj, level) - if ifcond: - ret +=3D '\n' + gen_endif(ifcond) + if obj.comment: + ret +=3D indent(level) + '/* %s */\n' % obj.comment + if obj.ifcond: + ret +=3D gen_if(obj.ifcond) + ret +=3D _tree_to_qlit(obj.value, level, suppress_first_indent) + if obj.ifcond: + ret +=3D '\n' + gen_endif(obj.ifcond) return ret =20 ret =3D '' @@ -201,7 +211,7 @@ def _use_type(self, typ): =20 @staticmethod def _gen_features(features): - return [_make_tree(f.name, f.ifcond) for f in features] + return [Annotated(f.name, f.ifcond) for f in features] =20 def _gen_tree(self, name, mtype, obj, ifcond, features): comment: Optional[str] =3D None @@ -215,7 +225,7 @@ def _gen_tree(self, name, mtype, obj, ifcond, features): obj['meta-type'] =3D mtype if features: obj['features'] =3D self._gen_features(features) - self._trees.append(_make_tree(obj, ifcond, comment)) + self._trees.append(Annotated(obj, ifcond, comment)) =20 def _gen_member(self, member): obj =3D {'name': member.name, 'type': self._use_type(member.type)} @@ -223,7 +233,7 @@ def _gen_member(self, member): obj['default'] =3D None if member.features: obj['features'] =3D self._gen_features(member.features) - return _make_tree(obj, member.ifcond) + return Annotated(obj, member.ifcond) =20 def _gen_variants(self, tag_name, variants): return {'tag': tag_name, @@ -231,16 +241,17 @@ def _gen_variants(self, tag_name, variants): =20 def _gen_variant(self, variant): obj =3D {'case': variant.name, 'type': self._use_type(variant.type= )} - return _make_tree(obj, variant.ifcond) + return Annotated(obj, variant.ifcond) =20 def visit_builtin_type(self, name, info, json_type): self._gen_tree(name, 'builtin', {'json-type': json_type}, [], None) =20 def visit_enum_type(self, name, info, ifcond, features, members, prefi= x): - self._gen_tree(name, 'enum', - {'values': [_make_tree(m.name, m.ifcond, None) - for m in members]}, - ifcond, features) + self._gen_tree( + name, 'enum', + {'values': [Annotated(m.name, m.ifcond) for m in members]}, + ifcond, features + ) =20 def visit_array_type(self, name, info, ifcond, element_type): element =3D self._use_type(element_type) @@ -257,12 +268,12 @@ def visit_object_type_flat(self, name, info, ifcond, = features, self._gen_tree(name, 'object', obj, ifcond, features) =20 def visit_alternate_type(self, name, info, ifcond, features, variants): - self._gen_tree(name, 'alternate', - {'members': [ - _make_tree({'type': self._use_type(m.type)}, - m.ifcond, None) - for m in variants.variants]}, - ifcond, features) + self._gen_tree(name, 'alternate', {'members': [ + Annotated({'type': self._use_type(m.type)}, m.ifcond) + for m in variants.variants + ]}, + ifcond, features + ) =20 def visit_command(self, name, info, ifcond, features, arg_type, ret_type, gen, success_response, boxed, --=20 2.29.2