Implement Message.__bool__ (#142)
* Implement Message.__bool__ with similar semantics to a collection, such that any value being set on the message (i.e. having a non-default value) make the Message value truthy . Co-authored-by: nat <n@natn.me>
This commit is contained in:
@@ -365,3 +365,33 @@ def test_message_repr():
|
||||
|
||||
assert repr(Test(name="Loki")) == "Test(name='Loki')"
|
||||
assert repr(Test(child=Test(), name="Loki")) == "Test(name='Loki', child=Test())"
|
||||
|
||||
|
||||
def test_bool():
|
||||
"""Messages should evaluate similarly to a collection
|
||||
>>> test = []
|
||||
>>> bool(test)
|
||||
... False
|
||||
>>> test.append(1)
|
||||
>>> bool(test)
|
||||
... True
|
||||
>>> del test[0]
|
||||
>>> bool(test)
|
||||
... False
|
||||
"""
|
||||
|
||||
@dataclass
|
||||
class Falsy(betterproto.Message):
|
||||
pass
|
||||
|
||||
@dataclass
|
||||
class Truthy(betterproto.Message):
|
||||
bar: int = betterproto.int32_field(1)
|
||||
|
||||
assert not Falsy()
|
||||
t = Truthy()
|
||||
assert not t
|
||||
t.bar = 1
|
||||
assert t
|
||||
t.bar = 0
|
||||
assert not t
|
||||
|
Reference in New Issue
Block a user