[PATCH v2 06/19] qapi/schema: make c_type() and json_type() abstract methods

John Snow posted 19 patches 10 months ago
Maintainers: Markus Armbruster <armbru@redhat.com>, Michael Roth <michael.roth@amd.com>
There is a newer version of this series
[PATCH v2 06/19] qapi/schema: make c_type() and json_type() abstract methods
Posted by John Snow 10 months ago
These methods should always return a str, it's only the default abstract
implementation that doesn't. They can be marked "abstract", which
requires subclasses to override the method with the proper return type.

Signed-off-by: John Snow <jsnow@redhat.com>
---
 scripts/qapi/schema.py | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/scripts/qapi/schema.py b/scripts/qapi/schema.py
index e45d9545eda..8e25dd35562 100644
--- a/scripts/qapi/schema.py
+++ b/scripts/qapi/schema.py
@@ -16,6 +16,7 @@
 
 # TODO catching name collisions in generated code would be nice
 
+from abc import ABC, abstractmethod
 from collections import OrderedDict
 import os
 import re
@@ -253,10 +254,11 @@ def visit(self, visitor):
         visitor.visit_include(self._sub_module.name, self.info)
 
 
-class QAPISchemaType(QAPISchemaDefinition):
+class QAPISchemaType(QAPISchemaDefinition, ABC):
     # Return the C type for common use.
     # For the types we commonly box, this is a pointer type.
-    def c_type(self):
+    @abstractmethod
+    def c_type(self) -> str:
         pass
 
     # Return the C type to be used in a parameter list.
@@ -267,7 +269,8 @@ def c_param_type(self):
     def c_unboxed_type(self):
         return self.c_type()
 
-    def json_type(self):
+    @abstractmethod
+    def json_type(self) -> str:
         pass
 
     def alternate_qtype(self):
-- 
2.43.0
Re: [PATCH v2 06/19] qapi/schema: make c_type() and json_type() abstract methods
Posted by Markus Armbruster 10 months ago
John Snow <jsnow@redhat.com> writes:

> These methods should always return a str, it's only the default abstract
> implementation that doesn't. They can be marked "abstract", which
> requires subclasses to override the method with the proper return type.
>
> Signed-off-by: John Snow <jsnow@redhat.com>
> ---
>  scripts/qapi/schema.py | 9 ++++++---
>  1 file changed, 6 insertions(+), 3 deletions(-)
>
> diff --git a/scripts/qapi/schema.py b/scripts/qapi/schema.py
> index e45d9545eda..8e25dd35562 100644
> --- a/scripts/qapi/schema.py
> +++ b/scripts/qapi/schema.py
> @@ -16,6 +16,7 @@
>  
>  # TODO catching name collisions in generated code would be nice
>  
> +from abc import ABC, abstractmethod
>  from collections import OrderedDict
>  import os
>  import re
> @@ -253,10 +254,11 @@ def visit(self, visitor):
>          visitor.visit_include(self._sub_module.name, self.info)
>  
>  
> -class QAPISchemaType(QAPISchemaDefinition):
> +class QAPISchemaType(QAPISchemaDefinition, ABC):
>      # Return the C type for common use.
>      # For the types we commonly box, this is a pointer type.
> -    def c_type(self):
> +    @abstractmethod
> +    def c_type(self) -> str:

You additionally add the type hint.  Suggest to either mention it in the
commit message, or add it only together with the other type hints in
PATCH 17.

>          pass
>  
>      # Return the C type to be used in a parameter list.
> @@ -267,7 +269,8 @@ def c_param_type(self):
>      def c_unboxed_type(self):
>          return self.c_type()
>  
> -    def json_type(self):
> +    @abstractmethod
> +    def json_type(self) -> str:

Likewise.

>          pass
>  
>      def alternate_qtype(self):
Re: [PATCH v2 06/19] qapi/schema: make c_type() and json_type() abstract methods
Posted by John Snow 10 months ago
On Mon, Jan 15, 2024 at 9:03 AM Markus Armbruster <armbru@redhat.com> wrote:
>
> John Snow <jsnow@redhat.com> writes:
>
> > These methods should always return a str, it's only the default abstract
> > implementation that doesn't. They can be marked "abstract", which
> > requires subclasses to override the method with the proper return type.
> >
> > Signed-off-by: John Snow <jsnow@redhat.com>
> > ---
> >  scripts/qapi/schema.py | 9 ++++++---
> >  1 file changed, 6 insertions(+), 3 deletions(-)
> >
> > diff --git a/scripts/qapi/schema.py b/scripts/qapi/schema.py
> > index e45d9545eda..8e25dd35562 100644
> > --- a/scripts/qapi/schema.py
> > +++ b/scripts/qapi/schema.py
> > @@ -16,6 +16,7 @@
> >
> >  # TODO catching name collisions in generated code would be nice
> >
> > +from abc import ABC, abstractmethod
> >  from collections import OrderedDict
> >  import os
> >  import re
> > @@ -253,10 +254,11 @@ def visit(self, visitor):
> >          visitor.visit_include(self._sub_module.name, self.info)
> >
> >
> > -class QAPISchemaType(QAPISchemaDefinition):
> > +class QAPISchemaType(QAPISchemaDefinition, ABC):
> >      # Return the C type for common use.
> >      # For the types we commonly box, this is a pointer type.
> > -    def c_type(self):
> > +    @abstractmethod
> > +    def c_type(self) -> str:
>
> You additionally add the type hint.  Suggest to either mention it in the
> commit message, or add it only together with the other type hints in
> PATCH 17.

Okie-dokey. (moved type hints to the big patch)

>
> >          pass
> >
> >      # Return the C type to be used in a parameter list.
> > @@ -267,7 +269,8 @@ def c_param_type(self):
> >      def c_unboxed_type(self):
> >          return self.c_type()
> >
> > -    def json_type(self):
> > +    @abstractmethod
> > +    def json_type(self) -> str:
>
> Likewise.
>
> >          pass
> >
> >      def alternate_qtype(self):
>