Skip to content

google.protobuf for python 3 - #2140

Closed
smessmer wants to merge 2 commits into
python:masterfrom
smessmer:protobuf
Closed

smessmer wants to merge 2 commits into
python:masterfrom
smessmer:protobuf

Conversation

@smessmer

Copy link
Copy Markdown
Contributor

This allows usage of the google.protobuf type stubs from python 3 code.

@JelleZijlstra

Copy link
Copy Markdown
Member

Thanks for the PR! What have you done to test that these stubs work right in Python 3?

@smessmer

Copy link
Copy Markdown
Contributor Author

We're using these in one of our projects, where they work fine. However, coverage is very likely incomplete.

@JelleZijlstra

Copy link
Copy Markdown
Member

Thanks. I noticed there are a ton of functions like protobuf/wrappers_pb2.pyi: def FromString(cls, s: str) -> FloatValue: .... Do those really take str, not bytes in Python 3? I don't have an easy way to check.

@smessmer

Copy link
Copy Markdown
Contributor Author

They probably take bytes, but I don't have an easy way to check either. I did change a few call sites from str to bytes where we used them and I know they're actually meant to be bytes.

@JelleZijlstra

Copy link
Copy Markdown
Member

Is it reasonable to assume that all FromString methods take the same type?

@smessmer

smessmer commented May 22, 2018 •

Copy link
Copy Markdown
Contributor Author

I'm unsure. Somebody implemented MergeFromString and ParseFromString in Message to take type Any.

@gvanrossum

gvanrossum commented May 23, 2018 via email

Copy link
Copy Markdown
Member

@JelleZijlstra

Copy link
Copy Markdown
Member

This has some conflicts now.

@nipunn1313

Copy link
Copy Markdown
Contributor

All FromString methods do in fact take bytes. They take in a serialized input.
FromString is a bit of a misnomer in python3, but the naming was designed for python2.

I believe that google.protobuf uses the same code for both python2 and python3, so as long as we use bytes and Text, we shouldn't have (in theory) any typing issues.

That being said, unexpected things do occur. I'm not sure there's a worthwhile way for us to derisk all of these up front.

In #2174 - I made a similar diff which replaced all instances of str -> bytes.

@JelleZijlstra

Copy link
Copy Markdown
Member

I merged #2174 instead.

@smessmer
smessmer deleted the protobuf branch June 1, 2018 21:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants