Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide TypedDict types for json mapping of protobuf #131

Open
dilipvamsi opened this issue Jun 9, 2020 · 6 comments
Open

Provide TypedDict types for json mapping of protobuf #131

dilipvamsi opened this issue Jun 9, 2020 · 6 comments

Comments

@dilipvamsi
Copy link

Hi,
It would be great if json_format definitions are also created while compiling the proto file.

Thanks,
Dilip Vamsi.

@nipunn1313
Copy link
Owner

Hi - looks like it is in typeshed https://github.com/python/typeshed/blob/master/third_party/2and3/google/protobuf/json_format.pyi

Is there anything more specific you'd like to have in the autogenerated pyi files?
Sorry - I am not familiar with json_format and would appreciate some more details to bring me up to speed.

@nipunn1313
Copy link
Owner

based on my readthrough, the typing in typeshed covers the json_format. It's not clear to me what autogenerated .pyi definitions would bring to the table. PRs are welcome!

@dilipvamsi
Copy link
Author

Hi,
Sorry, what I meant was json mapping https://developers.google.com/protocol-buffers/docs/proto3#json_mapping not json format.
If this is present in pyi file. It is will help in building a dict in accordance with the protobuf.
Using this I need not create an protobuf object and then convert to dict.

@nipunn1313
Copy link
Owner

What annotation are you looking for? Can you provide some example code which is under-annotated today?

It looks like these json_mapping methods are annotated in typeshed. Is there anything more specific you'd like to have in the autogenerated pyi files? https://github.com/python/typeshed/blob/master/third_party/2and3/google/protobuf/json_format.pyi

@dilipvamsi
Copy link
Author

dilipvamsi commented Jun 16, 2020

I presenting an example for both proto2 and proto3 implementations.
As converting to json has 2 versions one with preserving the names and other is converting them to the json names.

For proto 2

message Hello {
  required string name = 1;
  required google.protobuf.Timestamp timestamp = 2;
}
message Sample {
  optional string string = 1;
  optional int64 large_int = 2;
  optional Hello hello = 3;
  required int32 small_int = 4;
  repeated int32 numbers = 5; 
}

I want a auto generated typed classes be

class Hello___JSON_PRESERVING(typing.TypedDict):
    name: str
    timestamp: str

class Sample___JSON_PRESERVING(typing.TypedDict):
     string: typing.Optional[str]
     large_int: typing.Optional[str]
     hello: typing.Optional[Hello___JSON_PRESERVING]
     small_int: int
     numbers: typing.Optional[typing.List[int]]

class Hello___JSON(typing.TypedDict):
    name: str
    timestamp: str

class Sample___JSON(typing.TypedDict):
     string: typing.Optional[str]
     largeInt: typing.Optional[str]
     hello: typing.Optional[Hello___JSON]
     smallInt: int
     numbers: typing.Optional[typing.List[int]]

For proto 3

message Hello {
  string name = 1;
  google.protobuf.Timestamp timestamp = 2;
}
message Sample {
  string string = 1;
  int64 large_int = 2;
  Hello hello = 3;
  int32 small_int = 4;
  int32 numbers = 5; 
}

I want a auto generated typed classes be

class Hello___JSON_PRESERVING(typing.TypedDict):
    name: typing.Optional[str]
    timestamp: typing.Optional[str]

class Sample___JSON_PRESERVING(typing.TypedDict):
     string: typing.Optional[str]
     large_int: typing.Optional[str]
     hello: typing.Optional[Hello___JSON_PRESERVING]
     small_int: typing.Optional[int]
     numbers: typing.Optional[typing.List[int]]

class Hello___JSON(typing.TypedDict):
    name: typing.Optional[str]
    timestamp: typing.Optional[str]

class Sample___JSON(typing.TypedDict):
     string: typing.Optional[str]
     largeInt: typing.Optional[str]
     hello: typing.Optional[Hello___JSON]
     smallInt: typing.Optional[int]
     numbers: typing.Optional[typing.List[int]]

@nipunn1313
Copy link
Owner

Yep - this seems reasonable. I would be amenable to autogenerating these.

They could be generated under the namespace of the original message - making importing easier - as Hello would be generated in both the _pb2.py and the _pb2.pyi

For example

class Hello(typing.TypedDict):
    class __JSONFormatDict(typing.TypedDict):
        [fields]

@nipunn1313 nipunn1313 changed the title json_format of protobuf Provide TypedDict types for json mapping of protobuf Jul 23, 2021
@choznerol choznerol moved this to Surveying 👀 in open source Dec 20, 2022
@choznerol choznerol moved this from Surveying 👀 to Archive 🗃 in open source Oct 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants