The Python library pure_protobuf forces its users to use dataclasses, and decorate them with another decorator:
# to be clear: these two decorators are library code (external)
@message
@dataclass
class SearchRequest:
query: str = field(1, default='')
page_number: int32 = field(2, default=int32(0))
result_per_page: int32 = field(3, default=int32(0))
This @message
decorator assigns the SearchRequest
instance a method called dumps
:
SearchRequest(
query='hello',
page_number=int32(1),
result_per_page=int32(10),
).dumps() == b'\x0A\x05hello\x10\x01\x18\x0A'
In my application code, I have a specific use-case where I need to pass an object that has the dumps()
method. It can be a pure_protobuf
Message
instance like above, or it can be any other type, so long as it implements dumps()
.
It's working fine for classes that I've defined myself and implement the dumps()
"interface", but for pure_protobuf
data-classes, it keeps complaining that they have no attribute dumps()
.
What is making this more challenging is I'm not defining these pure_protobuf
data-classes myself, these will be defined by clients of my library, so I can't simply do something (silly) like:
@message
@dataclass
class SearchRequest:
query: str = field(1, default='')
page_number: int32 = field(2, default=int32(0))
result_per_page: int32 = field(3, default=int32(0))
def dumps(self):
self.dumps() # that is Message.dumps from the decorator
Am I out of options?