From c00a7571f176303e1e1b294f501d23d0a414bc10 Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Tue, 21 Nov 2023 21:27:06 +0000 Subject: [PATCH] docs: add example for __protobuf__ module level attribute --- docs/messages.rst | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/docs/messages.rst b/docs/messages.rst index e033473c..68d7d63d 100644 --- a/docs/messages.rst +++ b/docs/messages.rst @@ -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