Skip to content

Commit 484d38b

Browse files
committed
feat: default py_runtime version info to --python_version
1 parent 30bd94d commit 484d38b

File tree

3 files changed

+66
-11
lines changed

3 files changed

+66
-11
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ A brief description of the categories of changes:
2828
* (gazelle): Update error messages when unable to resolve a dependency to be more human-friendly.
2929
* (flags) The {obj}`--python_version` flag now also returns
3030
{obj}`config_common.FeatureFlagInfo`.
31+
* (toolchains) When {obj}`py_runtime.interpreter_version_info` isn't specified,
32+
the {obj}`--python_version` flag will determine the value. This allows
33+
specifying the build-time Python version for the
34+
{obj}`runtime_env_toolchains`.
3135

3236
### Fixed
3337
* (whl_library): Remove `--no-index` and add `--no-build-isolation` to the

python/private/common/py_runtime_rule.bzl

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
load("@bazel_skylib//lib:dicts.bzl", "dicts")
1717
load("@bazel_skylib//lib:paths.bzl", "paths")
18+
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
1819
load("//python/private:reexports.bzl", "BuiltinPyRuntimeInfo")
1920
load("//python/private:util.bzl", "IS_BAZEL_7_OR_HIGHER")
2021
load(":attributes.bzl", "NATIVE_RULES_ALLOWLIST_ATTRS")
@@ -80,6 +81,10 @@ def _py_runtime_impl(ctx):
8081
python_version = ctx.attr.python_version
8182

8283
interpreter_version_info = ctx.attr.interpreter_version_info
84+
if not interpreter_version_info:
85+
python_version_flag = ctx.attr._python_version_flag[BuildSettingInfo].value
86+
if python_version_flag:
87+
interpreter_version_info = _interpreter_version_info_from_version_str(python_version_flag)
8388

8489
# TODO: Uncomment this after --incompatible_python_disable_py2 defaults to true
8590
# if ctx.fragments.py.disable_py2 and python_version == "PY2":
@@ -133,13 +138,6 @@ def _py_runtime_impl(ctx):
133138
),
134139
]
135140

136-
def _is_singleton_depset(files):
137-
# Bazel 6 doesn't have this helper to optimize detecting singleton depsets.
138-
if _py_builtins:
139-
return _py_builtins.is_singleton_depset(files)
140-
else:
141-
return len(files.to_list()) == 1
142-
143141
# Bind to the name "py_runtime" to preserve the kind/rule_class it shows up
144142
# as elsewhere.
145143
py_runtime = rule(
@@ -260,15 +258,22 @@ the target platform. For an in-build runtime this attribute must not be set.
260258
"""),
261259
"interpreter_version_info": attr.string_dict(
262260
doc = """
263-
Version information about the interpreter this runtime provides. The
264-
supported keys match the names for `sys.version_info`. While the input
261+
Version information about the interpreter this runtime provides.
262+
263+
If not specified, uses {obj}`--python_version`
264+
265+
The supported keys match the names for `sys.version_info`. While the input
265266
values are strings, most are converted to ints. The supported keys are:
266267
* major: int, the major version number
267268
* minor: int, the minor version number
268269
* micro: optional int, the micro version number
269270
* releaselevel: optional str, the release level
270-
* serial: optional int, the serial number of the release"
271-
""",
271+
* serial: optional int, the serial number of the release
272+
273+
:::{versionchanged} 0.36.0
274+
{obj}`--python_version` determines the default value.
275+
:::
276+
""",
272277
mandatory = False,
273278
),
274279
"pyc_tag": attr.string(
@@ -327,5 +332,25 @@ The {obj}`PyRuntimeInfo.zip_main_template` field.
327332
:::
328333
""",
329334
),
335+
"_python_version_flag": attr.label(
336+
default = "//python/config_settings:python_version",
337+
),
330338
}),
331339
)
340+
341+
def _is_singleton_depset(files):
342+
# Bazel 6 doesn't have this helper to optimize detecting singleton depsets.
343+
if _py_builtins:
344+
return _py_builtins.is_singleton_depset(files)
345+
else:
346+
return len(files.to_list()) == 1
347+
348+
def _interpreter_version_info_from_version_str(version_str):
349+
parts = version_str.split(".")
350+
version_info = {}
351+
for key in ("major", "minor", "micro"):
352+
if not parts:
353+
break
354+
version_info[key] = parts.pop(0)
355+
356+
return version_info

tests/py_runtime/py_runtime_tests.bzl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ load("//python:py_runtime.bzl", "py_runtime")
2222
load("//python:py_runtime_info.bzl", "PyRuntimeInfo")
2323
load("//tests/base_rules:util.bzl", br_util = "util")
2424
load("//tests/support:py_runtime_info_subject.bzl", "py_runtime_info_subject")
25+
load("//tests/support:support.bzl", "PYTHON_VERSION")
2526

2627
_tests = []
2728

@@ -528,6 +529,31 @@ def _test_interpreter_version_info_parses_values_to_struct_impl(env, target):
528529

529530
_tests.append(_test_interpreter_version_info_parses_values_to_struct)
530531

532+
def _test_version_info_from_flag(name):
533+
py_runtime(
534+
name = name + "_subject",
535+
interpreter_version_info = None,
536+
interpreter_path = "/bogus",
537+
)
538+
analysis_test(
539+
name = name,
540+
target = name + "_subject",
541+
impl = _test_version_info_from_flag_impl,
542+
config_settings = {
543+
PYTHON_VERSION: "3.12",
544+
},
545+
)
546+
547+
def _test_version_info_from_flag_impl(env, target):
548+
version_info = env.expect.that_target(target).provider(PyRuntimeInfo, factory = py_runtime_info_subject).interpreter_version_info()
549+
version_info.major().equals(3)
550+
version_info.minor().equals(12)
551+
version_info.micro().equals(None)
552+
version_info.releaselevel().equals(None)
553+
version_info.serial().equals(None)
554+
555+
_tests.append(_test_version_info_from_flag)
556+
531557
def py_runtime_test_suite(name):
532558
test_suite(
533559
name = name,

0 commit comments

Comments
 (0)