Skip to content

Commit b9f39bf

Browse files
authored
fix(bzlmod): allow users using X.Y.Z python_version in pip.parse (#1830)
Before only X.Y would work and with this change users can also specify the exact python version. Fixes #1371
1 parent 584a9ae commit b9f39bf

File tree

5 files changed

+26
-12
lines changed

5 files changed

+26
-12
lines changed

.bazelrc

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
# (Note, we cannot use `common --deleted_packages` because the bazel version command doesn't support it)
55
# To update these lines, execute
66
# `bazel run @rules_bazel_integration_test//tools:update_deleted_packages`
7-
build --deleted_packages=examples/build_file_generation,examples/build_file_generation/random_number_generator,examples/bzlmod,examples/bzlmod_build_file_generation,examples/bzlmod_build_file_generation/other_module/other_module/pkg,examples/bzlmod_build_file_generation/runfiles,examples/bzlmod/entry_points,examples/bzlmod/entry_points/tests,examples/bzlmod/libs/my_lib,examples/bzlmod/other_module,examples/bzlmod/other_module/other_module/pkg,examples/bzlmod/patches,examples/bzlmod/py_proto_library,examples/bzlmod/py_proto_library/example.com/another_proto,examples/bzlmod/py_proto_library/example.com/proto,examples/bzlmod/runfiles,examples/bzlmod/tests,examples/bzlmod/tests/dupe_requirements,examples/bzlmod/tests/other_module,examples/bzlmod/whl_mods,examples/multi_python_versions/libs/my_lib,examples/multi_python_versions/requirements,examples/multi_python_versions/tests,examples/pip_parse,examples/pip_parse_vendored,examples/pip_repository_annotations,examples/py_proto_library,examples/py_proto_library/example.com/another_proto,examples/py_proto_library/example.com/proto,gazelle,gazelle/manifest,gazelle/manifest/generate,gazelle/manifest/hasher,gazelle/manifest/test,gazelle/modules_mapping,gazelle/python,gazelle/pythonconfig,tests/integration/compile_pip_requirements,tests/integration/compile_pip_requirements_test_from_external_repo,tests/integration/ignore_root_user_error,tests/integration/ignore_root_user_error/submodule,tests/integration/pip_parse,tests/integration/pip_parse/empty,tests/integration/pip_repository_entry_points,tests/integration/py_cc_toolchain_registered
8-
query --deleted_packages=examples/build_file_generation,examples/build_file_generation/random_number_generator,examples/bzlmod,examples/bzlmod_build_file_generation,examples/bzlmod_build_file_generation/other_module/other_module/pkg,examples/bzlmod_build_file_generation/runfiles,examples/bzlmod/entry_points,examples/bzlmod/entry_points/tests,examples/bzlmod/libs/my_lib,examples/bzlmod/other_module,examples/bzlmod/other_module/other_module/pkg,examples/bzlmod/patches,examples/bzlmod/py_proto_library,examples/bzlmod/py_proto_library/example.com/another_proto,examples/bzlmod/py_proto_library/example.com/proto,examples/bzlmod/runfiles,examples/bzlmod/tests,examples/bzlmod/tests/dupe_requirements,examples/bzlmod/tests/other_module,examples/bzlmod/whl_mods,examples/multi_python_versions/libs/my_lib,examples/multi_python_versions/requirements,examples/multi_python_versions/tests,examples/pip_parse,examples/pip_parse_vendored,examples/pip_repository_annotations,examples/py_proto_library,examples/py_proto_library/example.com/another_proto,examples/py_proto_library/example.com/proto,gazelle,gazelle/manifest,gazelle/manifest/generate,gazelle/manifest/hasher,gazelle/manifest/test,gazelle/modules_mapping,gazelle/python,gazelle/pythonconfig,tests/integration/compile_pip_requirements,tests/integration/compile_pip_requirements_test_from_external_repo,tests/integration/ignore_root_user_error,tests/integration/ignore_root_user_error/submodule,tests/integration/pip_parse,tests/integration/pip_parse/empty,tests/integration/pip_repository_entry_points,tests/integration/py_cc_toolchain_registered
7+
build --deleted_packages=examples/build_file_generation,examples/build_file_generation/random_number_generator,examples/bzlmod,examples/bzlmod/entry_points,examples/bzlmod/entry_points/tests,examples/bzlmod/libs/my_lib,examples/bzlmod/other_module,examples/bzlmod/other_module/other_module/pkg,examples/bzlmod/patches,examples/bzlmod/py_proto_library,examples/bzlmod/py_proto_library/example.com/another_proto,examples/bzlmod/py_proto_library/example.com/proto,examples/bzlmod/runfiles,examples/bzlmod/tests,examples/bzlmod/tests/dupe_requirements,examples/bzlmod/tests/other_module,examples/bzlmod/whl_mods,examples/bzlmod_build_file_generation,examples/bzlmod_build_file_generation/other_module/other_module/pkg,examples/bzlmod_build_file_generation/runfiles,examples/multi_python_versions/libs/my_lib,examples/multi_python_versions/requirements,examples/multi_python_versions/tests,examples/pip_parse,examples/pip_parse_vendored,examples/pip_repository_annotations,examples/py_proto_library,examples/py_proto_library/example.com/another_proto,examples/py_proto_library/example.com/proto,gazelle,gazelle/manifest,gazelle/manifest/generate,gazelle/manifest/hasher,gazelle/manifest/test,gazelle/modules_mapping,gazelle/python,gazelle/pythonconfig,tests/integration/compile_pip_requirements,tests/integration/compile_pip_requirements_test_from_external_repo,tests/integration/ignore_root_user_error,tests/integration/ignore_root_user_error/submodule,tests/integration/pip_parse,tests/integration/pip_parse/empty,tests/integration/pip_repository_entry_points,tests/integration/py_cc_toolchain_registered
8+
query --deleted_packages=examples/build_file_generation,examples/build_file_generation/random_number_generator,examples/bzlmod,examples/bzlmod/entry_points,examples/bzlmod/entry_points/tests,examples/bzlmod/libs/my_lib,examples/bzlmod/other_module,examples/bzlmod/other_module/other_module/pkg,examples/bzlmod/patches,examples/bzlmod/py_proto_library,examples/bzlmod/py_proto_library/example.com/another_proto,examples/bzlmod/py_proto_library/example.com/proto,examples/bzlmod/runfiles,examples/bzlmod/tests,examples/bzlmod/tests/dupe_requirements,examples/bzlmod/tests/other_module,examples/bzlmod/whl_mods,examples/bzlmod_build_file_generation,examples/bzlmod_build_file_generation/other_module/other_module/pkg,examples/bzlmod_build_file_generation/runfiles,examples/multi_python_versions/libs/my_lib,examples/multi_python_versions/requirements,examples/multi_python_versions/tests,examples/pip_parse,examples/pip_parse_vendored,examples/pip_repository_annotations,examples/py_proto_library,examples/py_proto_library/example.com/another_proto,examples/py_proto_library/example.com/proto,gazelle,gazelle/manifest,gazelle/manifest/generate,gazelle/manifest/hasher,gazelle/manifest/test,gazelle/modules_mapping,gazelle/python,gazelle/pythonconfig,tests/integration/compile_pip_requirements,tests/integration/compile_pip_requirements_test_from_external_repo,tests/integration/ignore_root_user_error,tests/integration/ignore_root_user_error/submodule,tests/integration/pip_parse,tests/integration/pip_parse/empty,tests/integration/pip_repository_entry_points,tests/integration/py_cc_toolchain_registered
99

1010
test --test_output=errors
1111

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ A brief description of the categories of changes:
3232
* (whl_library) The group redirection is only added when the package is part of
3333
the group potentially fixing aspects that want to traverse a `py_library` graph.
3434
Fixes [#1760](https://github.com/bazelbuild/rules_python/issues/1760).
35+
* (bzlmod) Setting a particular micro version for the interpreter and the
36+
`pip.parse` extension is now possible, see the
37+
`examples/pip_parse/MODULE.bazel` for how to do it.
38+
See [#1371](https://github.com/bazelbuild/rules_python/issues/1371).
3539

3640
### Added
3741

examples/pip_parse/MODULE.bazel

+11-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,15 @@ local_path_override(
88

99
python = use_extension("@rules_python//python/extensions:python.bzl", "python")
1010
python.toolchain(
11-
python_version = "3.9",
11+
# We can specify the exact version.
12+
python_version = "3.9.13",
13+
)
14+
15+
# You can use this repo mapping to ensure that your BUILD.bazel files don't need
16+
# to be updated when the python version changes to a different `3.9` version.
17+
use_repo(
18+
python,
19+
python_3_9 = "python_3_9_13",
1220
)
1321

1422
pip = use_extension("@rules_python//python/extensions:pip.bzl", "pip")
@@ -24,7 +32,8 @@ pip.parse(
2432
],
2533
},
2634
hub_name = "pypi",
27-
python_version = "3.9",
35+
# We need to use the same version here as in the `python.toolchain` call.
36+
python_version = "3.9.13",
2837
requirements_lock = "//:requirements_lock.txt",
2938
requirements_windows = "//:requirements_windows.txt",
3039
)

examples/pip_parse/WORKSPACE

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ load("@rules_python//python:repositories.bzl", "py_repositories", "python_regist
1010
py_repositories()
1111

1212
python_register_toolchains(
13-
name = "python39",
14-
python_version = "3.9",
13+
name = "python_3_9",
14+
python_version = "3.9.13",
1515
)
1616

1717
load("@rules_python//python:pip.bzl", "pip_parse")
@@ -51,7 +51,7 @@ pip_parse(
5151
# 3. Wrapper script, like in the autodetecting python toolchain.
5252
#
5353
# Here, we use the interpreter constant that resolves to the host interpreter from the default Python toolchain.
54-
python_interpreter_target = "@python39_host//:python",
54+
python_interpreter_target = "@python_3_9_host//:python",
5555

5656
# (Optional) You can set quiet to False if you want to see pip output.
5757
#quiet = False,

python/private/bzlmod/pip.bzl

+6-5
Original file line numberDiff line numberDiff line change
@@ -106,16 +106,19 @@ def _create_whl_repos(module_ctx, pip_attr, whl_map, whl_overrides):
106106
hub_name = pip_attr.hub_name
107107
if python_interpreter_target == None and not pip_attr.python_interpreter:
108108
python_name = "python_{}_host".format(
109-
version_label(pip_attr.python_version, sep = "_"),
109+
pip_attr.python_version.replace(".", "_"),
110110
)
111111
if python_name not in INTERPRETER_LABELS:
112112
fail((
113113
"Unable to find interpreter for pip hub '{hub_name}' for " +
114114
"python_version={version}: Make sure a corresponding " +
115-
'`python.toolchain(python_version="{version}")` call exists'
115+
'`python.toolchain(python_version="{version}")` call exists.' +
116+
"Expected to find {python_name} among registered versions:\n {labels}"
116117
).format(
117118
hub_name = hub_name,
118119
version = pip_attr.python_version,
120+
python_name = python_name,
121+
labels = " \n".join(INTERPRETER_LABELS),
119122
))
120123
python_interpreter_target = INTERPRETER_LABELS[python_name]
121124

@@ -405,9 +408,7 @@ Targets from different hubs should not be used together.
405408
mandatory = True,
406409
doc = """
407410
The Python version the dependencies are targetting, in Major.Minor format
408-
(e.g., "3.11"). Patch level granularity (e.g. "3.11.1") is not supported.
409-
If not specified, then the default Python version (as set by the root module or
410-
rules_python) will be used.
411+
(e.g., "3.11") or patch level granularity (e.g. "3.11.1").
411412
412413
If an interpreter isn't explicitly provided (using `python_interpreter` or
413414
`python_interpreter_target`), then the version specified here must have

0 commit comments

Comments
 (0)