Skip to content

Commit f038234

Browse files
committed
fix: use the python micro version to parse whl metadata in bzlmod
Add `<micro>` version to the target platform. Instead of `cpxy_os_cpu` the target platform string format becomes `cpxy.z_os_cpu` and this PR just spikes the implementation to see the main points that need to be done: - [ ] test `select_whls` function needs to be tested to ensure that the whl selection is not impacted when we have the full version in the target platform. - [ ] `download_only` legacy whl code path in `bzlmod` needs further testing. - [ ] test `whl_config_setting` handling. - [ ] doc the new format and add changelog. - [x] `pep508_deps` tests for handling the `full_python_version` correctly. Fixes #2319
1 parent a19e1e4 commit f038234

File tree

7 files changed

+43
-15
lines changed

7 files changed

+43
-15
lines changed

python/private/pypi/extension.bzl

+7-5
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616

1717
load("@bazel_features//:features.bzl", "bazel_features")
1818
load("@pythons_hub//:interpreters.bzl", "INTERPRETER_LABELS")
19+
load("@pythons_hub//:versions.bzl", "MINOR_MAPPING")
1920
load("//python/private:auth.bzl", "AUTH_ATTRS")
2021
load("//python/private:normalize_name.bzl", "normalize_name")
22+
load("//python/private:full_version.bzl", "full_version")
2123
load("//python/private:repo_utils.bzl", "repo_utils")
2224
load("//python/private:semver.bzl", "semver")
2325
load("//python/private:version_label.bzl", "version_label")
@@ -68,6 +70,7 @@ def _create_whl_repos(
6870
pip_attr,
6971
whl_overrides,
7072
available_interpreters = INTERPRETER_LABELS,
73+
minor_mapping = MINOR_MAPPING,
7174
get_index_urls = None):
7275
"""create all of the whl repositories
7376
@@ -159,8 +162,10 @@ def _create_whl_repos(
159162
requirements_osx = pip_attr.requirements_darwin,
160163
requirements_windows = pip_attr.requirements_windows,
161164
extra_pip_args = pip_attr.extra_pip_args,
162-
# TODO @aignas 2025-04-15: pass the full version into here
163-
python_version = major_minor,
165+
python_version = full_version(
166+
version=pip_attr.python_version,
167+
minor_mapping=minor_mapping,
168+
),
164169
logger = logger,
165170
),
166171
extra_pip_args = pip_attr.extra_pip_args,
@@ -304,9 +309,6 @@ def _whl_repos(*, requirement, whl_library_args, download_only, netrc, auth_patt
304309
if requirement.extra_pip_args:
305310
args["extra_pip_args"] = requirement.extra_pip_args
306311

307-
if download_only:
308-
args.setdefault("experimental_target_platforms", requirement.target_platforms)
309-
310312
target_platforms = requirement.target_platforms if multiple_requirements_for_whl else []
311313
repo_name = pypi_repo_name(
312314
normalize_name(requirement.distribution),

python/private/pypi/pkg_aliases.bzl

+2
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,8 @@ def get_filename_config_settings(
370370
py = "py3_"
371371

372372
abi = parsed.abi_tag
373+
# TODO @aignas 2025-04-20: test
374+
abi, _, _ = abi.partition(".")
373375

374376
if parsed.platform_tag == "any":
375377
prefixes = ["{}{}_any".format(py, abi)]

python/private/pypi/requirements_files_by_platform.bzl

+3-4
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,12 @@ def _platforms_from_args(extra_pip_args):
9191
return list(platforms.keys())
9292

9393
def _platform(platform_string, python_version = None):
94-
if not python_version or platform_string.startswith("cp3"):
94+
if not python_version or platform_string.startswith("cp"):
9595
return platform_string
9696

97-
_, _, tail = python_version.partition(".")
98-
minor, _, _ = tail.partition(".")
97+
major, _, tail = python_version.partition(".")
9998

100-
return "cp3{}_{}".format(minor, platform_string)
99+
return "cp{}{}_{}".format(major, tail, platform_string)
101100

102101
def requirements_files_by_platform(
103102
*,

python/private/pypi/whl_config_setting.bzl

+11-1
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,20 @@ def whl_config_setting(*, version = None, config_setting = None, filename = None
3535
a struct with the validated and parsed values.
3636
"""
3737
if target_platforms:
38-
for p in target_platforms:
38+
target_platforms_input = target_platforms
39+
target_platforms = []
40+
for p in target_platforms_input:
3941
if not p.startswith("cp"):
4042
fail("target_platform should start with 'cp' denoting the python version, got: " + p)
4143

44+
abi, _, tail = p.partition("_")
45+
# drop the micro version here, currently there is no usecase to use
46+
# multiple python interpreters with the same minor version but
47+
# different micro version.
48+
abi, _, _ = abi.partition(".")
49+
target_platforms.append("{}_{}".format(abi, tail))
50+
51+
4252
return struct(
4353
config_setting = config_setting,
4454
filename = filename,

python/private/pypi/whl_target_platforms.bzl

+3-1
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,10 @@ def select_whls(*, whls, want_platforms = [], logger = None):
7575
fail("expected all platforms to start with ABI, but got: {}".format(p))
7676

7777
abi, _, os_cpu = p.partition("_")
78+
abi, _, _ = abi.partition(".")
7879
_want_platforms[os_cpu] = None
79-
_want_platforms[p] = None
80+
# TODO @aignas 2025-04-20: add a test
81+
_want_platforms["{}_{}".format(abi, os_cpu)] = None
8082

8183
version_limit_candidate = int(abi[3:])
8284
if not version_limit:

tests/pypi/extension/extension_tests.bzl

+9-3
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ def _test_simple(env):
157157
available_interpreters = {
158158
"python_3_15_host": "unit_test_interpreter_target",
159159
},
160+
minor_mapping = {"3.15": "3.15.19"},
160161
)
161162

