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

The Message.FromString() Python method is missing from online documentation #19433

Open
dlenskiSB opened this issue Nov 27, 2024 · 0 comments
Open
Assignees
Labels

Comments

@dlenskiSB
Copy link

dlenskiSB commented Nov 27, 2024

Version: main

Operating System: Linux, but irrelevant

Runtime: Python 3.12

The undocumented method in question

With the python-protobuf package installed locally, pydoc shows the existence of a class/static method FromString() which creates a new instance from serialized data ("method" in the docstring doesn't make much sense, but 🤷🏻‍♂️):

$ pydoc3 google._upb._message.Message|grep -C3 FromString
 |  Class methods defined here:
 |
 |  FromString(...)
 |      Creates new method instance from given serialized data.

The C source of this method is found at:

protobuf/python/message.c

Lines 1525 to 1542 in 9668016

static PyObject* PyUpb_Message_FromString(PyObject* cls, PyObject* serialized) {
PyObject* ret = NULL;
PyObject* length = NULL;
ret = PyObject_CallObject(cls, NULL);
if (ret == NULL) goto err;
length = PyUpb_Message_MergeFromString(ret, serialized);
if (length == NULL) goto err;
done:
Py_XDECREF(length);
return ret;
err:
Py_XDECREF(ret);
ret = NULL;
goto done;
}

What does the documentation currently show?

The .ParseFromString and .MergeFromString methods are well-documented and widely seen in real-world Python protobuf code.

The .FromString static/class method is BASICALLY UNDOCUMENTED:

What should the documentation show?

I expect to see the .FromString method in online documentation.

It allows more concise and Pythonic deserialization of Protobuf messages, because it combines instantiating the Message sub-class with calling .ParseFromString() on it:

from foo_pb2 import FooMessage

serialized_bytes = b'...'   # serialized message

# Documented two-step approach
msg1 = FooMessage()
msg1.ParseFromString(serialized_bytes)

# UNDOCUMENTED one-step approach
msg2 = FooMessage.FromString(serialized_bytes)

assert msg1 == msg2
@dlenskiSB dlenskiSB added the untriaged auto added to all issues by default when created. label Nov 27, 2024
@shaod2 shaod2 added python and removed untriaged auto added to all issues by default when created. labels Dec 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants