[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v5 17/25] qapi/schema: fix typing for QAPISchemaVariants.tag_memb
From: |
Markus Armbruster |
Subject: |
[PATCH v5 17/25] qapi/schema: fix typing for QAPISchemaVariants.tag_member |
Date: |
Fri, 15 Mar 2024 16:22:53 +0100 |
From: John Snow <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>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
Signed-off-by: Markus Armbruster <armbru@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 74b0d7b007..9c138badb0 100644
--- a/scripts/qapi/schema.py
+++ b/scripts/qapi/schema.py
@@ -627,25 +627,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.lookup_type(self.tag_member.defined_in)
assert base_type
if not base_type.is_implicit():
@@ -666,11 +680,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.44.0
- [PATCH v5 09/25] qapi/schema: adjust type narrowing for mypy's benefit, (continued)
- [PATCH v5 09/25] qapi/schema: adjust type narrowing for mypy's benefit, Markus Armbruster, 2024/03/15
- [PATCH v5 16/25] qapi/schema: Don't initialize "members" with `None`, Markus Armbruster, 2024/03/15
- [PATCH v5 14/25] qapi/schema: assert info is present when necessary, Markus Armbruster, 2024/03/15
- [PATCH v5 12/25] qapi: Assert built-in types exist, Markus Armbruster, 2024/03/15
- [PATCH v5 20/25] qapi/parser.py: assert member.info is present in connect_member, Markus Armbruster, 2024/03/15
- [PATCH v5 22/25] qapi/schema: turn on mypy strictness, Markus Armbruster, 2024/03/15
- [PATCH v5 15/25] qapi/schema: add _check_complete flag, Markus Armbruster, 2024/03/15
- [PATCH v5 19/25] qapi/parser: demote QAPIExpression to Dict[str, Any], Markus Armbruster, 2024/03/15
- [PATCH v5 08/25] qapi/schema: make c_type() and json_type() abstract methods, Markus Armbruster, 2024/03/15
- [PATCH v5 17/25] qapi/schema: fix typing for QAPISchemaVariants.tag_member,
Markus Armbruster <=
- [PATCH v5 25/25] qapi: Dumb down QAPISchema.lookup_entity(), Markus Armbruster, 2024/03/15
- [PATCH v5 11/25] qapi/schema: assert resolve_type has 'info' and 'what' args on error, Markus Armbruster, 2024/03/15
- [PATCH v5 18/25] qapi/schema: assert inner type of QAPISchemaVariants in check_clash(), Markus Armbruster, 2024/03/15
- [PATCH v5 21/25] qapi/schema: add type hints, Markus Armbruster, 2024/03/15
- [PATCH v5 23/25] qapi/schema: remove unnecessary asserts, Markus Armbruster, 2024/03/15
- [PATCH v5 05/25] qapi: create QAPISchemaDefinition, Markus Armbruster, 2024/03/15
- [PATCH v5 01/25] qapi/parser: fix typo - self.returns.info => self.errors.info, Markus Armbruster, 2024/03/15
- [PATCH v5 07/25] qapi/schema: declare type for QAPISchemaArrayType.element_type, Markus Armbruster, 2024/03/15