mirror of https://github.com/grpc/grpc.git
[bazel] Auto-generate .pyi files in py_proto_library rule (#32872)
With some delay, this is a PR for https://github.com/grpc/grpc/issues/32564 (and previously https://github.com/grpc/grpc/pull/31791). I looked into adding a regular `py_test` for this change [as suggested](https://github.com/grpc/grpc/pull/31791#issuecomment-1423245116) but I am not aware of any effect that the presence of a .pyi stub file would have at runtime and where some sort of type-checking in a .py script would be affected. Stub files are only for use by type checkers & IDE's. I mean, something like this would work: ``` import helloworld_pb2 py_file = helloworld_pb2.__file__ pyi_file = py_file + 'i’ self.assertTrue(os.path.exists(pyi_file)) ``` But that seems really hacky to me. Instead I created a simple rule test for `py_proto_library` with Bazel Skylib which tests the declared outputs for an example `py_proto_library` target. Indirectly, this also tests that the declared output files are actually generated. Please let me know if this is sufficient.pull/33413/head
parent
017786f151
commit
fa32eb36b3
6 changed files with 87 additions and 4 deletions
@ -1,4 +1,5 @@ |
||||
gens/ |
||||
*_pb2.py |
||||
*_pb2.pyi |
||||
*_pb2_grpc.py |
||||
*.egg-info/ |
||||
|
@ -0,0 +1,70 @@ |
||||
# Copyright 2021 The gRPC Authors |
||||
# |
||||
# Licensed under the Apache License, Version 2.0 (the "License"); |
||||
# you may not use this file except in compliance with the License. |
||||
# You may obtain a copy of the License at |
||||
# |
||||
# http://www.apache.org/licenses/LICENSE-2.0 |
||||
# |
||||
# Unless required by applicable law or agreed to in writing, software |
||||
# distributed under the License is distributed on an "AS IS" BASIS, |
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
# See the License for the specific language governing permissions and |
||||
# limitations under the License. |
||||
"""Bazel rule tests of bazel/python_rules.bzl""" |
||||
|
||||
load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts") |
||||
|
||||
def _assert_in(env, item, container): |
||||
asserts.true( |
||||
env, |
||||
item in container, |
||||
"Expected " + str(item) + " to be in " + str(container), |
||||
) |
||||
|
||||
# Tests the declared outputs of the 'py_proto_library' rule and, indirectly, also tests that |
||||
# these outputs are actually generated (building ":helloworld_py_pb2" will fail if not all of |
||||
# the declared output files are actually generated). |
||||
def _py_proto_library_provider_contents_test_impl(ctx): |
||||
env = analysistest.begin(ctx) |
||||
|
||||
target = analysistest.target_under_test(env) |
||||
|
||||
files = [file.short_path for file in target.files.to_list()] |
||||
runfiles = [file.short_path for file in target.default_runfiles.files.to_list()] |
||||
py_info_transitive_sources = [ |
||||
file.short_path |
||||
for file in target[PyInfo].transitive_sources.to_list() |
||||
] |
||||
|
||||
_assert_in(env, "helloworld_pb2.py", files) |
||||
_assert_in(env, "helloworld_pb2.pyi", files) |
||||
_assert_in(env, "subdir/hello_dep_pb2.py", files) |
||||
_assert_in(env, "subdir/hello_dep_pb2.pyi", files) |
||||
|
||||
_assert_in(env, "helloworld_pb2.py", runfiles) |
||||
_assert_in(env, "helloworld_pb2.pyi", runfiles) |
||||
_assert_in(env, "subdir/hello_dep_pb2.py", runfiles) |
||||
_assert_in(env, "subdir/hello_dep_pb2.pyi", runfiles) |
||||
|
||||
_assert_in(env, "helloworld_pb2.py", py_info_transitive_sources) |
||||
_assert_in(env, "helloworld_pb2.pyi", py_info_transitive_sources) |
||||
_assert_in(env, "subdir/hello_dep_pb2.py", py_info_transitive_sources) |
||||
_assert_in(env, "subdir/hello_dep_pb2.pyi", py_info_transitive_sources) |
||||
|
||||
return analysistest.end(env) |
||||
|
||||
_py_proto_library_provider_contents_test = analysistest.make(_py_proto_library_provider_contents_test_impl) |
||||
|
||||
def python_rules_test_suite(name): |
||||
_py_proto_library_provider_contents_test( |
||||
name = "py_proto_library_provider_contents_test", |
||||
target_under_test = ":helloworld_py_pb2", |
||||
) |
||||
|
||||
native.test_suite( |
||||
name = name, |
||||
tests = [ |
||||
"py_proto_library_provider_contents_test", |
||||
], |
||||
) |
Loading…
Reference in new issue