pass
+class __NotSet:
+ """ helper for __repr__ for when fields aren't set """
+
+ def __repr__(self):
+ return "<not set>"
+
+
+__NOT_SET = __NotSet()
+
+
def _decorator(cls, *, eq, unsafe_hash, order, repr_, frozen):
if not isinstance(cls, type):
raise TypeError(
def __repr__(self):
parts = []
for name in fields:
- parts.append(f"{name}={getattr(self, name)!r}")
+ parts.append(f"{name}={getattr(self, name, __NOT_SET)!r}")
return f"{self.__class__.__qualname__}({', '.join(parts)})"
add_method_or_error(__repr__)
self.z = z
+@plain_data()
+class UnsetField:
+ __slots__ = "a", "b"
+
+ def __init__(self, **kwargs):
+ for name, value in kwargs.items():
+ setattr(self, name, value)
+
+
class TestPlainData(unittest.TestCase):
def test_fields(self):
self.assertEqual(fields(PlainData0), ())
"PlainData2(a=1, b=2, x='x', y='y', z=3)")
self.assertEqual(repr(PlainDataF2(1, 2, x="x", y="y", z=3)),
"PlainDataF2(a=1, b=2, x='x', y='y', z=3)")
+ self.assertEqual(repr(UnsetField()),
+ "UnsetField(a=<not set>, b=<not set>)")
+ self.assertEqual(repr(UnsetField(a=2)), "UnsetField(a=2, b=<not set>)")
+ self.assertEqual(repr(UnsetField(b=3)), "UnsetField(a=<not set>, b=3)")
+ self.assertEqual(repr(UnsetField(a=5, b=3)), "UnsetField(a=5, b=3)")
def test_frozen(self):
not_frozen = PlainData0()