diff mbox series

[3/3] qapi/introspect: Use @dataclass to simplify

Message ID 20250227080757.3978333-4-armbru@redhat.com (mailing list archive)
State New, archived
Headers show
Series qapi: Clean up for Python 3.8 | expand

Commit Message

Markus Armbruster Feb. 27, 2025, 8:07 a.m. UTC
A TODO comment in class Annotated reminds us to simplify it once we
can use @dataclass, new in Python 3.7.  We have that now, so do it.

There's a similar comment in scripts/qapi/source.py, but I can't
figure out how to use @dataclass there.  Left for another day.

Signed-off-by: Markus Armbruster <armbru@redhat.com>
---
 scripts/qapi/introspect.py | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)

Comments

Daniel P. Berrangé Feb. 27, 2025, 9:09 a.m. UTC | #1
On Thu, Feb 27, 2025 at 09:07:57AM +0100, Markus Armbruster wrote:
> A TODO comment in class Annotated reminds us to simplify it once we
> can use @dataclass, new in Python 3.7.  We have that now, so do it.
> 
> There's a similar comment in scripts/qapi/source.py, but I can't
> figure out how to use @dataclass there.  Left for another day.
> 
> Signed-off-by: Markus Armbruster <armbru@redhat.com>
> ---
>  scripts/qapi/introspect.py | 12 +++++-------
>  1 file changed, 5 insertions(+), 7 deletions(-)

Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>


With regards,
Daniel
diff mbox series

Patch

diff --git a/scripts/qapi/introspect.py b/scripts/qapi/introspect.py
index 42e5185c7c..89ee5d5f17 100644
--- a/scripts/qapi/introspect.py
+++ b/scripts/qapi/introspect.py
@@ -11,6 +11,7 @@ 
 See the COPYING file in the top-level directory.
 """
 
+from dataclasses import dataclass
 from typing import (
     Any,
     Dict,
@@ -79,19 +80,16 @@ 
 _ValueT = TypeVar('_ValueT', bound=_Value)
 
 
+@dataclass
 class Annotated(Generic[_ValueT]):
     """
     Annotated generally contains a SchemaInfo-like type (as a dict),
     But it also used to wrap comments/ifconds around scalar leaf values,
     for the benefit of features and enums.
     """
-    # TODO: Remove after Python 3.7 adds @dataclass:
-    # pylint: disable=too-few-public-methods
-    def __init__(self, value: _ValueT, ifcond: QAPISchemaIfCond,
-                 comment: Optional[str] = None):
-        self.value = value
-        self.comment: Optional[str] = comment
-        self.ifcond = ifcond
+    value: _ValueT
+    ifcond: QAPISchemaIfCond
+    comment: Optional[str] = None
 
 
 def _tree_to_qlit(obj: JSONValue,