Open michaelosthege opened 2 years ago
I like the idea overall although I think it could be more transparent to type checkers, have you considered using typing.Annotated for storing the converter functions and then the actual type could be used to annotate this?
Syntax wise could look like
@dataclass(eq=False, repr=False)
class MyMessage(betterproto.Message):
dimnames: List[str] = betterproto.string_field(1)
arr: Annotated[
numpy.ndarray, betterproto.converter(from_array, to_array, npproto.Ndarray)
] = betterproto.message_field(2)
Interesting, I didn't know about typing.Annotated. Two questions:
I suppose we'd prefer to store as the user-defined type (eg. ndarray
or tuple
and only apply the converter for (de)serialization?
Yes I think this would be good as we could actually simplify the code which handles datetime and timedelta. Yeah I'd agree with the last comment as well
I have two cases where I'd like to convert field types automatically on creation/get/set.
I understand that this is a little complicated with dataclasses to begin with, but I was hoping that the fields and
Message
class might enable something. At least a similar idea was mentioned in #253.Both scenarios for automatic conversion are covered by the task of protobuffing NumPy arrays. I found two projects related to this--neither of them worked for the generic case:
The following protobuf doesn't cover all generic dtypes either, but it's easy to work with:
With betterproto, we get this generated dataclass:
npproto.py
πI wrote the following code to convert between actual NumPy arrays and
Ndarray
messages:utils.py
πHere are some tests for it:
With this we have two examples where automatic conversion would be neat:
tuple β list
andndarray β Ndarray
.This message has a need for both:
The generated dataclass:
The dataclass constructor doesn't automatically convert types, so creating a
MyMessage
with a tuple of dimnames is problematic:If the type annotation was
Tuple[str]
it would've worked.Likewise it would be neat if the conversion code from above would be applied automatically so we could do this:
Thoughts, @Gobot1234 ? Would it be feasible to add built-in support for NumPy arrays this way? Maybe via an API to register conversion functions into the created message types?