Skip to content

Commit

Permalink
docs: add example for __protobuf__ module level attribute (#409)
Browse files Browse the repository at this point in the history
  • Loading branch information
parthea authored Nov 21, 2023
1 parent 48cd63f commit 6755884
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions docs/messages.rst
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,33 @@ A few things to note:
falsy value or not set all is to mark it ``optional``.
* Because all fields are optional, it is the responsibility of application logic
to determine whether a necessary field has been set.
* You can optionally define a `__protobuf__` attribute in your module which will be used
to differentiate messages which have the same name but exist in different modules.

.. code-block:: python
# file a.py
import proto
__protobuf__ = proto.module(package="a")
class A(proto.Message):
name = proto.Field(proto.STRING, number=1)
# file b.py
import proto
__protobuf__ = proto.module(package="b")
class A(proto.Message):
name = proto.Field(proto.STRING, number=1)
# file main.py
import a
import b
_a = a.A(name="Hello, A!")
_b = b.A(name="Hello, B!")
.. _messages: https://developers.google.com/protocol-buffers/docs/proto3#simple

Expand Down

0 comments on commit 6755884

Please sign in to comment.