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:
		| @@ -516,6 +516,10 @@ class Message(ABC): | ||||
|         .. describe:: bytes(x) | ||||
|  | ||||
|             Calls :meth:`__bytes__`. | ||||
|  | ||||
|         .. describe:: bool(x) | ||||
|  | ||||
|             Calls :meth:`__bool__`. | ||||
|     """ | ||||
|  | ||||
|     _serialized_on_wire: bool | ||||
| @@ -606,6 +610,14 @@ class Message(ABC): | ||||
|  | ||||
|         super().__setattr__(attr, value) | ||||
|  | ||||
|     def __bool__(self) -> bool: | ||||
|         """True if the Message has any fields with non-default values.""" | ||||
|         return any( | ||||
|             self.__raw_get(field_name) | ||||
|             not in (PLACEHOLDER, self._get_field_default(field_name)) | ||||
|             for field_name in self._betterproto.meta_by_field_name | ||||
|         ) | ||||
|  | ||||
|     @property | ||||
|     def _betterproto(self) -> ProtoClassMetadata: | ||||
|         """ | ||||
|   | ||||
		Reference in New Issue
	
	Block a user