qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/3] qapi: Clean up for Python 3.8
@ 2025-02-27  8:07 Markus Armbruster
  2025-02-27  8:07 ` [PATCH 1/3] docs/about/build-platforms: Correct minimum supported Python version Markus Armbruster
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Markus Armbruster @ 2025-02-27  8:07 UTC (permalink / raw)
  To: qemu-devel; +Cc: michael.roth, jsnow

The QAPI code generator code still contains a few minor oddities to
make it work with versions of Python we no longer support.  Bury them.

I'm leaving two things for later because I don't know what to do about
them:

* scripts/qapi/source.py has a "Replace with @dataclass in Python
  3.7+" comment.

* scripts/qapi/introspect.py has a "Python 3.6 does not offer
  TypedDict constructs" comment.

While there, fix a missing version bump in build dependency
documentation.

Markus Armbruster (3):
  docs/about/build-platforms: Correct minimum supported Python version
  qapi: Eliminate OrderedDict
  qapi/introspect: Use @dataclass to simplify

 docs/about/build-platforms.rst |  2 +-
 scripts/qapi/introspect.py     | 12 +++++-------
 scripts/qapi/parser.py         |  5 ++---
 scripts/qapi/schema.py         | 11 +++++------
 tests/qapi-schema/test-qapi.py | 11 +----------
 5 files changed, 14 insertions(+), 27 deletions(-)

-- 
2.48.1



^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 1/3] docs/about/build-platforms: Correct minimum supported Python version
  2025-02-27  8:07 [PATCH 0/3] qapi: Clean up for Python 3.8 Markus Armbruster
@ 2025-02-27  8:07 ` Markus Armbruster
  2025-02-27  9:04   ` Daniel P. Berrangé
  2025-02-27  8:07 ` [PATCH 2/3] qapi: Eliminate OrderedDict Markus Armbruster
  2025-02-27  8:07 ` [PATCH 3/3] qapi/introspect: Use @dataclass to simplify Markus Armbruster
  2 siblings, 1 reply; 7+ messages in thread
From: Markus Armbruster @ 2025-02-27  8:07 UTC (permalink / raw)
  To: qemu-devel; +Cc: michael.roth, jsnow

Fixes: ca056f4499c2 (Python: Drop support for Python 3.7)
Signed-off-by: Markus Armbruster <armbru@redhat.com>
---
 docs/about/build-platforms.rst | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/docs/about/build-platforms.rst b/docs/about/build-platforms.rst
index 482b09819c..1552b1a704 100644
--- a/docs/about/build-platforms.rst
+++ b/docs/about/build-platforms.rst
@@ -101,7 +101,7 @@ Python runtime
   option of the ``configure`` script to point QEMU to a supported
   version of the Python runtime.
 
-  As of QEMU |version|, the minimum supported version of Python is 3.7.
+  As of QEMU |version|, the minimum supported version of Python is 3.8.
 
 Python build dependencies
   Some of QEMU's build dependencies are written in Python.  Usually these
-- 
2.48.1



^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH 2/3] qapi: Eliminate OrderedDict
  2025-02-27  8:07 [PATCH 0/3] qapi: Clean up for Python 3.8 Markus Armbruster
  2025-02-27  8:07 ` [PATCH 1/3] docs/about/build-platforms: Correct minimum supported Python version Markus Armbruster
