[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 01/25] qapi/schema: add Visitable mixin
From: |
John Snow |
Subject: |
[PATCH 01/25] qapi/schema: add Visitable mixin |
Date: |
Tue, 22 Sep 2020 18:44:37 -0400 |
Python doesn't have anything quite exactly like Traits, Interfaces, or
Mixins; but we can approximate it.
Add a 'Visitable' class that enforces a type signature for the visit()
method; this way, mypy is ensuring that even for otherwise unrelated
classes that do not inherit from a common base, this signature will
always be forced to be compatible.
Signed-off-by: John Snow <jsnow@redhat.com>
---
scripts/qapi/schema.py | 12 +++++++++---
1 file changed, 9 insertions(+), 3 deletions(-)
diff --git a/scripts/qapi/schema.py b/scripts/qapi/schema.py
index 51af0449f5..55434f5c82 100644
--- a/scripts/qapi/schema.py
+++ b/scripts/qapi/schema.py
@@ -25,7 +25,13 @@
from .parser import QAPISchemaParser
-class QAPISchemaEntity:
+class Visitable:
+ """Abstract duck that suggests a class is visitable."""
+ def visit(self, visitor: 'QAPISchemaVisitor') -> None:
+ raise NotImplementedError
+
+
+class QAPISchemaEntity(Visitable):
meta: Optional[str] = None
def __init__(self, name: str, info, doc, ifcond=None, features=None):
@@ -136,7 +142,7 @@ def visit_event(self, name, info, ifcond, features,
arg_type, boxed):
pass
-class QAPISchemaModule:
+class QAPISchemaModule(Visitable):
def __init__(self, name):
self.name = name
self._entity_list = []
@@ -812,7 +818,7 @@ def visit(self, visitor):
self.arg_type, self.boxed)
-class QAPISchema:
+class QAPISchema(Visitable):
def __init__(self, fname):
self.fname = fname
parser = QAPISchemaParser(fname)
--
2.26.2
- [PATCH 00/25] qapi: static typing conversion, pt6, John Snow, 2020/09/22
- [PATCH 01/25] qapi/schema: add Visitable mixin,
John Snow <=
- [PATCH 03/25] qapi/schema.py: add assert in stub methods, John Snow, 2020/09/22
- [PATCH 02/25] qapi/schema.py: Move meta-type into class instances, John Snow, 2020/09/22
- [PATCH 04/25] qapi/schema.py: constrain QAPISchemaObjectType base type, John Snow, 2020/09/22
- [PATCH 06/25] qapi/schema.py: constrain QAPISchemaEvent arg_type type, John Snow, 2020/09/22
- [PATCH 05/25] qapi/schema.py: constrain QAPISchemaObjectTypeMember arg_type type, John Snow, 2020/09/22
- [PATCH 09/25] qapi/schema.py: remove superfluous assert, John Snow, 2020/09/22
- [PATCH 07/25] qapi/schema.py: constrain tag_member type, John Snow, 2020/09/22
- [PATCH 14/25] qapi/schema.py: Assert variants of an object are also objects, John Snow, 2020/09/22
- [PATCH 10/25] qapi/schema.py: Add assertion to ifcond property, John Snow, 2020/09/22
- [PATCH 21/25] qapi/schema.py: disable pylint line limit, John Snow, 2020/09/22