Skip to content

fix: allow creating PyRuntimeInfo without specifying interpreter_version_info #1992

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ A brief description of the categories of changes:
* (rules) Auto exec groups are enabled. This allows actions run by the rules,
such as precompiling, to pick an execution platform separately from what
other toolchains support.
* (providers) {obj}`PyRuntimeInfo` doesn't require passing the
`interpreter_version_info` arg.

### Removed
* Nothing yet
Expand Down
4 changes: 2 additions & 2 deletions python/private/common/providers.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ def interpreter_version_info_struct_from_dict(info_dict):
"""Create a struct of interpreter version info from a dict from an attribute.

Args:
info_dict: dict of versio info fields. See interpreter_version_info
info_dict: (dict | None) of version info fields. See interpreter_version_info
provider field docs.

Returns:
struct of version info; see interpreter_version_info provider field docs.
"""
info_dict = dict(info_dict) # Copy in case the original is frozen
info_dict = dict(info_dict or {}) # Copy in case the original is frozen
if info_dict:
if not ("major" in info_dict and "minor" in info_dict):
fail("interpreter_version_info must have at least two keys, 'major' and 'minor'")
Expand Down
5 changes: 5 additions & 0 deletions tests/py_runtime_info/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
load(":py_runtime_info_tests.bzl", "py_runtime_info_test_suite")

py_runtime_info_test_suite(
name = "py_runtime_info_tests",
)
65 changes: 65 additions & 0 deletions tests/py_runtime_info/py_runtime_info_tests.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Copyright 2023 The Bazel Authors. All rights reserved.
#
# 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.
"""Starlark tests for PyRuntimeInfo provider."""

load("@rules_testing//lib:analysis_test.bzl", "analysis_test")
load("@rules_testing//lib:test_suite.bzl", "test_suite")
load("//python:py_runtime_info.bzl", "PyRuntimeInfo")
load("//python/private:util.bzl", "IS_BAZEL_7_OR_HIGHER") # buildifier: disable=bzl-visibility

def _create_py_runtime_info_without_interpreter_version_info_impl(ctx):
kwargs = {}
if IS_BAZEL_7_OR_HIGHER:
kwargs["bootstrap_template"] = ctx.attr.bootstrap_template

return [PyRuntimeInfo(
interpreter = ctx.file.interpreter,
files = depset(ctx.files.files),
python_version = "PY3",
**kwargs
)]

_create_py_runtime_info_without_interpreter_version_info = rule(
implementation = _create_py_runtime_info_without_interpreter_version_info_impl,
attrs = {
"bootstrap_template": attr.label(allow_single_file = True, default = "bootstrap.txt"),
"files": attr.label_list(allow_files = True, default = ["data.txt"]),
"interpreter": attr.label(allow_single_file = True, default = "interpreter.sh"),
"python_version": attr.string(default = "PY3"),
},
)

_tests = []

def _test_can_create_py_runtime_info_without_interpreter_version_info(name):
_create_py_runtime_info_without_interpreter_version_info(
name = name + "_subject",
)
analysis_test(
name = name,
target = name + "_subject",
impl = _test_can_create_py_runtime_info_without_interpreter_version_info_impl,
)

def _test_can_create_py_runtime_info_without_interpreter_version_info_impl(env, target):
# If we get this for, construction succeeded, so nothing to check
_ = env, target # @unused

_tests.append(_test_can_create_py_runtime_info_without_interpreter_version_info)

def py_runtime_info_test_suite(name):
test_suite(
name = name,
tests = _tests,
)