@ 2025-02-27  8:07 ` Markus Armbruster
  2025-02-27  9:07   ` Daniel P. Berrangé
  2025-02-27  8:07 ` [PATCH 3/3] qapi/introspect: Use @dataclass to simplify Markus Armbruster
  2 siblings, 1 reply; 7+ messages in thread
From: Markus Armbruster @ 2025-02-27  8:07 UTC (permalink / raw)
  To: qemu-devel; +Cc: michael.roth, jsnow

We use OrderedDict to ensure dictionary order is insertion order.
Plain dict does that since Python 3.6, but it wasn't guaranteed until
3.7.  Since we have 3.7 now, replace OrderedDict by dict.

Signed-off-by: Markus Armbruster <armbru@redhat.com>
---
 scripts/qapi/parser.py         |  5 ++---
 scripts/qapi/schema.py         | 11 +++++------
 tests/qapi-schema/test-qapi.py | 11 +----------
 3 files changed, 8 insertions(+), 19 deletions(-)

diff --git a/scripts/qapi/parser.py b/scripts/qapi/parser.py
index adc85b5b39..64f0bb824a 100644
--- a/scripts/qapi/parser.py
+++ b/scripts/qapi/parser.py
@@ -14,7 +14,6 @@
 # This work is licensed under the terms of the GNU GPL, version 2.
 # See the COPYING file in the top-level directory.
 
-from collections import OrderedDict
 import os
 import re
 from typing import (
@@ -154,7 +153,7 @@ def _parse(self) -> None:
                                        "value of 'include' must be a string")
                 incl_fname = os.path.join(os.path.dirname(self._fname),
                                           include)
-                self._add_expr(OrderedDict({'include': incl_fname}), info)
+                self._add_expr({'include': incl_fname}, info)
                 exprs_include = self._include(include, info, incl_fname,
                                               self._included)
                 if exprs_include:
@@ -355,7 +354,7 @@ def accept(self, skip_comment: bool = True) -> None:
                 raise QAPIParseError(self, "stray '%s'" % match.group(0))
 
     def get_members(self) -> Dict[str, object]:
-        expr: Dict[str, object] = OrderedDict()
+        expr: Dict[str, object] = {}
         if self.tok == '}':
             self.accept()
             return expr
diff --git a/scripts/qapi/schema.py b/scripts/qapi/schema.py
index 7f70969c09..cbe3b5aa91 100644
--- a/scripts/qapi/schema.py
+++ b/scripts/qapi/schema.py
@@ -19,7 +19,6 @@
 from __future__ import annotations
 
 from abc import ABC, abstractmethod
-from collections import OrderedDict
 import os
 import re
 from typing import (
@@ -557,7 +556,7 @@ def check(self, schema: QAPISchema) -> None:
         super().check(schema)
         assert self._checked and not self._check_complete
 
-        seen = OrderedDict()
+        seen = {}
         if self._base_name:
             self.base = schema.resolve_type(self._base_name, self.info,
                                             "'base'")
@@ -1141,10 +1140,10 @@ def __init__(self, fname: str):
         self.docs = parser.docs
         self._entity_list: List[QAPISchemaEntity] = []
         self._entity_dict: Dict[str, QAPISchemaDefinition] = {}
-        self._module_dict: Dict[str, QAPISchemaModule] = OrderedDict()
+        self._module_dict: Dict[str, QAPISchemaModule] = {}
         # NB, values in the dict will identify the first encountered
         # usage of a named feature only
-        self._feature_dict: Dict[str, QAPISchemaFeature] = OrderedDict()
+        self._feature_dict: Dict[str, QAPISchemaFeature] = {}
 
         # All schemas get the names defined in the QapiSpecialFeature enum.
         # Rely on dict iteration order matching insertion order so that
@@ -1454,7 +1453,7 @@ def _def_command(self, expr: QAPIExpression) -> None:
         ifcond = QAPISchemaIfCond(expr.get('if'))
         info = expr.info
         features = self._make_features(expr.get('features'), info)
-        if isinstance(data, OrderedDict):
+        if isinstance(data, dict):
             data = self._make_implicit_object_type(
                 name, info, ifcond,
                 'arg', self._make_members(data, info))
@@ -1473,7 +1472,7 @@ def _def_event(self, expr: QAPIExpression) -> None:
         ifcond = QAPISchemaIfCond(expr.get('if'))
         info = expr.info
         features = self._make_features(expr.get('features'), info)
-        if isinstance(data, OrderedDict):
+        if isinstance(data, dict):
             data = self._make_implicit_object_type(
                 name, info, ifcond,
                 'arg', self._make_members(data, info))
diff --git a/tests/qapi-schema/test-qapi.py b/tests/qapi-schema/test-qapi.py
index 7e3f9f4aa1..8fe951c880 100755
--- a/tests/qapi-schema/test-qapi.py
+++ b/tests/qapi-schema/test-qapi.py
@@ -96,17 +96,8 @@ def _print_variants(variants):
 
     @staticmethod
     def _print_if(ifcond, indent=4):
-        # TODO Drop this hack after replacing OrderedDict by plain
-        # dict (requires Python 3.7)
-        def _massage(subcond):
-            if isinstance(subcond, str):
-                return subcond
-            if isinstance(subcond, list):
-                return [_massage(val) for val in subcond]
-            return {key: _massage(val) for key, val in subcond.items()}
-
         if ifcond.is_present():
-            print('%sif %s' % (' ' * indent, _massage(ifcond.ifcond)))
+            print('%sif %s' % (' ' * indent, ifcond.ifcond))
 
     @classmethod
     def _print_features(cls, features, indent=4):
-- 
2.48.1



^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH 3/3] qapi/introspect: Use @dataclass to simplify
  2025-02-27  8:07 [PATCH 0/3] qapi: Clean up for Python 3.8 Markus Armbruster
  2025-02-27  8:07 ` [PATCH 1/3] docs/about/build-platforms: Correct minimum supported Python version Markus Armbruster
  2025-02-27  8:07 ` [PATCH 2/3] qapi: Eliminate OrderedDict Markus Armbruster
@ 2025-02-27  8:07 ` Markus Armbruster
  2025-02-27  9:09   ` Daniel P. Berrangé
  2 siblings, 1 reply; 7+ messages in thread
From: Markus Armbruster @ 2025-02-27  8:07 UTC (permalink / raw)
  To: qemu-devel; +Cc: michael.roth, jsnow

A TODO comment in class Annotated reminds us to simplify it once we
can use @dataclass, new in Python 3.7.  We have that now, so do it.

There's a similar comment in scripts/qapi/source.py, but I can't
figure out how to use @dataclass there.  Left for another day.

Signed-off-by: Markus Armbruster <armbru@redhat.com>
---
 scripts/qapi/introspect.py | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)

diff --git a/scripts/qapi/introspect.py b/scripts/qapi/introspect.py
index 42e5185c7c..89ee5d5f17 100644
--- a/scripts/qapi/introspect.py
+++ b/scripts/qapi/introspect.py
@@ -11,6 +11,7 @@
 See the COPYING file in the top-level directory.
 """
 
+from dataclasses import dataclass
 from typing import (
     Any,
     Dict,
@@ -79,19 +80,16 @@
 _ValueT = TypeVar('_ValueT', bound=_Value)
 
 
+@dataclass
 class Annotated(Generic[_ValueT]):
     """
     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.
     """
-    # TODO: Remove after Python 3.7 adds @dataclass:
-    # pylint: disable=too-few-public-methods
-    def __init__(self, value: _ValueT, ifcond: QAPISchemaIfCond,
-                 comment: Optional[str] = None):
-        self.value = value
-        self.comment: Optional[str] = comment
-        self.ifcond = ifcond
+    value: _ValueT
+    ifcond: QAPISchemaIfCond
+    comment: Optional[str] = None
 
 
 def _tree_to_qlit(obj: JSONValue,
-- 
2.48.1



^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH 1/3] docs/about/build-platforms: Correct minimum supported Python version
  2025-02-27  8:07 ` [PATCH 1/3] docs/about/build-platforms: Correct minimum supported Python version Markus Armbruster
