File tree Expand file tree Collapse file tree 2 files changed +33
-0
lines changed Expand file tree Collapse file tree 2 files changed +33
-0
lines changed Original file line number Diff line number Diff line change @@ -1022,6 +1022,29 @@ def FromString(cls: Type[T], data: bytes) -> T:
1022
1022
"""
1023
1023
return cls ().parse (data )
1024
1024
1025
+ # For compatibility with Google protobuf official implementation.
1026
+ def ParseFromString (self , data : bytes ) -> T :
1027
+ """
1028
+ Parse the binary encoded Protobuf into this message instance. This
1029
+ returns the instance itself and is therefore assignable and chainable.
1030
+
1031
+ .. note::
1032
+ This is a method for compatibility with other libraries,
1033
+ you should really use :meth:`parse`.
1034
+
1035
+
1036
+ Parameters
1037
+ -----------
1038
+ data: :class:`bytes`
1039
+ The data to parse the protobuf from.
1040
+
1041
+ Returns
1042
+ --------
1043
+ :class:`Message`
1044
+ The initialized message.
1045
+ """
1046
+ return self .parse (data )
1047
+
1025
1048
def to_dict (
1026
1049
self , casing : Casing = Casing .CAMEL , include_default_values : bool = False
1027
1050
) -> Dict [str , Any ]:
Original file line number Diff line number Diff line change @@ -485,3 +485,13 @@ def test_service_argument__expected_parameter():
485
485
do_thing_request_parameter = sig .parameters ["do_thing_request" ]
486
486
assert do_thing_request_parameter .default is Parameter .empty
487
487
assert do_thing_request_parameter .annotation == "DoThingRequest"
488
+
489
+
490
+ def test_message_parse_from_string ():
491
+ @dataclass
492
+ class SimpleMessage (betterproto .Message ):
493
+ message : str = betterproto .string_field (1 )
494
+
495
+ test_message = SimpleMessage (message = "test message" )
496
+
497
+ assert test_message == test_message .ParseFromString (bytes (test_message ))
You can’t perform that action at this time.
0 commit comments