qapi: Eliminate OrderedDict

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>
Message-ID: <20250227080757.3978333-3-armbru@redhat.com>
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
This commit is contained in:
Markus Armbruster 2025-02-27 09:07:56 +01:00
parent 87c8b4fc3c
commit 5fbc8126ac
3 changed files with 8 additions and 19 deletions

View file

@ -14,7 +14,6 @@
# This work is licensed under the terms of the GNU GPL, version 2. # This work is licensed under the terms of the GNU GPL, version 2.
# See the COPYING file in the top-level directory. # See the COPYING file in the top-level directory.
from collections import OrderedDict
import os import os
import re import re
from typing import ( from typing import (
@ -154,7 +153,7 @@ class QAPISchemaParser:
"value of 'include' must be a string") "value of 'include' must be a string")
incl_fname = os.path.join(os.path.dirname(self._fname), incl_fname = os.path.join(os.path.dirname(self._fname),
include) include)
self._add_expr(OrderedDict({'include': incl_fname}), info) self._add_expr({'include': incl_fname}, info)
exprs_include = self._include(include, info, incl_fname, exprs_include = self._include(include, info, incl_fname,
self._included) self._included)
if exprs_include: if exprs_include:
@ -355,7 +354,7 @@ class QAPISchemaParser:
raise QAPIParseError(self, "stray '%s'" % match.group(0)) raise QAPIParseError(self, "stray '%s'" % match.group(0))
def get_members(self) -> Dict[str, object]: def get_members(self) -> Dict[str, object]:
expr: Dict[str, object] = OrderedDict() expr: Dict[str, object] = {}
if self.tok == '}': if self.tok == '}':
self.accept() self.accept()
return expr return expr

View file

@ -19,7 +19,6 @@
from __future__ import annotations from __future__ import annotations
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from collections import OrderedDict
import os import os
import re import re
from typing import ( from typing import (
@ -557,7 +556,7 @@ class QAPISchemaObjectType(QAPISchemaType):
super().check(schema) super().check(schema)
assert self._checked and not self._check_complete assert self._checked and not self._check_complete
seen = OrderedDict() seen = {}
if self._base_name: if self._base_name:
self.base = schema.resolve_type(self._base_name, self.info, self.base = schema.resolve_type(self._base_name, self.info,
"'base'") "'base'")
@ -1141,10 +1140,10 @@ class QAPISchema:
self.docs = parser.docs self.docs = parser.docs
self._entity_list: List[QAPISchemaEntity] = [] self._entity_list: List[QAPISchemaEntity] = []
self._entity_dict: Dict[str, QAPISchemaDefinition] = {} 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 # NB, values in the dict will identify the first encountered
# usage of a named feature only # 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. # All schemas get the names defined in the QapiSpecialFeature enum.
# Rely on dict iteration order matching insertion order so that # Rely on dict iteration order matching insertion order so that
@ -1454,7 +1453,7 @@ class QAPISchema:
ifcond = QAPISchemaIfCond(expr.get('if')) ifcond = QAPISchemaIfCond(expr.get('if'))
info = expr.info info = expr.info
features = self._make_features(expr.get('features'), info) features = self._make_features(expr.get('features'), info)
if isinstance(data, OrderedDict): if isinstance(data, dict):
data = self._make_implicit_object_type( data = self._make_implicit_object_type(
name, info, ifcond, name, info, ifcond,
'arg', self._make_members(data, info)) 'arg', self._make_members(data, info))
@ -1473,7 +1472,7 @@ class QAPISchema:
ifcond = QAPISchemaIfCond(expr.get('if')) ifcond = QAPISchemaIfCond(expr.get('if'))
info = expr.info info = expr.info
features = self._make_features(expr.get('features'), info) features = self._make_features(expr.get('features'), info)
if isinstance(data, OrderedDict): if isinstance(data, dict):
data = self._make_implicit_object_type( data = self._make_implicit_object_type(
name, info, ifcond, name, info, ifcond,
'arg', self._make_members(data, info)) 'arg', self._make_members(data, info))

View file

@ -96,17 +96,8 @@ class QAPISchemaTestVisitor(QAPISchemaVisitor):
@staticmethod @staticmethod
def _print_if(ifcond, indent=4): 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(): if ifcond.is_present():
print('%sif %s' % (' ' * indent, _massage(ifcond.ifcond))) print('%sif %s' % (' ' * indent, ifcond.ifcond))
@classmethod @classmethod
def _print_features(cls, features, indent=4): def _print_features(cls, features, indent=4):