Skip to content

Commit 941f4f6

Browse files
Protocol Buffer TeamLogofile
authored andcommitted
This documentation change includes the following:
* content/best-practices/dos-donts.md: Adds a note to clarify that "Common Types" (such as Date and Money) are not included with the standard compiler and require a dependency on the GoogleAPIs repository. * content/editions/features.md: Significantly expands the documentation for Protobuf editions features. * content/programming-guides/proto3.md: Adds content to the Proto3 programming guide about specifying paths for import files * content/reference/python/python-comparison.md: Adds a new documentation page that compares different Python implementations for Protocol Buffers. * content/programming-guides/proto-limits.md: Minor content updates. * content/includes/version-tables.css: Minor style adjustments. PiperOrigin-RevId: 799563507 Change-Id: I0624d9dc24207f1366e91b5b82b162da141bf52b
1 parent 6320b7e commit 941f4f6

File tree

4 files changed

+125
-7
lines changed

4 files changed

+125
-7
lines changed

content/best-practices/dos-donts.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,11 @@ when a perfectly suitable common type already exists!
168168
* [`color`](https://github.com/googleapis/googleapis/blob/master/google/type/color.proto)
169169
is a color in the RGBA color space.
170170

171+
**Note:** While the "Well-Known Types" (such as `Duration` and `Timestamp`) are
172+
included with the Protocol Buffers compiler, the "Common Types" (such as `Date`
173+
and `Money`) are not. To use the Common Types, you may need to add a dependency
174+
on the [googleapis repository](https://github.com/googleapis/googleapis).
175+
171176
<a id="do-define-widely-used-message-types-in-separate-files"></a>
172177

173178
## **Do** Define Message Types in Separate Files {#separate-files}

content/programming-guides/proto-limits.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ others.
1515

1616
## Number of Fields {#fields}
1717

18+
All messages are limited to 65,535 fields.
19+
1820
Message with only singular proto fields (such as Boolean):
1921

2022
* ~2100 fields (proto2)

content/programming-guides/proto3.md

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,34 @@ file:
879879
import "myproject/other_protos.proto";
880880
```
881881

882+
The protobuf compiler searches for imported files in a set of directories
883+
specified using the `-I`/`--proto_path` flag. The path given in an `import`
884+
statement is resolved relative to these directories. For more information on
885+
using the compiler, see [Generating Your Classes](#generating).
886+
887+
For example, consider the following directory structure:
888+
889+
```
890+
my_project/
891+
├── protos/
892+
│ ├── main.proto
893+
│ └── common/
894+
│ └── timestamp.proto
895+
```
896+
897+
To use definitions from `timestamp.proto` within `main.proto`, you would run the
898+
compiler from the `my_project` directory and set `--proto_path=protos`. The
899+
`import` statement in `main.proto` would then be:
900+
901+
```proto
902+
// Located in my_project/protos/main.proto
903+
import "common/timestamp.proto";
904+
```
905+
906+
In general you should set the `--proto_path` flag to the highest-level directory
907+
that contains protos. This is often the root of the project, but in this example
908+
it's in a separate `/protos` directory.
909+
882910
By default, you can use definitions only from directly imported `.proto` files.
883911
However, sometimes you may need to move a `.proto` file to a new location.
884912
Instead of moving the `.proto` file directly and updating all the call sites in
@@ -915,12 +943,6 @@ import "old.proto";
915943
// You use definitions from old.proto and new.proto, but not other.proto
916944
```
917945

918-
The protocol compiler searches for imported files in a set of directories
919-
specified on the protocol compiler command line using the `-I`/`--proto_path`
920-
flag. If no flag was given, it looks in the directory in which the compiler was
921-
invoked. In general you should set the `--proto_path` flag to the root of your
922-
project and use fully qualified names for all imports.
923-
924946
### Using proto2 Message Types {#proto2}
925947

926948
It's possible to import
@@ -1739,7 +1761,7 @@ generator plugin for the compiler; you can find this and installation
17391761
instructions in the [golang/protobuf](https://github.com/golang/protobuf/)
17401762
repository on GitHub.
17411763

1742-
The Protocol Compiler is invoked as follows:
1764+
The protobuf compiler is invoked as follows:
17431765

17441766
```sh
17451767
protoc --proto_path=IMPORT_PATH --cpp_out=DST_DIR --java_out=DST_DIR --python_out=DST_DIR --go_out=DST_DIR --ruby_out=DST_DIR --objc_out=DST_DIR --csharp_out=DST_DIR path/to/file.proto
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
+++
2+
title = "Python Comparison"
3+
weight = 751
4+
linkTitle = "Python Comparison"
5+
description = "Describes how Python compares objects."
6+
type = "docs"
7+
+++
8+
9+
Because of how proto data is serialized, you cannot rely on the wire
10+
representation of a proto message instance to determine if its content is the
11+
same as another instance. A subset of the ways that a wire-format proto message
12+
instance can vary include the following:
13+
14+
* The protobuf schema changes in certain ways.
15+
* A map field stores its values in a different order.
16+
* The binary is built with different flags (such as opt vs. debug).
17+
* The protobuf library is updated.
18+
19+
Because of these ways that serialized data can vary, determining equality
20+
involves other methods.
21+
22+
## Comparison Methods {#methods}
23+
24+
You can compare protocol buffer messages for equality using the standard Python
25+
`==` operator. Comparing two objects using the `==` operator compares with
26+
`message.ListFields()`. When testing, you can use `self.assertEqual(msg1,
27+
msg2)`.
28+
29+
Two messages are considered equal if they have the same type and all of their
30+
corresponding fields are equal. The inequality operator `!=` is the exact
31+
inverse of `==`.
32+
33+
Message equality is recursive: for two messages to be equal, any nested messages
34+
must also be equal.
35+
36+
## Field Equality and Presence
37+
38+
The equality check for fields is value-based. For fields with
39+
[explicit presence](#singular-explicit), the presence of a field is also taken
40+
into account.
41+
42+
A field that is explicitly set to its default value is **not** considered equal
43+
to a field that is unset.
44+
45+
For example, consider the following message which has an explicit presence
46+
field:
47+
48+
```proto
49+
edition = "2023";
50+
message MyMessage {
51+
int32 value = 1; // 'explicit' presence by default in Editions
52+
}
53+
```
54+
55+
If you create two instances, one where `value` is unset and one where `value` is
56+
explicitly set to `0`, they will not be equal:
57+
58+
```python
59+
msg1 = MyMessage()
60+
msg2 = MyMessage()
61+
msg2.value = 0
62+
63+
assert not msg1.HasField("value")
64+
assert msg2.HasField("value")
65+
assert msg1 != msg2
66+
```
67+
68+
This same principle applies to sub-message fields: an unset sub-message is not
69+
equal to a sub-message that is present but empty (a default instance of the
70+
sub-message class).
71+
72+
For fields with [implicit presence](#singular-implicit), since presence cannot
73+
be tracked, the field is always compared by its value against the corresponding
74+
field in the other message.
75+
76+
This behavior, where presence is part of the equality check, is different from
77+
how some other languages or protobuf libraries might handle equality, where
78+
unset fields and fields set to their default value are sometimes treated as
79+
equivalent (often for wire-format compatibility). In Python, `==` performs a
80+
stricter check.
81+
82+
## Other Field Types {#other-types}
83+
84+
* **Repeated fields** are equal if they have the same number of elements and
85+
each corresponding element is equal. The order of elements matters.
86+
* **Map fields** are equal if they have the same set of key-value pairs. The
87+
order of pairs does not matter.
88+
* **Floating-point fields** (`float` and `double`) are compared by value. Be
89+
aware of the usual caveats with floating-point comparisons.

0 commit comments

Comments
 (0)