diff mbox series

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

Message ID 20240112222945.3033854-7-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
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(-)

Comments

Markus Armbruster Jan. 15, 2024, 2:03 p.m. UTC | #1
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):
John Snow Jan. 17, 2024, 4:11 p.m. UTC | #2
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):
>
diff mbox series

Patch

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):