diff mbox series

[v2,05/19] qapi/schema: declare type for QAPISchemaArrayType.element_type

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

Commit Message

John Snow Jan. 12, 2024, 10:29 p.m. UTC
This field should always be present and defined after check() is
called. Declare the property but allow its initialization to be delayed
until check() so that it can be typed without the use of `Optional`.

This helps simplify typing by avoiding the need to interrogate the value
for None at multiple callsites; the overwhelming majority of uses assume
a fully-initialized object.

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

Comments

Markus Armbruster Jan. 15, 2024, 1:59 p.m. UTC | #1
John Snow <jsnow@redhat.com> writes:

> This field should always be present and defined after check() is
> called. Declare the property but allow its initialization to be delayed
> until check() so that it can be typed without the use of `Optional`.
>
> This helps simplify typing by avoiding the need to interrogate the value
> for None at multiple callsites; the overwhelming majority of uses assume
> a fully-initialized object.

If you like my version of the previous patch's commit message, we could
reuse it here.

> 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 48a51dcd188..e45d9545eda 100644
> --- a/scripts/qapi/schema.py
> +++ b/scripts/qapi/schema.py
> @@ -389,7 +389,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
John Snow Jan. 17, 2024, 4:06 p.m. UTC | #2
On Mon, Jan 15, 2024 at 8:59 AM Markus Armbruster <armbru@redhat.com> wrote:
>
> John Snow <jsnow@redhat.com> writes:
>
> > This field should always be present and defined after check() is
> > called. Declare the property but allow its initialization to be delayed
> > until check() so that it can be typed without the use of `Optional`.
> >
> > This helps simplify typing by avoiding the need to interrogate the value
> > for None at multiple callsites; the overwhelming majority of uses assume
> > a fully-initialized object.
>
> If you like my version of the previous patch's commit message, we could
> reuse it here.

Sure, I tweaked and re-used it.

--js

>
> > 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 48a51dcd188..e45d9545eda 100644
> > --- a/scripts/qapi/schema.py
> > +++ b/scripts/qapi/schema.py
> > @@ -389,7 +389,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
>
diff mbox series

Patch

diff --git a/scripts/qapi/schema.py b/scripts/qapi/schema.py
index 48a51dcd188..e45d9545eda 100644
--- a/scripts/qapi/schema.py
+++ b/scripts/qapi/schema.py
@@ -389,7 +389,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