@ 2025-02-27  9:04   ` Daniel P. Berrangé
  0 siblings, 0 replies; 7+ messages in thread
From: Daniel P. Berrangé @ 2025-02-27  9:04 UTC (permalink / raw)
  To: Markus Armbruster; +Cc: qemu-devel, michael.roth, jsnow

On Thu, Feb 27, 2025 at 09:07:55AM +0100, Markus Armbruster wrote:
> Fixes: ca056f4499c2 (Python: Drop support for Python 3.7)
> Signed-off-by: Markus Armbruster <armbru@redhat.com>
> ---
>  docs/about/build-platforms.rst | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)

Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>


With regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 2/3] qapi: Eliminate OrderedDict
  2025-02-27  8:07 ` [PATCH 2/3] qapi: Eliminate OrderedDict Markus Armbruster
@ 2025-02-27  9:07   ` Daniel P. Berrangé
  0 siblings, 0 replies; 7+ messages in thread
From: Daniel P. Berrangé @ 2025-02-27  9:07 UTC (permalink / raw)
  To: Markus Armbruster; +Cc: qemu-devel, michael.roth, jsnow

On Thu, Feb 27, 2025 at 09:07:56AM +0100, Markus Armbruster wrote:
> We use OrderedDict to ensure dictionary order is insertion order.
> Plain dict does that since Python 3.6, but it wasn't guaranteed until
> 3.7.  Since we have 3.7 now, replace OrderedDict by dict.
> 
> Signed-off-by: Markus Armbruster <armbru@redhat.com>
> ---
>  scripts/qapi/parser.py         |  5 ++---
>  scripts/qapi/schema.py         | 11 +++++------
>  tests/qapi-schema/test-qapi.py | 11 +----------
>  3 files changed, 8 insertions(+), 19 deletions(-)

Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>


With regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 3/3] qapi/introspect: Use @dataclass to simplify
  2025-02-27  8:07 ` [PATCH 3/3] qapi/introspect: Use @dataclass to simplify Markus Armbruster
@ 2025-02-27  9:09   ` Daniel P. Berrangé
  0 siblings, 0 replies; 7+ messages in thread
From: Daniel P. Berrangé @ 2025-02-27  9:09 UTC (permalink / raw)
  To: Markus Armbruster; +Cc: qemu-devel, michael.roth, jsnow

On Thu, Feb 27, 2025 at 09:07:57AM +0100, Markus Armbruster wrote:
> A TODO comment in class Annotated reminds us to simplify it once we
> can use @dataclass, new in Python 3.7.  We have that now, so do it.
> 
> There's a similar comment in scripts/qapi/source.py, but I can't
> figure out how to use @dataclass there.  Left for another day.
> 
> Signed-off-by: Markus Armbruster <armbru@redhat.com>
> ---
>  scripts/qapi/introspect.py | 12 +++++-------
>  1 file changed, 5 insertions(+), 7 deletions(-)

Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>


With regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|



^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2025-02-27  9:09 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-02-27  8:07 [PATCH 0/3] qapi: Clean up for Python 3.8 Markus Armbruster
2025-02-27  8:07 ` [PATCH 1/3] docs/about/build-platforms: Correct minimum supported Python version Markus Armbruster
2025-02-27  9:04   ` Daniel P. Berrangé
2025-02-27  8:07 ` [PATCH 2/3] qapi: Eliminate OrderedDict Markus Armbruster
2025-02-27  9:07   ` Daniel P. Berrangé
2025-02-27  8:07 ` [PATCH 3/3] qapi/introspect: Use @dataclass to simplify Markus Armbruster
2025-02-27  9:09   ` Daniel P. Berrangé

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).