feat: added ParseFromString method to message for compatibility #336
Open
simon-saliba wants to merge 1 commit intodanielgtaylor:masterfrom
Open
feat: added ParseFromString method to message for compatibility #336simon-saliba wants to merge 1 commit intodanielgtaylor:masterfrom
simon-saliba wants to merge 1 commit intodanielgtaylor:masterfrom
Conversation
…official protobuf method
d0b5cd2 to
cb9161b
Compare
|
Thanks! |
cetanu
approved these changes
Feb 15, 2022
Gobot1234
requested changes
Feb 15, 2022
| return cls().parse(data) | ||
|
|
||
| # For compatibility with Google protobuf official implementation. | ||
| def ParseFromString(self, data: bytes) -> T: |
Collaborator
There was a problem hiding this comment.
Suggested change
| def ParseFromString(self, data: bytes) -> T: | |
| def ParseFromString(self: T, data: bytes) -> T: |
| :class:`Message` | ||
| The initialized message. | ||
| """ | ||
| return self.parse(data) |
Collaborator
There was a problem hiding this comment.
Isn't this slightly different from the offical implementation which clears any currently set values? https://github.com/protocolbuffers/protobuf/blob/2a2a9b6e64e0cda49b0e5377cad58c790087e1c9/python/google/protobuf/message.py#L193-L199
There was a problem hiding this comment.
This indeed seems to be the case.
I've added a simple example which shows the difference in result from ParseFromString with the one from this PR:
syntax = "proto3";
package some.package.name;
message Cat {
int64 age = 1;
int64 lives = 2;
}def ParseFromString(self, data: bytes):
return self.parse(data)
if __name__ == '__main__':
# Create bytes
cat = Cat(
age=5,
# lives=9,
)
data = cat.SerializeToString()
# Dynamically add ParseFromString to the Cat class
Cat.ParseFromString = ParseFromString
# ParseFromString for the betterproto cat and the pb2 cat
cat_betterproto = Cat(age=3, lives=8)
cat_pb2 = Cat_pb2(age=3, lives=8)
cat_betterproto.ParseFromString(data)
cat_pb2.ParseFromString(data)
print(cat_betterproto) # Cat(age=5, lives=8), lives are not cleared
print(cat_pb2) # age: 5, lives are cleared
assert cat_pb2.lives == 0
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR adds the ParseFromString attribute to the message class for compatibility with the official Google protobuf implementation and method exposure.
Closes #323