Skip to content

Commit 5f6b24a

Browse files
authored
docs: Clarify semantics of multiple oneof variants passed to msg ctor (#263)
Example: import proto class Song(proto.Message): name = proto.Field(proto.STRING, number=1, oneof="identifier") database_id = proto.Field(proto.STRING, number=2, oneof="identifier") s = Song(name="Canon in D minor", database_id="b5a37aad3") assert "database_id" in s and "name" not in s s = Song(database_id="e6aa708c7e", name="Little Fugue")
1 parent e66207e commit 5f6b24a

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

packages/proto-plus/docs/fields.rst

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,33 @@ a string (which should match for all fields within the oneof):
151151
have consecutive field numbers, but they must be declared in consecutive
152152
order.
153153

154+
.. warning::
155+
156+
If a message is constructed with multiple variants of a single ``oneof`` passed
157+
to its constructor, the **last** keyword/value pair passed will be the final
158+
value set.
159+
160+
This is consistent with PEP-468_, which specifies the order that keyword args
161+
are seen by called functions, and with the regular protocol buffers runtime,
162+
which exhibits the same behavior.
163+
164+
Example:
165+
166+
.. code-block:: python
167+
168+
import proto
169+
170+
class Song(proto.Message):
171+
name = proto.Field(proto.STRING, number=1, oneof="identifier")
172+
database_id = proto.Field(proto.STRING, number=2, oneof="identifier")
173+
174+
s = Song(name="Canon in D minor", database_id="b5a37aad3")
175+
assert "database_id" in s and "name" not in s
176+
177+
s = Song(database_id="e6aa708c7e", name="Little Fugue")
178+
assert "name" in s and "database_id" not in s
179+
180+
154181
155182
Optional fields
156183
---------------
@@ -199,5 +226,4 @@ information.
199226

200227
.. _documentation: https://github.com/protocolbuffers/protobuf/blob/v3.12.0/docs/field_presence.md
201228

202-
203-
229+
.. _PEP-468: https://www.python.org/dev/peps/pep-0468/

0 commit comments

Comments
 (0)