Skip to contentSkip to conversation

qapi: Clean up for Python 3.8

PATCH In review Markus Armbruster 2 participants 7 messages Feb 27, 2025

lore
Share & tools
lore
#0 lore raw

cover qapi: Clean up for Python 3.8

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(-)
Signature · 1 line
2.48.1
#1 lore raw

PATCH 1/3 docs/about/build-platforms: Correct minimum supported Python version

Fixes ca056f4499c2 (Python: Drop support for Python 3.7)
---
 docs/about/build-platforms.rst | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
docs/about/build-platforms.rst +1 -1
index 482b09819c..1552b1a704 100644
@@ -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
Signature · 1 line
2.48.1
Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] docs/about/build-platforms: Correct minimum supported Python version

Quoted 5 lines · Markus Armbruster
Fixes ca056f4499c2 (Python: Drop support for Python 3.7)
---
 docs/about/build-platforms.rst | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

With regards,
Daniel

Signature · 3 lines
|: 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 :|
#3 lore raw

PATCH 2/3 qapi: Eliminate OrderedDict

In reply to Markus Armbruster

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.

---
 scripts/qapi/parser.py         |  5 ++---
 scripts/qapi/schema.py         | 11 +++++------
 tests/qapi-schema/test-qapi.py | 11 +----------
 3 files changed, 8 insertions(+), 19 deletions(-)
scripts/qapi/parser.py +2 -3
index adc85b5b39..64f0bb824a 100644
@@ -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
scripts/qapi/schema.py +5 -6
index 7f70969c09..cbe3b5aa91 100644
@@ -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))
tests/qapi-schema/test-qapi.py +1 -10
index 7e3f9f4aa1..8fe951c880 100755
@@ -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):
Signature · 1 line
2.48.1
Reviewed-by on v1 2/3
#4 lore raw

Re: [PATCH 2/3] qapi: Eliminate OrderedDict

Quoted 10 lines · Markus Armbruster

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.

---
 scripts/qapi/parser.py         |  5 ++---
 scripts/qapi/schema.py         | 11 +++++------
 tests/qapi-schema/test-qapi.py | 11 +----------
 3 files changed, 8 insertions(+), 19 deletions(-)

With regards,
Daniel

Signature · 3 lines
|: 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 :|
#5 lore raw

PATCH 3/3 qapi/introspect: Use @dataclass to simplify

In reply to Markus Armbruster

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.

---
 scripts/qapi/introspect.py | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)
scripts/qapi/introspect.py +5 -7
index 42e5185c7c..89ee5d5f17 100644
@@ -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,
Signature · 1 line
2.48.1
Reviewed-by on v1 3/3
#6 lore raw

Re: [PATCH 3/3] qapi/introspect: Use @dataclass to simplify

Quoted 10 lines · Markus Armbruster

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.

---
 scripts/qapi/introspect.py | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)

With regards,
Daniel

Signature · 3 lines
|: 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 :|