protobuf
protobuf copied to clipboard
Python ParseFromString missing serialized when the data include the bytes or Any
What version of protobuf and what language are you using?
Version: 3.18.1
Language: Python
What operating system (Linux, Windows, ...) and version?
Mac OS 11.6
What runtime / compiler are you using (e.g., python version or gcc version)
Python 3.8.11
What did you do?
Request.proto
syntax = "proto3";
import "google/protobuf/any.proto";
message Request{
int64 timestamp = 1;
string region = 2;
google.protobuf.Any request = 3;
string token = 4;
}
decode.py
import request_pb2
import requests
# Simulate the request
f = open("req.bin", "rb")
rq = request_pb2.Request
rq.ParseFromString(f.read())
What did you expect to see
I will get something like the result of protoc
command :
protoc --decode=Request Request.proto < req.bin
timestamp:1625319236
region: "US"
request: "\n-type.googleapis................................."
token: ...
..
What did you see instead?
TypeError: ParseFromString() missing 1 required positional argument: 'serialized'
The error message is same when I change the Any
to bytes
, or change rb
to r
Make sure you include information that can help us debug (full error message, exception listing, stack trace, logs).
I also tried to change the Any to the structure of req.bin
, it works. But it is not a good solution to save all the request types on the api gateway server, and the library should be able to handle the data like protoc
command, So I report this issue as a bug.
Anything else we should know about your project / environment
I'm building the api gateway for redirecting the protobuf request to different server base on the region
, Also renew and replace the token if it is expired.
So I only need to read and edit the field in the Request.proto
and no need to deserialize the data in Any
Related: https://stackoverflow.com/questions/66099634/tensorflow-typeerror-parsefromstring-missing-1-required-positional-argument
This looks like a Python error.
I'm not a Python programmer but I'm guessing this line needs to be changed:
rq.ParseFromString(f.read())
I think the proto itself is fine.
@elharo Thanks for your reply. The stackoverflow one is about tensorflow. Assume that they are using same function in this protobuf library. The reason is new version not support that. And they change the get file method and the definition (may be something similar to the .proto
?) back to their v1.
So I guess the reason of this error is:
- The new version python changes may affect the file getting method or some function in
pb2
library - The
pb2
library which generated by new versionprotoc
may having bugs so the function not work.
I think you simply need to instantiate the Request object:
rq = request_pb2.Request()
Did the previous suggestion help fix the problem?
We triage inactive PRs and issues in order to make it easier to find active work. If this issue should remain active or becomes active again, please add a comment.
This issue is labeled inactive
because the last activity was over 90 days ago.
We triage inactive PRs and issues in order to make it easier to find active work. If this issue should remain active or becomes active again, please reopen it.
This issue was closed and archived because there has been no new activity in the 14 days since the inactive
label was added.