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 <js...@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