[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [PATCH 10/19] qapi/schema: make QAPISchemaArrayType.element_type non
|
From: |
Markus Armbruster |
|
Subject: |
Re: [PATCH 10/19] qapi/schema: make QAPISchemaArrayType.element_type non-Optional |
|
Date: |
Tue, 21 Nov 2023 15:27:59 +0100 |
|
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux) |
John Snow <jsnow@redhat.com> writes:
> This field should always be present and defined. Change this to be a
> runtime @property that can emit an error if it's called prior to
> check().
>
> This helps simplify typing by avoiding the need to interrogate the value
> for None at multiple callsites.
>
> RFC: Yes, this is a slightly different technique than the one I used for
> QAPISchemaObjectTypeMember.type;
In PATCH 04.
> I think I prefer this one as being a
> little less hokey, but it is more SLOC. Dealer's choice for which style
> wins out -- now you have an example of both.
Thanks for letting us see both.
I believe all the extra lines accomplish is a different exception
RuntimeError with a custom message vs. plain AttributeError.
I don't think the more elaborate exception is worth the extra code.
> Signed-off-by: John Snow <jsnow@redhat.com>
> ---
> scripts/qapi/schema.py | 13 +++++++++++--
> 1 file changed, 11 insertions(+), 2 deletions(-)
>
> diff --git a/scripts/qapi/schema.py b/scripts/qapi/schema.py
> index c9a194103e1..462acb2bb61 100644
> --- a/scripts/qapi/schema.py
> +++ b/scripts/qapi/schema.py
> @@ -366,7 +366,16 @@ 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: Optional[QAPISchemaType] = None
> +
> + @property
> + def element_type(self) -> QAPISchemaType:
> + if self._element_type is None:
> + raise RuntimeError(
> + "QAPISchemaArray has no element_type until "
> + "after check() has been run."
> + )
> + return self._element_type
>
> def need_has_if_optional(self):
> # When FOO is an array, we still need has_FOO to distinguish
> @@ -375,7 +384,7 @@ def need_has_if_optional(self):
>
> def check(self, schema):
> super().check(schema)
> - self.element_type = schema.resolve_type(
> + self._element_type = schema.resolve_type(
> self._element_type_name, self.info,
> self.info and self.info.defn_meta)
> assert not isinstance(self.element_type, QAPISchemaArrayType)
- Re: [PATCH 03/19] qapi/schema: name QAPISchemaInclude entities, (continued)
[PATCH 12/19] qapi/schema: split "checked" field into "checking" and "checked", John Snow, 2023/11/15
[PATCH 01/19] qapi/schema: fix QAPISchemaEntity.__repr__(), John Snow, 2023/11/15
[PATCH 06/19] qapi/schema: adjust type narrowing for mypy's benefit, John Snow, 2023/11/15
[PATCH 10/19] qapi/schema: make QAPISchemaArrayType.element_type non-Optional, John Snow, 2023/11/15
- Re: [PATCH 10/19] qapi/schema: make QAPISchemaArrayType.element_type non-Optional,
Markus Armbruster <=
[PATCH 05/19] qapi/schema: make c_type() and json_type() abstract methods, John Snow, 2023/11/15
- Re: [PATCH 05/19] qapi/schema: make c_type() and json_type() abstract methods, Philippe Mathieu-Daudé, 2023/11/16
- Re: [PATCH 05/19] qapi/schema: make c_type() and json_type() abstract methods, Markus Armbruster, 2023/11/21
- Re: [PATCH 05/19] qapi/schema: make c_type() and json_type() abstract methods, Daniel P . Berrangé, 2023/11/21
- Re: [PATCH 05/19] qapi/schema: make c_type() and json_type() abstract methods, John Snow, 2023/11/21
- Re: [PATCH 05/19] qapi/schema: make c_type() and json_type() abstract methods, Daniel P . Berrangé, 2023/11/21
- Re: [PATCH 05/19] qapi/schema: make c_type() and json_type() abstract methods, Markus Armbruster, 2023/11/22
- Re: [PATCH 05/19] qapi/schema: make c_type() and json_type() abstract methods, Daniel P . Berrangé, 2023/11/22
[PATCH 09/19] qapi/schema: assert info is present when necessary, John Snow, 2023/11/15