Skip to content

feat(python.toolchain): support file-based default Python version #2588

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Mar 29, 2025
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ Unreleased changes template.

{#v0-0-0-added}
### Added
* (python) {attr}`python.toolchain.default_version_file` has been added to
allow users to set the default python version in the root module by reading
the default version number from a file.
* {obj}`//python/bin:python`: convenience target for directly running an
interpreter. {obj}`--//python/bin:python_src` can be used to specify a
binary whose interpreter to use.
Expand Down
29 changes: 27 additions & 2 deletions python/private/python.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,13 @@ def parse_modules(*, module_ctx, _fail = fail):
# * rules_python needs to set a soft default in case the root module doesn't,
# e.g. if the root module doesn't use Python itself.
# * The root module is allowed to override the rules_python default.
is_default = toolchain_attr.is_default
if toolchain_attr.default_version_file:
version_from_file = module_ctx.read(toolchain_attr.default_version_file).strip()
is_default = version_from_file == toolchain_version
else:
is_default = toolchain_attr.is_default
if toolchain_attr.is_default and not is_default:
fail("The 'is_default' attribute doesn't work if you set 'default_version_file'.")

# Also only the root module should be able to decide ignore_root_user_error.
# Modules being depended upon don't know the final environment, so they aren't
Expand Down Expand Up @@ -560,6 +566,7 @@ def _create_toolchain_attrs_struct(*, tag = None, python_version = None, toolcha
python_version = python_version if python_version else tag.python_version,
configure_coverage_tool = getattr(tag, "configure_coverage_tool", False),
ignore_root_user_error = getattr(tag, "ignore_root_user_error", True),
default_version_file = getattr(tag, "default_version_file", None),
)

def _get_bazel_version_specific_kwargs():
Expand Down Expand Up @@ -635,6 +642,18 @@ Then the python interpreter will be available as `my_python_name`.
mandatory = False,
doc = "Whether or not to configure the default coverage tool provided by `rules_python` for the compatible toolchains.",
),
"default_version_file": attr.label(
mandatory = False,
allow_single_file = True,
doc = """\
File saying what the default Python version should be. If the contents of the
file match the {attr}`python_version` attribute, this toolchain is the default version.
If this attribute is set, the {attr}`is_default` attribute is ignored.

:::{versionadded} VERSION_NEXT_FEATURE
:::
""",
),
"ignore_root_user_error": attr.bool(
default = True,
doc = """\
Expand All @@ -653,7 +672,13 @@ error to run with root access instead.
),
"is_default": attr.bool(
mandatory = False,
doc = "Whether the toolchain is the default version",
doc = """\
Whether the toolchain is the default version.

:::{versionchanged} VERSION_NEXT_FEATURE
This setting is ignored if {attr}`default_version_file` is set.
:::
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we deprecate this in favour of the defaults tag_class?

cc: @rickeylev what do you think?

""",
),
"python_version": attr.string(
mandatory = True,
Expand Down