162163
pypi.exposed_packages().contains_exactly({"pypi": ["simple"]})
@@ -204,6 +205,7 @@ def _test_simple_multiple_requirements(env):
204205
available_interpreters = {
205206
"python_3_15_host": "unit_test_interpreter_target",
206207
},
208+
minor_mapping = {"3.15": "3.15.19"},
207209
)
208210

209211
pypi.exposed_packages().contains_exactly({"pypi": ["simple"]})
@@ -270,6 +272,7 @@ torch==2.4.1 ; platform_machine != 'x86_64' \
270272
available_interpreters = {
271273
"python_3_15_host": "unit_test_interpreter_target",
272274
},
275+
minor_mapping = {"3.15": "3.15.19"},
273276
)
274277

275278
pypi.exposed_packages().contains_exactly({"pypi": ["torch"]})
@@ -392,6 +395,7 @@ torch==2.4.1+cpu ; platform_machine == 'x86_64' \
392395
available_interpreters = {
393396
"python_3_12_host": "unit_test_interpreter_target",
394397
},
398+
minor_mapping = {"3.12": "3.12.19"},
395399
simpleapi_download = mocksimpleapi_download,
396400
)
397401

@@ -515,6 +519,7 @@ simple==0.0.3 \
515519
available_interpreters = {
516520
"python_3_15_host": "unit_test_interpreter_target",
517521
},
522+
minor_mapping = {"3.15": "3.15.19"},
518523
)
519524

520525
pypi.exposed_packages().contains_exactly({"pypi": ["simple"]})
@@ -544,23 +549,22 @@ simple==0.0.3 \
544549
"pypi_315_extra": {
545550
"dep_template": "@pypi//{name}:{target}",
546551
"download_only": True,
547-
"experimental_target_platforms": ["cp315_linux_x86_64"],
552+
# TODO @aignas 2025-04-20: ensure that this is in the hub repo
553+
# "experimental_target_platforms": ["cp315_linux_x86_64"],
548554
"extra_pip_args": ["--platform=manylinux_2_17_x86_64", "--python-version=315", "--implementation=cp", "--abi=cp315"],
549555
"python_interpreter_target": "unit_test_interpreter_target",
550556
"requirement": "extra==0.0.1 --hash=sha256:deadb00f",
551557
},
552558
"pypi_315_simple_linux_x86_64": {
553559
"dep_template": "@pypi//{name}:{target}",
554560
"download_only": True,
555-
"experimental_target_platforms": ["cp315_linux_x86_64"],
556561
"extra_pip_args": ["--platform=manylinux_2_17_x86_64", "--python-version=315", "--implementation=cp", "--abi=cp315"],
557562
"python_interpreter_target": "unit_test_interpreter_target",
558563
"requirement": "simple==0.0.1 --hash=sha256:deadbeef",
559564
},
560565
"pypi_315_simple_osx_aarch64": {
561566
"dep_template": "@pypi//{name}:{target}",
562567
"download_only": True,
563-
"experimental_target_platforms": ["cp315_osx_aarch64"],
564568
"extra_pip_args": ["--platform=macosx_10_9_arm64", "--python-version=315", "--implementation=cp", "--abi=cp315"],
565569
"python_interpreter_target": "unit_test_interpreter_target",
566570
"requirement": "simple==0.0.3 --hash=sha256:deadbaaf",
@@ -648,6 +652,7 @@ git_dep @ git+https://git.server/repo/project@deadbeefdeadbeef
648652
available_interpreters = {
649653
"python_3_15_host": "unit_test_interpreter_target",
650654
},
655+
minor_mapping = {"3.15": "3.15.19"},
651656
simpleapi_download = mocksimpleapi_download,
652657
)
653658

@@ -850,6 +855,7 @@ optimum[onnxruntime-gpu]==1.17.1 ; sys_platform == 'linux'
850855
available_interpreters = {
851856
"python_3_15_host": "unit_test_interpreter_target",
852857
},
858+
minor_mapping = {"3.15": "3.15.19"},
853859
)
854860

855861
pypi.exposed_packages().contains_exactly({"pypi": []})

tests/pypi/pep508/deps_tests.bzl

+8-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ _tests.append(test_self_dependencies_can_come_in_any_order)
154154
def _test_can_get_deps_based_on_specific_python_version(env):
155155
requires_dist = [
156156
"bar",
157-
"baz; python_version < '3.8'",
157+
"baz; python_full_version < '3.7.3'",
158158
"posix_dep; os_name=='posix' and python_version >= '3.8'",
159159
]
160160

@@ -163,6 +163,11 @@ def _test_can_get_deps_based_on_specific_python_version(env):
163163
requires_dist = requires_dist,
164164
platforms = ["cp38_linux_x86_64"],
165165
)
166+
py373 = deps(
167+
"foo",
168+
requires_dist = requires_dist,
169+
platforms = ["cp37.3_linux_x86_64"],
170+
)
166171
py37 = deps(
167172
"foo",
168173
requires_dist = requires_dist,
@@ -174,6 +179,8 @@ def _test_can_get_deps_based_on_specific_python_version(env):
174179
env.expect.that_dict(py37.deps_select).contains_exactly({})
175180
env.expect.that_collection(py38.deps).contains_exactly(["bar", "posix_dep"])
176181
env.expect.that_dict(py38.deps_select).contains_exactly({})
182+
env.expect.that_collection(py373.deps).contains_exactly(["bar"])
183+
env.expect.that_dict(py373.deps_select).contains_exactly({})
177184

178185
_tests.append(_test_can_get_deps_based_on_specific_python_version)
179186

0 commit comments

Comments
 (0)