Skip to content

Commit 3f14780

Browse files
committed
wip
1 parent 6c02eb5 commit 3f14780

File tree

5 files changed

+95
-7
lines changed

5 files changed

+95
-7
lines changed

python/private/python.bzl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,10 @@ def parse_modules(*, module_ctx, _fail = fail):
248248
# "3.X"` transitions work as expected.
249249
minor_version_toolchains = []
250250
other_toolchains = []
251+
minor_mapping = config.minor_mapping
251252
for t in toolchains:
252-
if t.python_version in config.minor_mapping:
253+
# TODO @aignas 2025-04-04: I am getting lost here when unit testing
254+
if t.python_version == minor_mapping.get(t.python_version):
253255
minor_version_toolchains.append(t)
254256
else:
255257
other_toolchains.append(t)
@@ -258,7 +260,7 @@ def parse_modules(*, module_ctx, _fail = fail):
258260
return struct(
259261
config = config,
260262
debug_info = debug_info,
261-
default_python_version = toolchains[-1].python_version,
263+
default_python_version = default_toolchain.python_version,
262264
toolchains = [
263265
struct(
264266
python_version = t.python_version,

python/uv/private/lock.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ def _lock_impl(ctx):
106106

107107
exec_tools = ctx.toolchains[EXEC_TOOLS_TOOLCHAIN_TYPE].exec_tools
108108
runtime = exec_tools.exec_interpreter[platform_common.ToolchainInfo].py3_runtime
109+
fail(runtime.interpreter_version_info)
109110
python = runtime.interpreter or runtime.interpreter_path
110111
python_files = runtime.files
111112
args.add("--python", python)

tests/config_settings/transition/multi_version_tests.bzl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414
"""Tests for py_test."""
1515

16+
load("@pythons_hub//:versions.bzl", "DEFAULT_PYTHON_VERSION")
1617
load("@rules_testing//lib:analysis_test.bzl", "analysis_test")
1718
load("@rules_testing//lib:test_suite.bzl", "test_suite")
1819
load("@rules_testing//lib:util.bzl", "TestingAspectInfo", rt_util = "util")
@@ -29,7 +30,7 @@ load("//tests/support:support.bzl", "CC_TOOLCHAIN")
2930
# If the toolchain is not resolved then you will have a weird message telling
3031
# you that your transition target does not have a PyRuntime provider, which is
3132
# caused by there not being a toolchain detected for the target.
32-
_PYTHON_VERSION = "3.11"
33+
_PYTHON_VERSION = DEFAULT_PYTHON_VERSION
3334

3435
_tests = []
3536

tests/python/python_tests.bzl

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,54 @@ def _test_default_non_rules_python_ignore_root_user_error_non_root_module(env):
284284

285285
_tests.append(_test_default_non_rules_python_ignore_root_user_error_non_root_module)
286286

287+
def _test_toolchain_ordering(env):
288+
py = parse_modules(
289+
module_ctx = _mock_mctx(
290+
_mod(
291+
name = "my_module",
292+
toolchain = [
293+
_toolchain("3.10"),
294+
_toolchain("3.10.15"),
295+
_toolchain("3.10.16"),
296+
_toolchain("3.10.11"),
297+
_toolchain("3.11.1"),
298+
_toolchain("3.11.10"),
299+
_toolchain("3.11.11", is_default = True),
300+
],
301+
),
302+
_mod(name = "rules_python", toolchain = [_toolchain("3.11")]),
303+
),
304+
)
305+
got_versions = [
306+
t.python_version
307+
for t in py.toolchains
308+
]
309+
310+
env.expect.that_str(py.default_python_version).equals("3.11")
311+
env.expect.that_dict(py.config.minor_mapping).contains_exactly({
312+
"3.10": "3.10.16",
313+
"3.11": "3.11.11",
314+
"3.12": "3.12.9",
315+
"3.13": "3.13.2",
316+
"3.8": "3.8.20",
317+
"3.9": "3.9.21",
318+
})
319+
env.expect.that_collection(got_versions).contains_exactly([
320+
# First the full-version toolchains that are in minor_mapping
321+
# so that they get matched first if only the `python_version` is in MINOR_MAPPING
322+
"3.10.16",
323+
# Next, the rest, where we will match things based on the `python_version` being
324+
# the same
325+
"3.10",
326+
"3.10.15",
327+
"3.10.11",
328+
"3.11.1",
329+
"3.11.10",
330+
"3.11.11",
331+
]).in_order()
332+
333+
_tests.append(_test_toolchain_ordering)
334+
287335
def _test_default_from_defaults(env):
288336
py = parse_modules(
289337
module_ctx = _mock_mctx(

tests/toolchains/defs.bzl

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414

1515
""
1616

17+
load("@pythons_hub//:versions.bzl", "MINOR_MAPPING", "DEFAULT_PYTHON_VERSION")
1718
load("//python:versions.bzl", "PLATFORMS", "TOOL_VERSIONS")
19+
load("//python/private:full_version.bzl", "full_version")
1820
load("//tests/support:sh_py_run_test.bzl", "py_reconfig_test")
1921

2022
def define_toolchain_tests(name):
@@ -30,7 +32,41 @@ def define_toolchain_tests(name):
3032
constraint_values = platform_info.compatible_with,
3133
)
3234

33-
for python_version, meta in TOOL_VERSIONS.items():
35+
# First we expect the transitions with a specific version to always
36+
# give us that specific version
37+
exact_version_tests = {
38+
(v, v): "python_{}_test".format(v)
39+
for v in TOOL_VERSIONS
40+
}
41+
native.test_suite(
42+
name = "exact_version_tests",
43+
tests = exact_version_tests.values(),
44+
)
45+
46+
# Then we expect to get the version in the MINOR_MAPPING if we provide
47+
# the version from the MINOR_MAPPING
48+
minor_mapping_tests = {
49+
(minor, full): "python_{}_test".format(minor)
50+
for minor, full in MINOR_MAPPING.items()
51+
}
52+
native.test_suite(
53+
name = "minor_mapping_tests",
54+
tests = minor_mapping_tests.values(),
55+
)
56+
57+
# Lastly, if we don't provide any version to the transition, we should
58+
# get the default version
59+
default_version = full_version(
60+
version = DEFAULT_PYTHON_VERSION,
61+
minor_mapping = MINOR_MAPPING,
62+
)
63+
default_version_tests = {
64+
(None, default_version): "default_version_test"
65+
}
66+
tests = exact_version_tests | minor_mapping_tests | default_version_tests
67+
68+
for (input_python_version, expect_python_version), test_name in tests.items():
69+
meta = TOOL_VERSIONS[expect_python_version]
3470
target_compatible_with = {
3571
"//conditions:default": ["@platforms//:incompatible"],
3672
}
@@ -39,12 +75,12 @@ def define_toolchain_tests(name):
3975
target_compatible_with[is_platform] = []
4076

4177
py_reconfig_test(
42-
name = "python_{}_test".format(python_version),
78+
name = test_name,
4379
srcs = ["python_toolchain_test.py"],
4480
main = "python_toolchain_test.py",
45-
python_version = python_version,
81+
python_version = input_python_version,
4682
env = {
47-
"EXPECT_PYTHON_VERSION": python_version,
83+
"EXPECT_PYTHON_VERSION": expect_python_version,
4884
},
4985
deps = ["//python/runfiles"],
5086
data = ["//tests/support:current_build_settings"],

0 commit comments

Comments
 (0)