[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v4 07/23] qapi/schema: declare type for QAPISchemaArrayType.eleme
From: |
John Snow |
Subject: |
[PATCH v4 07/23] qapi/schema: declare type for QAPISchemaArrayType.element_type |
Date: |
Wed, 13 Mar 2024 00:41:11 -0400 |
A QAPISchemaArrayType's element type gets resolved only during .check().
We have QAPISchemaArrayType.__init__() initialize self.element_type =
None, and .check() assign the actual type. Using .element_type before
.check() is wrong, and hopefully crashes due to the value being None.
Works.
However, it makes for awkward typing. With .element_type:
Optional[QAPISchemaType], mypy is of course unable to see that it's None
before .check(), and a QAPISchemaType after. To help it over the hump,
we'd have to assert self.element_type is not None before all the (valid)
uses. The assertion catches invalid uses, but only at run time; mypy
can't flag them.
Instead, declare .element_type in .__init__() as QAPISchemaType
*without* initializing it. Using .element_type before .check() now
certainly crashes, which is an improvement. Mypy still can't flag
invalid uses, but that's okay.
Signed-off-by: John Snow <jsnow@redhat.com>
---
scripts/qapi/schema.py | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/scripts/qapi/schema.py b/scripts/qapi/schema.py
index 8440a7243d8..c549a4e3bd0 100644
--- a/scripts/qapi/schema.py
+++ b/scripts/qapi/schema.py
@@ -381,7 +381,7 @@ def __init__(self, name, info, element_type):
super().__init__(name, info, None)
assert isinstance(element_type, str)
self._element_type_name = element_type
- self.element_type = None
+ self.element_type: QAPISchemaType
def need_has_if_optional(self):
# When FOO is an array, we still need has_FOO to distinguish
--
2.44.0
- [PATCH v4 01/23] qapi/parser: fix typo - self.returns.info => self.errors.info, (continued)
- [PATCH v4 01/23] qapi/parser: fix typo - self.returns.info => self.errors.info, John Snow, 2024/03/13
- [PATCH v4 09/23] qapi/schema: adjust type narrowing for mypy's benefit, John Snow, 2024/03/13
- [PATCH v4 04/23] qapi/schema: add pylint suppressions, John Snow, 2024/03/13
- [PATCH v4 13/23] qapi/schema: fix QAPISchemaArrayType.check's call to resolve_type, John Snow, 2024/03/13
- [PATCH v4 02/23] qapi/parser: shush up pylint, John Snow, 2024/03/13
- [PATCH v4 12/23] qapi: use schema.resolve_type instead of schema.lookup_type, John Snow, 2024/03/13
- [PATCH v4 18/23] qapi/schema: assert inner type of QAPISchemaVariants in check_clash(), John Snow, 2024/03/13
- [PATCH v4 14/23] qapi/schema: assert info is present when necessary, John Snow, 2024/03/13
- [PATCH v4 07/23] qapi/schema: declare type for QAPISchemaArrayType.element_type,
John Snow <=
- [PATCH v4 05/23] qapi: create QAPISchemaDefinition, John Snow, 2024/03/13
[PATCH v4 17/23] qapi/schema: fix typing for QAPISchemaVariants.tag_member, John Snow, 2024/03/13
[PATCH v4 16/23] qapi/schema: Don't initialize "members" with `None`, John Snow, 2024/03/13