[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 30/37] qapi/introspect.py: Add a typed 'extra' structure
From: |
John Snow |
Subject: |
[PATCH 30/37] qapi/introspect.py: Add a typed 'extra' structure |
Date: |
Tue, 15 Sep 2020 18:40:20 -0400 |
Typing arbitrarily shaped dicts with mypy is difficult prior to Python
3.8; using explicit structures is nicer.
Since we always define an Extra type now, the return type of _make_tree
simplifies and always returns the tuple.
Signed-off-by: John Snow <jsnow@redhat.com>
---
scripts/qapi/introspect.py | 31 +++++++++++++++++++------------
1 file changed, 19 insertions(+), 12 deletions(-)
diff --git a/scripts/qapi/introspect.py b/scripts/qapi/introspect.py
index b036fcf9ce..41ca8afc67 100644
--- a/scripts/qapi/introspect.py
+++ b/scripts/qapi/introspect.py
@@ -10,6 +10,8 @@
See the COPYING file in the top-level directory.
"""
+from typing import (NamedTuple, Optional, Sequence)
+
from .common import (
c_name,
gen_endif,
@@ -21,16 +23,21 @@
QAPISchemaType)
-def _make_tree(obj, ifcond, features, extra=None):
- if extra is None:
- extra = {}
- if ifcond:
- extra['if'] = ifcond
+class Extra(NamedTuple):
+ """
+ Extra contains data that isn't intended for output by introspection.
+ """
+ comment: Optional[str] = None
+ ifcond: Sequence[str] = tuple()
+
+
+def _make_tree(obj, ifcond, features,
+ extra: Optional[Extra] = None):
+ comment = extra.comment if extra else None
+ extra = Extra(comment, ifcond)
if features:
- obj['features'] = [(f.name, {'if': f.ifcond}) for f in features]
- if extra:
- return (obj, extra)
- return obj
+ obj['features'] = [(f.name, Extra(None, f.ifcond)) for f in features]
+ return (obj, extra)
def _tree_to_qlit(obj, level=0, suppress_first_indent=False):
@@ -40,8 +47,8 @@ def indent(level):
if isinstance(obj, tuple):
ifobj, extra = obj
- ifcond = extra.get('if')
- comment = extra.get('comment')
+ ifcond = extra.ifcond
+ comment = extra.comment
ret = ''
if comment:
ret += indent(level) + '/* %s */\n' % comment
@@ -168,7 +175,7 @@ def _gen_tree(self, name, mtype, obj, ifcond, features):
if not self._unmask:
# Output a comment to make it easy to map masked names
# back to the source when reading the generated output.
- extra = {'comment': '"%s" = %s' % (self._name(name), name)}
+ extra = Extra(comment=f'"{self._name(name)}" = {name}')
name = self._name(name)
obj['name'] = name
obj['meta-type'] = mtype
--
2.26.2
- [PATCH 18/37] qapi/events.py: Move comments into docstrings, (continued)
- [PATCH 18/37] qapi/events.py: Move comments into docstrings, John Snow, 2020/09/15
- [PATCH 22/37] qapi/source.py: add notational type hints, John Snow, 2020/09/15
- [PATCH 19/37] qapi/commands.py: Don't re-bind to variable of different type, John Snow, 2020/09/15
- [PATCH 17/37] qapi/events.py: add notational type hints, John Snow, 2020/09/15
- [PATCH 20/37] qapi/commands.py: add notational type hints, John Snow, 2020/09/15
- [PATCH 23/37] qapi/source.py: delint with pylint, John Snow, 2020/09/15
- [PATCH 28/37] qapi/gen.py: update write() to be more idiomatic, John Snow, 2020/09/15
- [PATCH 29/37] qapi/gen.py: delint with pylint, John Snow, 2020/09/15
- [PATCH 27/37] qapi/gen.py: Remove unused parameter, John Snow, 2020/09/15
- [PATCH 26/37] qapi/gen.py: Enable checking with mypy, John Snow, 2020/09/15
- [PATCH 30/37] qapi/introspect.py: Add a typed 'extra' structure,
John Snow <=
- [PATCH 25/37] qapi/gen.py: add notational type hints, John Snow, 2020/09/15
- [PATCH 34/37] qapi/types.py: add notational type hints, John Snow, 2020/09/15
- [PATCH 36/37] qapi/visit.py: remove unused parameters from gen_visit_object, John Snow, 2020/09/15
- [PATCH 37/37] qapi/visit.py: add notational type hints, John Snow, 2020/09/15
- [PATCH 31/37] qapi/introspect.py: add _gen_features helper, John Snow, 2020/09/15
- [PATCH 32/37] qapi/introspect.py: create a typed 'Node' data structure, John Snow, 2020/09/15
- [PATCH 35/37] qapi/types.py: remove one-letter variables, John Snow, 2020/09/15
- [PATCH 33/37] qapi/introspect.py: add notational type hints, John Snow, 2020/09/15
- Re: [PATCH 00/37] qapi: static typing conversion, pt1, John Snow, 2020/09/16
- Re: [PATCH 00/37] qapi: static typing conversion, pt1, John Snow, 2020/09/17