Skip to content

Commit f16a44d

Browse files
Max Jakobido-namely
andauthored
Add mypy support (#288)
* Add mypy support * Add explicit version for mypy * newer mypy version default * align names with Ruby's .rbi * add test * declare Dockerfile variable in outer scope * --mypy_grpc_out * add to mypy to renovate config * remove pip from final container * Update .github/renovate.json5 Co-authored-by: Ido David <36866853+ido-namely@users.noreply.github.com> * add file assertion * add test for consistency of language and --with-pyi * remove extra whitespace Co-authored-by: Ido David <36866853+ido-namely@users.noreply.github.com>
1 parent 8e47926 commit f16a44d

6 files changed

Lines changed: 60 additions & 1 deletion

File tree

.github/renovate.json5

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,16 @@
180180
depNameTemplate: "debian",
181181
datasourceTemplate: "docker",
182182
versioningTemplate: "debian",
183+
},
184+
{
185+
fileMatch: [
186+
"variables.sh$"
187+
],
188+
matchStrings: [
189+
"MYPY_VERSION=\\$\\{MYPY_VERSION:-(?<currentValue>.*?)\\}\\n"
190+
],
191+
depNameTemplate: "mypy-protobuf",
192+
datasourceTemplate: "pypi",
183193
}
184194
],
185195
packageRules: [

Dockerfile

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ ARG go_envoyproxy_pgv_version
1414
ARG go_mwitkow_gpv_version
1515
ARG go_protoc_gen_go_version
1616
ARG go_protoc_gen_go_grpc_version
17+
ARG mypy_version
1718

1819
FROM golang:$go_version-$debian_version AS build
1920

@@ -28,6 +29,7 @@ ARG go_mwitkow_gpv_version
2829
ARG uber_prototool_version
2930
ARG go_protoc_gen_go_version
3031
ARG go_protoc_gen_go_grpc_version
32+
ARG mypy_version
3133

3234

3335
RUN set -ex && apt-get update && apt-get install -y --no-install-recommends \
@@ -42,7 +44,8 @@ RUN set -ex && apt-get update && apt-get install -y --no-install-recommends \
4244
autoconf \
4345
zlib1g-dev \
4446
libssl-dev \
45-
clang
47+
clang \
48+
python3-pip
4649

4750
WORKDIR /tmp
4851
RUN git clone --depth 1 --shallow-submodules -b v$grpc_version.x --recursive https://github.com/grpc/grpc && \
@@ -110,6 +113,9 @@ RUN curl -fsSL "https://github.com/grpc/grpc-web/releases/download/${grpc_web_ve
110113
-o /tmp/grpc_web_plugin && \
111114
chmod +x /tmp/grpc_web_plugin
112115

116+
# Add mypy support
117+
RUN pip3 install -t /opt/mypy-protobuf mypy-protobuf==${mypy_version}
118+
113119
FROM debian:$debian_version-slim AS protoc-all
114120

115121
ARG grpc_version
@@ -173,6 +179,11 @@ COPY --from=build /go/pkg/mod/github.com/envoyproxy/protoc-gen-validate@v${go_en
173179

174180
COPY --from=build /go/pkg/mod/github.com/mwitkow/go-proto-validators@v${go_mwitkow_gpv_version}/ /opt/include/github.com/mwitkow/go-proto-validators/
175181

182+
# Copy mypy
183+
COPY --from=build /opt/mypy-protobuf/ /opt/mypy-protobuf/
184+
RUN mv /opt/mypy-protobuf/bin/* /usr/local/bin/
185+
ENV PYTHONPATH="${PYTHONPATH}:/opt/mypy-protobuf/"
186+
176187
ADD all/entrypoint.sh /usr/local/bin
177188
RUN chmod +x /usr/local/bin/entrypoint.sh
178189

all/entrypoint.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ printUsage() {
1616
echo " --lint CHECKS Enable linting protoc-lint (CHECKS are optional - see https://github.com/ckaznocha/protoc-gen-lint#optional-checks)"
1717
echo " --with-gateway Generate grpc-gateway files (experimental)."
1818
echo " --with-docs FORMAT Generate documentation (FORMAT is optional - see https://github.com/pseudomuto/protoc-gen-doc#invoking-the-plugin)"
19+
echo " --with-pyi Generate mypy stub files (.pyi files) - see https://github.com/nipunn1313/mypy-protobuf"
1920
echo " --with-rbi Generate Sorbet type declaration files (.rbi files) - see https://github.com/coinbase/protoc-gen-rbi"
2021
echo " --with-typescript Generate TypeScript declaration files (.d.ts files) - see https://github.com/improbable-eng/ts-protoc-gen#readme"
2122
echo " --with-validator Generate validations for (${VALIDATOR_SUPPORTED_LANGUAGES[@]}) - see https://github.com/envoyproxy/protoc-gen-validate"
@@ -50,6 +51,7 @@ GEN_VALIDATOR=false
5051
VALIDATOR_SUPPORTED_LANGUAGES=("go" "gogo" "cpp" "java" "python")
5152
DOCS_FORMAT="html,index.html"
5253
GEN_RBI=false
54+
GEN_PYI=false
5355
GEN_TYPESCRIPT=false
5456
LINT=false
5557
LINT_CHECKS=""
@@ -134,6 +136,10 @@ while test $# -gt 0; do
134136
GEN_RBI=true
135137
shift
136138
;;
139+
--with-pyi)
140+
GEN_PYI=true
141+
shift
142+
;;
137143
--with-typescript)
138144
GEN_TYPESCRIPT=true
139145
shift
@@ -296,6 +302,11 @@ if [[ "$GEN_RBI" == true && "$GEN_LANG" != "ruby" ]]; then
296302
exit 1
297303
fi
298304

305+
if [[ "$GEN_PYI" == true && "$GEN_LANG" != "python" ]]; then
306+
echo "Generating .pyi mypy stub files is a Python specific option."
307+
exit 1
308+
fi
309+
299310
if [[ "$GEN_TYPESCRIPT" == true && "$GEN_LANG" != "node" ]]; then
300311
echo "Generating TypeScript declaration files is Node specific."
301312
exit 1
@@ -432,6 +443,10 @@ if [[ $GEN_RBI == true ]]; then
432443
GEN_STRING="$GEN_STRING --rbi_out=$OUT_DIR"
433444
fi
434445

446+
if [[ $GEN_PYI == true ]]; then
447+
GEN_STRING="$GEN_STRING --mypy_out=$OUT_DIR --mypy_grpc_out=$OUT_DIR"
448+
fi
449+
435450
if [[ $GEN_TYPESCRIPT == true ]]; then
436451
GEN_STRING="$GEN_STRING --plugin=protoc-gen-ts=$(which protoc-gen-ts) --ts_out=$GRPC_OUT:$OUT_DIR"
437452
fi

all/test/all_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,27 @@ func (s *TestSuite) TestAllCases() {
140140
{fileName: "/all/test/test_pb2_grpc.py"},
141141
},
142142
},
143+
"python pyi": {
144+
lang: "python",
145+
protofileName: "all/test/test.proto",
146+
expectedOutputDir: "gen/pb_python",
147+
fileExpectations: []FileExpectation{
148+
{fileName: "/__init__.py"},
149+
{fileName: "/all/__init__.py"},
150+
{fileName: "/all/test/__init__.py"},
151+
{fileName: "/all/test/test_pb2.py"},
152+
{fileName: "/all/test/test_pb2.pyi"},
153+
{fileName: "/all/test/test_pb2_grpc.py"},
154+
{fileName: "/all/test/test_pb2_grpc.pyi"},
155+
},
156+
extraArgs: []string{"--with-pyi"},
157+
},
158+
"python pyi with inconsistent language parameter": {
159+
lang: "ruby",
160+
protofileName: "all/test/test.proto",
161+
expectedExitCode: 1,
162+
extraArgs: []string{"--with-pyi"},
163+
},
143164
"python with alternative output dir": {
144165
lang: "python",
145166
protofileName: "all/test/test.proto",

build.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ for build in ${BUILDS[@]}; do
1616
--build-arg go_version="${GO_VERSION}" \
1717
--build-arg uber_prototool_version="${UBER_PROTOTOOL_VERSION}" \
1818
--build-arg scala_pb_version="${SCALA_PB_VERSION}" \
19+
--build-arg mypy_version="${MYPY_VERSION}" \
1920
--build-arg node_version="${NODE_VERSION}" \
2021
--build-arg node_grpc_tools_node_protoc_ts_version="${NODE_GRPC_TOOLS_NODE_PROTOC_TS_VERSION}" \
2122
--build-arg node_grpc_tools_version="${NODE_GRPC_TOOLS_VERSION}" \

variables.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ GRPC_WEB_VERSION=${GRPC_WEB_VERSION:-1.3.1}
1414
GRPC_GATEWAY_VERSION=${GRPC_GATEWAY_VERSION:-v2.0.1}
1515
UBER_PROTOTOOL_VERSION=${UBER_PROTOTOOL_VERSION:-1.10.0}
1616
SCALA_PB_VERSION=${SCALA_PB_VERSION:-0.11.0}
17+
MYPY_VERSION=${MYPY_VERSION:-3.3.0}
1718
NODE_VERSION=${NODE_VERSION:-14}
1819
NODE_GRPC_TOOLS_NODE_PROTOC_TS_VERSION=${NODE_GRPC_TOOLS_NODE_PROTOC_TS_VERSION:-5.3.2}
1920
NODE_GRPC_TOOLS_VERSION=${NODE_GRPC_TOOLS_VERSION:-1.11.2}

0 commit comments

Comments
 (0)