From: John Snow <jsnow@redhat.com>
To: qemu-devel@nongnu.org
Cc: Michael Roth <michael.roth@amd.com>,
Peter Maydell <peter.maydell@linaro.org>,
Markus Armbruster <armbru@redhat.com>,
John Snow <jsnow@redhat.com>
Subject: [PATCH v2 14/19] qapi/schema: fix typing for QAPISchemaVariants.tag_member
Date: Fri, 12 Jan 2024 17:29:40 -0500 [thread overview]
Message-ID: <20240112222945.3033854-15-jsnow@redhat.com> (raw)
In-Reply-To: <20240112222945.3033854-1-jsnow@redhat.com>
There are two related changes here:
(1) We need to perform type narrowing for resolving the type of
tag_member during check(), and
(2) tag_member is a delayed initialization field, but we can hide it
behind a property that raises an Exception if it's called too
early. This simplifies the typing in quite a few places and avoids
needing to assert that the "tag_member is not None" at a dozen
callsites, which can be confusing and suggest the wrong thing to a
drive-by contributor.
Signed-off-by: John Snow <jsnow@redhat.com>
---
scripts/qapi/schema.py | 22 +++++++++++++++++++---
1 file changed, 19 insertions(+), 3 deletions(-)
diff --git a/scripts/qapi/schema.py b/scripts/qapi/schema.py
index 0d9a70ab4cb..6afafdb8b0d 100644
--- a/scripts/qapi/schema.py
+++ b/scripts/qapi/schema.py
@@ -637,25 +637,39 @@ def __init__(self, tag_name, info, tag_member, variants):
assert isinstance(v, QAPISchemaVariant)
self._tag_name = tag_name
self.info = info
- self.tag_member = tag_member
+ self._tag_member: Optional[QAPISchemaObjectTypeMember] = tag_member
self.variants = variants
+ @property
+ def tag_member(self) -> 'QAPISchemaObjectTypeMember':
+ if self._tag_member is None:
+ raise RuntimeError(
+ "QAPISchemaVariants has no tag_member property until "
+ "after check() has been run."
+ )
+ return self._tag_member
+
def set_defined_in(self, name):
for v in self.variants:
v.set_defined_in(name)
def check(self, schema, seen):
if self._tag_name: # union
- self.tag_member = seen.get(c_name(self._tag_name))
+ # We need to narrow the member type:
+ tmp = seen.get(c_name(self._tag_name))
+ assert tmp is None or isinstance(tmp, QAPISchemaObjectTypeMember)
+ self._tag_member = tmp
+
base = "'base'"
# Pointing to the base type when not implicit would be
# nice, but we don't know it here
- if not self.tag_member or self._tag_name != self.tag_member.name:
+ if not self._tag_member or self._tag_name != self._tag_member.name:
raise QAPISemError(
self.info,
"discriminator '%s' is not a member of %s"
% (self._tag_name, base))
# Here we do:
+ assert self.tag_member.defined_in
base_type = schema.resolve_type(self.tag_member.defined_in)
if not base_type.is_implicit():
base = "base type '%s'" % self.tag_member.defined_in
@@ -675,11 +689,13 @@ def check(self, schema, seen):
"discriminator member '%s' of %s must not be conditional"
% (self._tag_name, base))
else: # alternate
+ assert self._tag_member
assert isinstance(self.tag_member.type, QAPISchemaEnumType)
assert not self.tag_member.optional
assert not self.tag_member.ifcond.is_present()
if self._tag_name: # union
# branches that are not explicitly covered get an empty type
+ assert self.tag_member.defined_in
cases = {v.name for v in self.variants}
for m in self.tag_member.type.members:
if m.name not in cases:
--
2.43.0
next prev parent reply other threads:[~2024-01-12 22:31 UTC|newest]
Thread overview: 46+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-01-12 22:29 [PATCH v2 00/19] qapi: statically type schema.py John Snow
2024-01-12 22:29 ` [PATCH v2 01/19] qapi: sort pylint suppressions John Snow
2024-01-15 12:18 ` Markus Armbruster
2024-01-15 19:58 ` John Snow
2024-01-16 6:48 ` Markus Armbruster
2024-01-12 22:29 ` [PATCH v2 02/19] qapi/schema: add " John Snow
2024-01-12 22:29 ` [PATCH v2 03/19] qapi: create QAPISchemaDefinition John Snow
2024-01-15 13:16 ` Markus Armbruster
2024-01-15 21:23 ` John Snow
2024-01-16 7:22 ` Markus Armbruster
2024-01-16 15:49 ` John Snow
2024-01-12 22:29 ` [PATCH v2 04/19] qapi/schema: declare type for QAPISchemaObjectTypeMember.type John Snow
2024-01-15 13:53 ` Markus Armbruster
2024-01-16 15:55 ` John Snow
2024-01-12 22:29 ` [PATCH v2 05/19] qapi/schema: declare type for QAPISchemaArrayType.element_type John Snow
2024-01-15 13:59 ` Markus Armbruster
2024-01-17 16:06 ` John Snow
2024-01-12 22:29 ` [PATCH v2 06/19] qapi/schema: make c_type() and json_type() abstract methods John Snow
2024-01-15 14:03 ` Markus Armbruster
2024-01-17 16:11 ` John Snow
2024-01-12 22:29 ` [PATCH v2 07/19] qapi/schema: adjust type narrowing for mypy's benefit John Snow
2024-01-12 22:29 ` [PATCH v2 08/19] qapi/schema: add type narrowing to lookup_type() John Snow
2024-01-12 22:29 ` [PATCH v2 09/19] qapi/schema: allow resolve_type to be used for built-in types John Snow
2024-01-16 11:09 ` Markus Armbruster
2024-01-17 16:44 ` John Snow
2024-01-22 13:12 ` Markus Armbruster
2024-01-31 22:28 ` John Snow
2024-01-31 23:04 ` John Snow
2024-01-12 22:29 ` [PATCH v2 10/19] qapi: use schema.resolve_type instead of schema.lookup_type John Snow
2024-01-12 22:29 ` [PATCH v2 11/19] qapi/schema: fix QAPISchemaArrayType.check's call to resolve_type John Snow
2024-01-16 12:17 ` Markus Armbruster
2024-01-17 16:48 ` John Snow
2024-01-17 20:00 ` Markus Armbruster
2024-01-12 22:29 ` [PATCH v2 12/19] qapi/schema: assert info is present when necessary John Snow
2024-01-16 13:37 ` Markus Armbruster
2024-01-12 22:29 ` [PATCH v2 13/19] qapi/schema: split "checked" field into "checking" and "checked" John Snow
2024-01-16 14:58 ` Markus Armbruster
2024-02-01 19:41 ` John Snow
2024-02-01 19:57 ` John Snow
2024-01-12 22:29 ` John Snow [this message]
2024-01-17 8:22 ` [PATCH v2 14/19] qapi/schema: fix typing for QAPISchemaVariants.tag_member Markus Armbruster
2024-01-12 22:29 ` [PATCH v2 15/19] qapi/schema: assert inner type of QAPISchemaVariants in check_clash() John Snow
2024-01-12 22:29 ` [PATCH v2 16/19] qapi/parser: demote QAPIExpression to Dict[str, Any] John Snow
2024-01-12 22:29 ` [PATCH v2 17/19] qapi/schema: add type hints John Snow
2024-01-12 22:29 ` [PATCH v2 18/19] qapi/schema: turn on mypy strictness John Snow
2024-01-12 22:29 ` [PATCH v2 19/19] qapi/schema: remove unnecessary asserts John Snow
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240112222945.3033854-15-jsnow@redhat.com \
--to=jsnow@redhat.com \
--cc=armbru@redhat.com \
--cc=michael.roth@amd.com \
--cc=peter.maydell@linaro.org \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).