protobuf icon indicating copy to clipboard operation
protobuf copied to clipboard

Python ParseFromString missing serialized when the data include the bytes or Any

Open rete-p-0214 opened this issue 3 years ago • 6 comments

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

rete-p-0214 avatar Oct 27 '21 20:10 rete-p-0214

Related: https://stackoverflow.com/questions/66099634/tensorflow-typeerror-parsefromstring-missing-1-required-positional-argument

This looks like a Python error.

elharo avatar Oct 29 '21 11:10 elharo

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 avatar Oct 29 '21 12:10 elharo

@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:

  1. The new version python changes may affect the file getting method or some function in pb2 library
  2. The pb2 library which generated by new version protoc may having bugs so the function not work.

rete-p-0214 avatar Oct 30 '21 03:10 rete-p-0214

I think you simply need to instantiate the Request object:

rq = request_pb2.Request()

np5 avatar Apr 29 '22 14:04 np5

Did the previous suggestion help fix the problem?

deannagarcia avatar Jul 14 '22 23:07 deannagarcia

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.

github-actions[bot] avatar Feb 16 '24 10:02 github-actions[bot]

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.

github-actions[bot] avatar Mar 02 '24 10:03 github-actions[bot]