diff mbox series

[04/19] qapi/schema: declare type for QAPISchemaObjectTypeMember.type

Message ID 20231116014350.653792-5-jsnow@redhat.com (mailing list archive)
State New, archived
Headers show
Series qapi: statically type schema.py | expand

Commit Message

John Snow Nov. 16, 2023, 1:43 a.m. UTC
declare, but don't initialize the type of "type" to be QAPISchemaType -
and allow the value to be initialized during check().

This avoids the need for several "assert type is not None" statements
littered throughout the code by asserting it "will always be set."

It's a little hokey, but it works -- at the expense of slightly
incorrect type information before check() is called, anyway. If this
field is accessed before it is initialized, you'll be treated to an
AttributeError exception.

Fixes stuff like this:

qapi/schema.py:657: error: "None" has no attribute "alternate_qtype"  [attr-defined]
qapi/schema.py:662: error: "None" has no attribute "describe"  [attr-defined]

Signed-off-by: John Snow <jsnow@redhat.com>
---
 scripts/qapi/schema.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/scripts/qapi/schema.py b/scripts/qapi/schema.py
index 0fb44452dd5..c5fdd625452 100644
--- a/scripts/qapi/schema.py
+++ b/scripts/qapi/schema.py
@@ -771,7 +771,7 @@  def __init__(self, name, info, typ, optional, ifcond=None, features=None):
             assert isinstance(f, QAPISchemaFeature)
             f.set_defined_in(name)
         self._type_name = typ
-        self.type = None
+        self.type: QAPISchemaType  # set during check(). Kind of hokey.
         self.optional = optional
         self.features = features or []