Skip to content

Commit 4d19363

Browse files
committed
fix: allow creating PyRuntimeInfo without specifying interpreter_version_info
This regression was flagged by some of the Bazel tests. When the dict->struct interpreter_version code was factored out, it didn't properly handle the value `None`, which occurs if the provider is directly instantiated by way of the default value. To fix, treat None as an empty dict. The logic already assumes the dict is potentially empty or missing keys, so it can continue on OK. Also adds a test for this.
1 parent 95b150d commit 4d19363

File tree

4 files changed

+68
-2
lines changed

4 files changed

+68
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ A brief description of the categories of changes:
3434
* (rules) Auto exec groups are enabled. This allows actions run by the rules,
3535
such as precompiling, to pick an execution platform separately from what
3636
other toolchains support.
37+
* (providers) {obj}`PyRuntimeInfo` doesn't require passing the
38+
`interpreter_version_info` arg.
3739

3840
### Removed
3941
* Nothing yet

python/private/common/providers.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ def interpreter_version_info_struct_from_dict(info_dict):
4141
"""Create a struct of interpreter version info from a dict from an attribute.
4242
4343
Args:
44-
info_dict: dict of versio info fields. See interpreter_version_info
44+
info_dict: (dict | None) of version info fields. See interpreter_version_info
4545
provider field docs.
4646
4747
Returns:
4848
struct of version info; see interpreter_version_info provider field docs.
4949
"""
50-
info_dict = dict(info_dict) # Copy in case the original is frozen
50+
info_dict = dict(info_dict or {}) # Copy in case the original is frozen
5151
if info_dict:
5252
if not ("major" in info_dict and "minor" in info_dict):
5353
fail("interpreter_version_info must have at least two keys, 'major' and 'minor'")

tests/py_runtime_info/BUILD.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
load(":py_runtime_info_tests.bzl", "py_runtime_info_test_suite")
2+
3+
py_runtime_info_test_suite(
4+
name = "py_runtime_info_tests",
5+
)
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Copyright 2023 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
"""Starlark tests for PyRuntimeInfo provider."""
15+
16+
load("@rules_testing//lib:analysis_test.bzl", "analysis_test")
17+
load("@rules_testing//lib:test_suite.bzl", "test_suite")
18+
load("//python:py_runtime_info.bzl", "PyRuntimeInfo")
19+
20+
def _create_py_runtime_info_without_interpreter_version_info_impl(ctx):
21+
return [PyRuntimeInfo(
22+
interpreter = ctx.file.interpreter,
23+
files = depset(ctx.files.files),
24+
python_version = "PY3",
25+
bootstrap_template = ctx.attr.bootstrap_template,
26+
)]
27+
28+
_create_py_runtime_info_without_interpreter_version_info = rule(
29+
implementation = _create_py_runtime_info_without_interpreter_version_info_impl,
30+
attrs = {
31+
"interpreter": attr.label(allow_single_file = True, default = "interpreter.sh"),
32+
"files": attr.label_list(allow_files = True, default = ["data.txt"]),
33+
"python_version": attr.string(default = "PY3"),
34+
"bootstrap_template": attr.label(allow_single_file = True, default = "bootstrap.txt"),
35+
},
36+
)
37+
38+
_tests = []
39+
40+
def _test_can_create_py_runtime_info_without_interpreter_version_info(name):
41+
_create_py_runtime_info_without_interpreter_version_info(
42+
name = name + "_subject",
43+
)
44+
analysis_test(
45+
name = name,
46+
target = name + "_subject",
47+
impl = _test_can_create_py_runtime_info_without_interpreter_version_info_impl,
48+
)
49+
50+
def _test_can_create_py_runtime_info_without_interpreter_version_info_impl(env, target):
51+
pass
52+
53+
_tests.append(_test_can_create_py_runtime_info_without_interpreter_version_info)
54+
55+
def py_runtime_info_test_suite(name):
56+
test_suite(
57+
name = name,
58+
tests = _tests,
59+
)

0 commit comments

Comments
 (0)