From ddeed6fa9e8d03e78bd5ac6bdead8950b88a1f44 Mon Sep 17 00:00:00 2001 From: Dmitry Tantsur Date: Thu, 26 Jun 2025 15:58:01 +0200 Subject: [PATCH] Provide usable __repr__ for serializable objects These objects are frequently logged or compared in unit tests. It's very helpful to be able to inspect their content. Change-Id: Ib725dcd5f54f4492205f95974d887b8b42c74039 --- ironic_python_agent/encoding.py | 5 +++++ ironic_python_agent/tests/unit/test_encoding.py | 2 ++ 2 files changed, 7 insertions(+) diff --git a/ironic_python_agent/encoding.py b/ironic_python_agent/encoding.py index 715d13411..ec1f0874d 100644 --- a/ironic_python_agent/encoding.py +++ b/ironic_python_agent/encoding.py @@ -24,6 +24,11 @@ class Serializable(object): """Turn this object into a dict.""" return dict((f, getattr(self, f)) for f in self.serializable_fields) + def __repr__(self): + fields = " ".join(f"{f}=" + repr(getattr(self, f)) + for f in self.serializable_fields) + return f"<{self.__class__.__name__} {fields}>" + class SerializableComparable(Serializable): """A Serializable class which supports some comparison operators diff --git a/ironic_python_agent/tests/unit/test_encoding.py b/ironic_python_agent/tests/unit/test_encoding.py index 09b2fbcea..8770754b1 100644 --- a/ironic_python_agent/tests/unit/test_encoding.py +++ b/ironic_python_agent/tests/unit/test_encoding.py @@ -43,6 +43,8 @@ class TestSerializable(base.IronicAgentTest): expected = {'jack': 'hello', 'jill': 'world'} obj = SerializableTesting('hello', 'world') self.assertEqual(expected, obj.serialize()) + self.assertEqual( + "", repr(obj)) class TestSerializableComparable(base.IronicAgentTest):