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 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 smtp.lore.kernel.org (Postfix) with ESMTPS id 74C16C02194 for ; Fri, 7 Feb 2025 18:57:24 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tgTWs-00013s-5V; Fri, 07 Feb 2025 13:56:42 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tgTWp-00013T-7d for qemu-devel@nongnu.org; Fri, 07 Feb 2025 13:56:39 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tgTWl-0003ur-Vs for qemu-devel@nongnu.org; Fri, 07 Feb 2025 13:56:38 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1738954592; 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=AhCXKEBu16WAD8C93Sg6ImNhML9szrFyJEDAX0+ai/M=; b=UV8iHVXIT8MFgqdOJCZ4uferhWx+g1FsF9wgmJjRWxdFnabt4IKAdhBra73eLjiI96I5QG Igv8e4lZ6E686Apg8CW0csogO2n52P1OinkZvWmNYI/9yVthoganoshFYX0xD9BLeVafFg BZYnnGlwNogFIKav2SMWZdwQeUHRw7Q= Received: from mail-pj1-f72.google.com (mail-pj1-f72.google.com [209.85.216.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-284-iLOYGisoMQihSQuqI3z1SQ-1; Fri, 07 Feb 2025 13:56:31 -0500 X-MC-Unique: iLOYGisoMQihSQuqI3z1SQ-1 X-Mimecast-MFC-AGG-ID: iLOYGisoMQihSQuqI3z1SQ Received: by mail-pj1-f72.google.com with SMTP id 98e67ed59e1d1-2f81a0d0a18so5143996a91.3 for ; Fri, 07 Feb 2025 10:56:31 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738954590; x=1739559390; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=AhCXKEBu16WAD8C93Sg6ImNhML9szrFyJEDAX0+ai/M=; b=mULZ2IiGHi6OvdZSztZUENMIMRwyvoaGDA1QKnzH31ulspzuNFgbfM4Lm2q+3dgWxb abD0tBvyRj5t9Qgwzn44vmvQ7yeikDsMNgEocowc3xOlKcTzVRBELUu7MRJMmp4PThfA Nd85xu/tnin6NEM1WU8jgvM9tntQC24fF5dBmVNMgevoZgPLeQOVi3Q82leSCFUba62D Y/r+QxSqCLp6lnZDxZzV6ASkg8J29hvLfhuS1f77KuAvmaidBuCeteZitPqnQKsbtzrv UcvElOPdCpZZlIizVtKVXdQoLQfezk80/pd5YJosEP1RKD3e0Tf23VwHcAIh1dBfzmpM Yllw== X-Forwarded-Encrypted: i=1; AJvYcCWapySdFDj2IMco2JubGctnpWHa3WixpxVZ0zUAdq9ar9SxVXTICO2dxGr9oI+8FfAimpFQtoJ8W1tH@nongnu.org X-Gm-Message-State: AOJu0YyIVb78pdiA+c8RGuuw7pPaaf+JPxp8EN99eMuXy0fpj56LteFT BavOfaX6B++hRwJ/Rrc1yM1HL+LWRwbtcfPRqWq0/nL0KvQkXMPdUOlBEpNqjnr/nd6bBn/2vPb AbVi+gQwQicuwSSClMHh1IfjzhIoKjnz/xYGbXkf/3IKQ9QFFGjf2lKm9+GqILiBvF7fcUkppu0 tgxN0skuZol1VEIPtlYgKDzqy96Bs= X-Gm-Gg: ASbGnctQPXff2rXl9L/9podZvF5wfTl8C+5VwRsI9PzZtwBDr0T8ETAMRY+SBW6t0zX 04/ISmXoqpLdwA+IYoityau7QO1hKijB1mTcXNzuRfHelS0LSvfRlunxxioZVhTI/XGGO63lAzT +C/KmkYDFKFtj0LX6lmg== X-Received: by 2002:a17:90a:ec86:b0:2fa:15ab:4de7 with SMTP id 98e67ed59e1d1-2fa24064f1dmr7498740a91.12.1738954590275; Fri, 07 Feb 2025 10:56:30 -0800 (PST) X-Google-Smtp-Source: AGHT+IG1g9i0nyyg8imkPYn7Ev3KiyFaSNI5O3vmMoxV6z6Fg/9uJ+hgqfEpI/5bwwsjwkXLVzJt/GjhaEH3ZPM0quE= X-Received: by 2002:a17:90a:ec86:b0:2fa:15ab:4de7 with SMTP id 98e67ed59e1d1-2fa24064f1dmr7498716a91.12.1738954590010; Fri, 07 Feb 2025 10:56:30 -0800 (PST) MIME-Version: 1.0 References: <20250205123550.2754387-1-berrange@redhat.com> <20250205123550.2754387-5-berrange@redhat.com> <87wme2hsrn.fsf@pond.sub.org> In-Reply-To: <87wme2hsrn.fsf@pond.sub.org> From: John Snow Date: Fri, 7 Feb 2025 13:56:18 -0500 X-Gm-Features: AWEUYZl3P2kPRE49SdVB249pgoBqL6hrG0SEY_1kioi0ZwAU5DHy5o4_gr2Il9Q Message-ID: Subject: Re: [PATCH v4 4/4] qapi: expose all schema features to code To: Markus Armbruster Cc: =?UTF-8?Q?Daniel_P=2E_Berrang=C3=A9?= , qemu-devel , =?UTF-8?B?TWFyYy1BbmRyw6kgTHVyZWF1?= , Michael Roth , =?UTF-8?Q?Philippe_Mathieu=2DDaud=C3=A9?= , Paolo Bonzini Content-Type: multipart/alternative; boundary="0000000000009e57e8062d91ea25" 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: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, 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_NONE=-0.0001, RCVD_IN_MSPIKE_H2=0.001, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=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.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org --0000000000009e57e8062d91ea25 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Fri, Feb 7, 2025, 6:57=E2=80=AFAM Markus Armbruster = wrote: > Daniel P. Berrang=C3=A9 writes: > > > This replaces use of the constants from the QapiSpecialFeatures > > enum, with constants from the auto-generate QapiFeatures enum > > in qapi-features.h > > > > The 'deprecated' and 'unstable' features still have a little bit of > > special handling, being force defined to be the 1st + 2nd features > > in the enum, regardless of whether they're used in the schema. This > > retains compatibility with common code that references the features > > via the QapiSpecialFeatures constants. > > > > Signed-off-by: Daniel P. Berrang=C3=A9 > > Daniel, feel free to ignore this at least for now. I'm trying to learn > some typing lore from John. > > v3 made mypy unhappy. I asked John for advice, and also posted a > solution involving ValuesView I hacked up myself. Daniel took it for > v4. > > John suggested to use List. > > I now wonder whether could use Iterable. > > I'll show the three solutions inline. > > John, thoughts? > ValuesView works just fine. It accurately describes what that function returns. I only avoided it in my fixup because it's a more obscure type and generally list is easier to work with as a first-class built in primitive type to the language. (read as: I didn't have to consult any docs to fix it up using List and I'm lazy.) Your solution describes precisely the type being returned (always good) and avoids any re-copying of data. Do be aware by caching the values view object in another object that you are keeping a "live reference" to the list of dict values that I think can change if the source dict changes. I doubt it matters, but you should know about that. The only design consideration you have now is what type you actually want to return and why. I think it barely matters, and I'm always going to opt for whatever is the least annoying for the patch author so I don't have to bore/torture them with python minutiae. As long as the tests pass (my first three patches in the dan-fixup branch I posted based on v3) I'm more than content. > [...] > > > diff --git a/scripts/qapi/features.py b/scripts/qapi/features.py > > new file mode 100644 > > index 0000000000..be3e5d03ff > > --- /dev/null > > +++ b/scripts/qapi/features.py > > @@ -0,0 +1,51 @@ > > +""" > > +QAPI features generator > > + > > +Copyright 2024 Red Hat > > + > > +This work is licensed under the terms of the GNU GPL, version 2. > > +# See the COPYING file in the top-level directory. > > +""" > > + > > +from typing import Dict, ValuesView > > + > > +from .common import c_enum_const, c_name > > +from .gen import QAPISchemaMonolithicCVisitor > > +from .schema import ( > > + QAPISchema, > > + QAPISchemaFeature, > > +) > > + > > + > > +class QAPISchemaGenFeatureVisitor(QAPISchemaMonolithicCVisitor): > > + > > + def __init__(self, prefix: str): > > + super().__init__( > > + prefix, 'qapi-features', > > + ' * Schema-defined QAPI features', > > + __doc__) > > + > > + self.features: ValuesView[QAPISchemaFeature] > > This is the ValuesView solution. > > The List solution: > > self.features: List[QAPISchemaFeature] =3D [] > > The Iterable solution: > > self.features: Iterable[QAPISchemaFeature] > > [...] > > > > diff --git a/scripts/qapi/schema.py b/scripts/qapi/schema.py > > index e97c978d38..7f70969c09 100644 > > --- a/scripts/qapi/schema.py > > +++ b/scripts/qapi/schema.py > > [...] > > > @@ -1147,6 +1161,9 @@ def __init__(self, fname: str): > > self._def_exprs(exprs) > > self.check() > > > > + def features(self) -> ValuesView[QAPISchemaFeature]: > > + return self._feature_dict.values() > > This is the ValuesView solution. > > The List solution: > > def features(self) -> List[QAPISchemaFeature]: > return list(self._feature_dict.values()) > > The Iterable solution: > > def features(self) -> Iterable[QAPISchemaFeature]: > return self._feature_dict.values() > > > > + > > def _def_entity(self, ent: QAPISchemaEntity) -> None: > > self._entity_list.append(ent) > > > > [...] > > --0000000000009e57e8062d91ea25 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


On Fri, Feb 7, 2025, 6:57=E2=80= =AFAM Markus Armbruster <armbru@red= hat.com> wrote:
Daniel P. Be= rrang=C3=A9 <berrange@redhat.com> writes:

> This replaces use of the constants from the QapiSpecialFeatures
> enum, with constants from the auto-generate QapiFeatures enum
> in qapi-features.h
>
> The 'deprecated' and 'unstable' features still have a = little bit of
> special handling, being force defined to be the 1st + 2nd features
> in the enum, regardless of whether they're used in the schema. Thi= s
> retains compatibility with common code that references the features > via the QapiSpecialFeatures constants.
>
> Signed-off-by: Daniel P. Berrang=C3=A9 <berrange@redhat.com>= ;

Daniel, feel free to ignore this at least for now.=C2=A0 I'm trying to = learn
some typing lore from John.

v3 made mypy unhappy.=C2=A0 I asked John for advice, and also posted a
solution involving ValuesView I hacked up myself.=C2=A0 Daniel took it for<= br> v4.

John suggested to use List.

I now wonder whether could use Iterable.

I'll show the three solutions inline.

John, thoughts?

ValuesView works just fine. It accurately describes what tha= t function returns. I only avoided it in my fixup because it's a more o= bscure type and generally list is easier to work with as a first-class buil= t in primitive type to the language.

(read as: I didn't have to consult any docs to fix it up u= sing List and I'm lazy.)

Your solution describes precisely the type being returned (always good= ) and avoids any re-copying of data.

Do be aware by caching the values view object in another objec= t that you are keeping a "live reference" to the list of dict val= ues that I think can change if the source dict changes. I doubt it matters,= but you should know about that.

The only design consideration you have now is what type you actua= lly want to return and why. I think it barely matters, and I'm always g= oing to opt for whatever is the least annoying for the patch author so I do= n't have to bore/torture them with python minutiae.

As long as the tests pass (my first three p= atches in the dan-fixup branch I posted based on v3) I'm more than cont= ent.


[...]

> diff --git a/scripts/qapi/features.py b/scripts/qapi/features.py
> new file mode 100644
> index 0000000000..be3e5d03ff
> --- /dev/null
> +++ b/scripts/qapi/features.py
> @@ -0,0 +1,51 @@
> +"""
> +QAPI features generator
> +
> +Copyright 2024 Red Hat
> +
> +This work is licensed under the terms of the GNU GPL, version 2.
> +# See the COPYING file in the top-level directory.
> +"""
> +
> +from typing import Dict, ValuesView
> +
> +from .common import c_enum_const, c_name
> +from .gen import QAPISchemaMonolithicCVisitor
> +from .schema import (
> +=C2=A0 =C2=A0 QAPISchema,
> +=C2=A0 =C2=A0 QAPISchemaFeature,
> +)
> +
> +
> +class QAPISchemaGenFeatureVisitor(QAPISchemaMonolithicCVisitor):
> +
> +=C2=A0 =C2=A0 def __init__(self, prefix: str):
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 super().__init__(
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 prefix, 'qapi-features&= #39;,
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ' * Schema-defined QAPI= features',
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 __doc__)
> +
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 self.features: ValuesView[QAPISchemaFeatu= re]

This is the ValuesView solution.

The List solution:

=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0self.features: List[QAPISchemaFeat= ure] =3D []

The Iterable solution:

=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0self.features: Iterable[QAPISchema= Feature]

[...]


> diff --git a/scripts/qapi/schema.py b/scripts/qapi/schema.py
> index e97c978d38..7f70969c09 100644
> --- a/scripts/qapi/schema.py
> +++ b/scripts/qapi/schema.py

[...]

> @@ -1147,6 +1161,9 @@ def __init__(self, fname: str):
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 self._def_exprs(exprs)
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 self.check()
>=C2=A0
> +=C2=A0 =C2=A0 def features(self) -> ValuesView[QAPISchemaFeature]:=
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 return self._feature_dict.values()

This is the ValuesView solution.

The List solution:

=C2=A0 =C2=A0 =C2=A0 =C2=A0def features(self) -> List[QAPISchemaFeature]= :
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0return list(self._feature_dict.val= ues())

The Iterable solution:

=C2=A0 =C2=A0 =C2=A0 =C2=A0def features(self) -> Iterable[QAPISchemaFeat= ure]:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0return self._feature_dict.values()=


> +
>=C2=A0 =C2=A0 =C2=A0 def _def_entity(self, ent: QAPISchemaEntity) ->= None:
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 self._entity_list.append(ent)
>=C2=A0

[...]

--0000000000009e57e8062d91ea25--