Skip to content

Commit 46f08de

Browse files
authored
docs/refactor: Use python.defaults, not is_default (#2924)
When there are multiple Python toolchains, there are currently two ways of setting the default version: the `is_default` attribute of the `python.toolchain()` tag class and the `python.defaults()` tag class. The latter is more powerful, since it also supports files and environment variables. This patch updates the examples and the docs to use `python.defaults()`. Relates to pull request #2588 and issue #2587.
1 parent 85fcd7a commit 46f08de

File tree

8 files changed

+33
-22
lines changed

8 files changed

+33
-22
lines changed

docs/api/rules_python/python/bin/index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
A target to directly run a Python interpreter.
1111

1212
By default, it uses the Python version that toolchain resolution matches
13-
(typically the one marked `is_default=True` in `MODULE.bazel`).
13+
(typically the one set with `python.defaults(python_version = ...)` in
14+
`MODULE.bazel`).
1415

1516
This runs a Python interpreter in a similar manner as when running `python3`
1617
on the command line. It can be invoked using `bazel run`. Remember that in

docs/toolchains.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ you should read the dev-only library module section.
4444
bazel_dep(name="rules_python", version=...)
4545
python = use_extension("@rules_python//python/extensions:python.bzl", "python")
4646
47-
python.toolchain(python_version = "3.12", is_default = True)
47+
python.defaults(python_version = "3.12")
48+
python.toolchain(python_version = "3.12")
4849
```
4950

5051
### Library modules
@@ -72,7 +73,8 @@ python = use_extension(
7273
dev_dependency = True
7374
)
7475
75-
python.toolchain(python_version = "3.12", is_default=True)
76+
python.defaults(python_version = "3.12")
77+
python.toolchain(python_version = "3.12")
7678
```
7779

7880
#### Library modules without version constraints
@@ -161,9 +163,13 @@ Multiple versions can be specified and used within a single build.
161163
# MODULE.bazel
162164
python = use_extension("@rules_python//python/extensions:python.bzl", "python")
163165

166+
python.defaults(
167+
# The environment variable takes precedence if set.
168+
python_version = "3.11",
169+
python_version_env = "BAZEL_PYTHON_VERSION",
170+
)
164171
python.toolchain(
165172
python_version = "3.11",
166-
is_default = True,
167173
)
168174

169175
python.toolchain(
@@ -264,7 +270,8 @@ bazel_dep(name = "rules_python", version = "0.40.0")
264270

265271
python = use_extension("@rules_python//python/extensions:python.bzl", "python")
266272

267-
python.toolchain(is_default = True, python_version = "3.10")
273+
python.defaults(python_version = "3.10")
274+
python.toolchain(python_version = "3.10")
268275

269276
use_repo(python, "python_3_10", "python_3_10_host")
270277
```

examples/bzlmod/MODULE.bazel

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,13 @@ bazel_dep(name = "rules_rust", version = "0.54.1")
2828
# We next initialize the python toolchain using the extension.
2929
# You can set different Python versions in this block.
3030
python = use_extension("@rules_python//python/extensions:python.bzl", "python")
31+
python.defaults(
32+
# Use python.defaults if you have defined multiple toolchain versions.
33+
python_version = "3.9",
34+
python_version_env = "BAZEL_PYTHON_VERSION",
35+
)
3136
python.toolchain(
3237
configure_coverage_tool = True,
33-
# Only set when you have multiple toolchain versions.
34-
is_default = True,
3538
python_version = "3.9",
3639
)
3740

examples/bzlmod/other_module/MODULE.bazel

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,16 @@ PYTHON_NAME_39 = "python_3_9"
2525
PYTHON_NAME_311 = "python_3_11"
2626

2727
python = use_extension("@rules_python//python/extensions:python.bzl", "python")
28+
python.defaults(
29+
# In a submodule this is ignored
30+
python_version = "3.11",
31+
)
2832
python.toolchain(
2933
configure_coverage_tool = True,
3034
python_version = "3.9",
3135
)
3236
python.toolchain(
3337
configure_coverage_tool = True,
34-
# In a submodule this is ignored
35-
is_default = True,
3638
python_version = "3.11",
3739
)
3840

examples/bzlmod_build_file_generation/MODULE.bazel

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,13 @@ python = use_extension("@rules_python//python/extensions:python.bzl", "python")
4646

4747
# We next initialize the python toolchain using the extension.
4848
# You can set different Python versions in this block.
49+
python.defaults(
50+
# The environment variable takes precedence if set.
51+
python_version = "3.9",
52+
python_version_env = "BAZEL_PYTHON_VERSION",
53+
)
4954
python.toolchain(
5055
configure_coverage_tool = True,
51-
is_default = True,
5256
python_version = "3.9",
5357
)
5458

examples/multi_python_versions/MODULE.bazel

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ python.defaults(
1717
)
1818
python.toolchain(
1919
configure_coverage_tool = True,
20-
# Only set when you have mulitple toolchain versions.
21-
is_default = True,
2220
python_version = "3.9",
2321
)
2422
python.toolchain(

python/extensions/python.bzl

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,8 @@ The simplest way to configure the toolchain with `rules_python` is as follows.
2020
2121
```starlark
2222
python = use_extension("@rules_python//python/extensions:python.bzl", "python")
23-
python.toolchain(
24-
is_default = True,
25-
python_version = "3.11",
26-
)
23+
python.defaults(python_version = "3.11")
24+
python.toolchain(python_version = "3.11")
2725
use_repo(python, "python_3_11")
2826
```
2927

python/private/python.bzl

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ def parse_modules(*, module_ctx, _fail = fail):
223223
# A default toolchain is required so that the non-version-specific rules
224224
# are able to match a toolchain.
225225
if default_toolchain == None:
226-
fail("No default Python toolchain configured. Is rules_python missing `is_default=True`?")
226+
fail("No default Python toolchain configured. Is rules_python missing `python.defaults()`?")
227227
elif default_toolchain.python_version not in global_toolchain_versions:
228228
fail('Default version "{python_version}" selected by module ' +
229229
'"{module_name}", but no toolchain with that version registered'.format(
@@ -891,10 +891,8 @@ In order to use a different name than the above, you can use the following `MODU
891891
syntax:
892892
```starlark
893893
python = use_extension("@rules_python//python/extensions:python.bzl", "python")
894-
python.toolchain(
895-
is_default = True,
896-
python_version = "3.11",
897-
)
894+
python.defaults(python_version = "3.11")
895+
python.toolchain(python_version = "3.11")
898896
899897
use_repo(python, my_python_name = "python_3_11")
900898
```
@@ -930,7 +928,7 @@ Whether the toolchain is the default version.
930928
931929
:::{versionchanged} 1.4.0
932930
This setting is ignored if the default version is set using the `defaults`
933-
tag class.
931+
tag class (encouraged).
934932
:::
935933
""",
936934
),

0 commit comments

Comments
 (0)