pw_protobuf: Add some type annotations
These would have helped me orient in the code when I was working on it.
Change-Id: I4deacfad46a6ba210ebdef4697fd621d1ff831f9
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/110636
Reviewed-by: Wyatt Hepler <hepler@google.com>
Pigweed-Auto-Submit: Ted Pudlik <tpudlik@google.com>
Commit-Queue: Auto-Submit <auto-submit@pigweed.google.com.iam.gserviceaccount.com>
diff --git a/pw_protobuf/py/pw_protobuf/options.py b/pw_protobuf/py/pw_protobuf/options.py
index d9ffca6..9fc301b 100644
--- a/pw_protobuf/py/pw_protobuf/options.py
+++ b/pw_protobuf/py/pw_protobuf/options.py
@@ -26,9 +26,11 @@
_SINGLE_LINE_COMMENT_RE = re.compile(r'//.*?$', flags=re.MULTILINE)
_SHELL_STYLE_COMMENT_RE = re.compile(r'#.*?$', flags=re.MULTILINE)
+# A list of (proto field path, Options) tuples.
+FieldOptions = List[Tuple[str, Options]]
-def load_options_from(options: List[Tuple[str, Options]],
- options_file_name: Path):
+
+def load_options_from(options: FieldOptions, options_file_name: Path):
"""Loads a single .options file for the given .proto"""
with open(options_file_name) as options_file:
# Read the options file and strip all styles of comments before parsing.
@@ -52,9 +54,9 @@
def load_options(include_paths: List[Path],
- proto_file_name: Path) -> List[Tuple[str, Options]]:
+ proto_file_name: Path) -> FieldOptions:
"""Loads the .options for the given .proto."""
- options: List[Tuple[str, Options]] = []
+ options: FieldOptions = []
for include_path in include_paths:
options_file_name = include_path / proto_file_name.with_suffix(
@@ -65,7 +67,7 @@
return options
-def match_options(name: str, options: List[Tuple[str, Options]]) -> Options:
+def match_options(name: str, options: FieldOptions) -> Options:
"""Return the matching options for a name."""
matched = Options()
for name_glob, mask_options in options:
diff --git a/pw_protobuf/py/pw_protobuf/proto_tree.py b/pw_protobuf/py/pw_protobuf/proto_tree.py
index db0ca42..687b16e 100644
--- a/pw_protobuf/py/pw_protobuf/proto_tree.py
+++ b/pw_protobuf/py/pw_protobuf/proto_tree.py
@@ -530,8 +530,9 @@
response_node))
-def _populate_fields(proto_file, global_root: ProtoNode,
- package_root: ProtoNode, proto_options) -> None:
+def _populate_fields(proto_file: descriptor_pb2.FileDescriptorProto,
+ global_root: ProtoNode, package_root: ProtoNode,
+ proto_options: Optional[options.FieldOptions]) -> None:
"""Traverses a proto file, adding all message and enum fields to a tree."""
def populate_message(node, message):
"""Recursively populates nested messages and enums."""
@@ -558,7 +559,9 @@
_add_service_methods(global_root, package_root, service_node, service)
-def _build_hierarchy(proto_file):
+def _build_hierarchy(
+ proto_file: descriptor_pb2.FileDescriptorProto
+) -> Tuple[ProtoPackage, ProtoPackage]:
"""Creates a ProtoNode hierarchy from a proto file descriptor."""
root = ProtoPackage('')
@@ -590,8 +593,10 @@
return root, package_root
-def build_node_tree(file_descriptor_proto,
- proto_options=None) -> Tuple[ProtoNode, ProtoNode]:
+def build_node_tree(
+ file_descriptor_proto: descriptor_pb2.FileDescriptorProto,
+ proto_options: Optional[options.FieldOptions] = None
+) -> Tuple[ProtoNode, ProtoNode]:
"""Constructs a tree of proto nodes from a file descriptor.
Returns the root node of the entire proto package tree and the node