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
This commit is contained in:
Dmitry Tantsur
2025-06-26 15:58:01 +02:00
parent 883e3cf057
commit ddeed6fa9e
2 changed files with 7 additions and 0 deletions

View File

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

View File

@@ -43,6 +43,8 @@ class TestSerializable(base.IronicAgentTest):
expected = {'jack': 'hello', 'jill': 'world'}
obj = SerializableTesting('hello', 'world')
self.assertEqual(expected, obj.serialize())
self.assertEqual(
"<SerializableTesting jack='hello' jill='world'>", repr(obj))
class TestSerializableComparable(base.IronicAgentTest):