Skip to content

Commit bab7a86

Browse files
committed
Add grpc_stubs config option to control stub generation
Add a new 'grpc_stubs' configuration option that allows users to choose which gRPC stubs to generate: - sync_and_async: Generate both sync and async stubs (default) - sync_only: Generate only synchronous stubs - async_only: Generate only asynchronous stubs This uses the new only_sync/only_async flags in mypy-protobuf >= 3.8.0. Fixes: #485 Signed-off-by: Mathias L. Baumann <mathias.baumann@frequenz.com>
1 parent 433c44e commit bab7a86

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

pyproject.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ compile_proto = "frequenz.repo.config.setuptools.grpc_tools:CompileProto"
5656
actor = []
5757
api = [
5858
"grpcio-tools >= 1.47.0, < 2",
59-
"mypy-protobuf >= 3.0.0, < 4",
59+
# Requires mypy-protobuf >= 3.8.0 for only_async/only_sync support
60+
# See: https://github.com/nipunn1313/mypy-protobuf/pull/694
61+
"mypy-protobuf >= 3.8.0, < 4",
6062
"setuptools >= 67.6.0, < 81",
6163
]
6264
app = []

src/frequenz/repo/config/protobuf.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ class ProtobufConfig:
3636
docs_path: str = "protobuf-reference"
3737
"""The path of the root directory where the documentation files will be generated."""
3838

39+
grpc_stubs: str = "sync_and_async"
40+
"""The type of gRPC stubs to generate.
41+
42+
Possible values:
43+
- "sync_and_async": Generate both sync and async stubs (default).
44+
- "sync_only": Generate only synchronous stubs.
45+
- "async_only": Generate only asynchronous stubs.
46+
"""
47+
3948
@classmethod
4049
def from_pyproject_toml(
4150
cls, path: str = "pyproject.toml", /, **defaults: Any

src/frequenz/repo/config/setuptools/grpc_tools.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ class CompileProto(_setuptools.Command):
3737
py_path: str
3838
"""The path of the root directory where the Python files will be generated."""
3939

40+
grpc_stubs: str
41+
"""The type of gRPC stubs to generate (sync_and_async, sync_only, async_only)."""
42+
4043
description: str = "compile protobuf files"
4144
"""Description of the command."""
4245

@@ -63,6 +66,11 @@ class CompileProto(_setuptools.Command):
6366
None,
6467
"path of the root directory where the Python files will be generated",
6568
),
69+
(
70+
"grpc-stubs=",
71+
None,
72+
"type of gRPC stubs to generate (sync_and_async, sync_only, async_only)",
73+
),
6674
],
6775
)
6876
"""Options of the command."""
@@ -75,6 +83,7 @@ def initialize_options(self) -> None:
7583
self.proto_glob = config.proto_glob
7684
self.include_paths = config.include_paths
7785
self.py_path = config.py_path
86+
self.grpc_stubs = config.grpc_stubs
7887

7988
def finalize_options(self) -> None:
8089
"""Finalize options."""
@@ -103,12 +112,29 @@ def run(self) -> None:
103112
)
104113
return
105114

115+
# Build the mypy_grpc_out option based on grpc_stubs setting
116+
mypy_grpc_out_opt = f"--mypy_grpc_out={self.py_path}"
117+
match self.grpc_stubs:
118+
case "sync_only":
119+
mypy_grpc_out_opt = f"--mypy_grpc_out=only_sync:{self.py_path}"
120+
case "async_only":
121+
mypy_grpc_out_opt = f"--mypy_grpc_out=only_async:{self.py_path}"
122+
case "sync_and_async":
123+
pass # Default, no extra options needed
124+
case _:
125+
print(
126+
f"WARNING: Unknown grpc_stubs value '{self.grpc_stubs}', "
127+
"using default 'sync_and_async'"
128+
)
129+
106130
protoc_cmd = (
107131
[_sys.executable, "-m", "grpc_tools.protoc"]
108132
+ [f"-I{p}" for p in [*include_paths, self.proto_path]]
109133
+ [
110-
f"--{opt}={self.py_path}"
111-
for opt in "python_out grpc_python_out mypy_out mypy_grpc_out".split()
134+
f"--python_out={self.py_path}",
135+
f"--grpc_python_out={self.py_path}",
136+
f"--mypy_out={self.py_path}",
137+
mypy_grpc_out_opt,
112138
]
113139
+ proto_files
114140
)

0 commit comments

Comments
 (0)