feat: enable pyproject.toml as single source of truth for Python version#3514
feat: enable pyproject.toml as single source of truth for Python version#3514janwinkler1 wants to merge 2 commits into
Conversation
Summary of ChangesHello @janwinkler1, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the Python ecosystem's integration by introducing Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces a significant and valuable feature by enabling pyproject.toml as a single source of truth for Python versions in both python.defaults() and pip.default(). This enhancement greatly improves consistency and maintainability across projects. The implementation includes a new repository rule (pyproject_version_repo) and utility functions to extract and validate the Python version from pyproject.toml. Additionally, config_settings in pip.default() has been made optional, which is a good usability improvement. The changes are well-tested with dedicated integration and priority tests, ensuring the new functionality works as expected and respects the defined priority order for Python version resolution. The code is clean, well-documented, and adheres to best practices.
958106b to
1a1fef2
Compare
1a1fef2 to
bd73882
Compare
|
Hey, thank you for the contribution. In general I am +1 on having the python version read from
What is the benefit of this? If we are setting the default version through the
+1 on this change. If you'd like to merge it earlier than we decide on the various
LGTM, but I am wondering if this is the simplest possible interface. We could instead reuse, continue using the machinery that is used by
Do we need to restrict the user to
Is the idea to set the default value for |
|
I, too, am supportive of this.
I'm not worried about that. We should just pick a python runtime to use for repo-phase logic that rules_python uses. It simplifies us being able to assume what python is being used and its capabilities. There's also the
I'd say lets just go with what the author is willing to contribute. If someone later wants |
|
hi @aignas and @rickeylev, thank you for your comments! i am currently taking a vacation, will address your comments nexy week! cheerio |
rickeylev
left a comment
There was a problem hiding this comment.
Core logic is OK, but some of the impl details and tests need to change
| continue | ||
|
|
||
| for tag in mod.tags.default: | ||
| pyproject_toml = getattr(tag, "pyproject_toml", None) |
There was a problem hiding this comment.
The pyproject tag will always be present, so this can use normal . for accessing.
There was a problem hiding this comment.
Kept getattr because the existing unit tests in tests/pypi/extension/ create mock tag structs that don't include pyproject_toml. Using tag.pyproject_toml breaks test_simple. Happy to update the test mocks instead if preferred.
There was a problem hiding this comment.
+1 to update the mocks. In there setting it to None is good enough. There is a function where that would be easy to do.
| ), | ||
| "config_settings": attr.label_list( | ||
| mandatory = True, | ||
| mandatory = False, |
There was a problem hiding this comment.
I think a recent PR fixed this?
There was a problem hiding this comment.
@rickeylev do you happen to know which one? i havent found it.. could also split this out as a separate PR if you'd prefer it merged independently.
| ), | ||
| "pyproject_toml": attr.label( | ||
| mandatory = False, | ||
| allow_single_file = True, |
There was a problem hiding this comment.
allow_single_file can be omitted. It has no meaning for repo/bzlmod rules
| The version must be specified as `==X.Y.Z` (exact version with full semver). | ||
| This is designed to work with dependency management tools like Renovate. | ||
|
|
||
| :::{versionadded} 1.8.0 |
There was a problem hiding this comment.
Change to VERSION_NEXT_FEATURE instead of 1.8.0
| ), | ||
| "python_version": attr.string( | ||
| mandatory = True, | ||
| mandatory = False, |
There was a problem hiding this comment.
update python_version attr doc to indicate the other option
add e.g.
:::{seealso}
The {obj}`pyproject_toml` attribute for getting the version from a project file.
:::
There was a problem hiding this comment.
Ah, this is a good opportunity to add a toml2json tool, there's another pending PR that needs it, too.
Copy the toml2json tool from https://github.com/bazel-contrib/rules_python/pull/3557/changes and use it in this PR for parsing the pyproject.toml file.
- Copy tools/toml2json; move it to tools/private/toml2json
- Copy its tests.
- Use convert_uv_lock_to_json in python/private/pypi/uv_lock.bzl as inspiration for creating a pyproject_to_json bzl function. Notably, invoke the helper toml2json script using the utility functions it uses. These ensure the script is executed correctly/safely.
| ) | ||
| if default_python_version_file: | ||
|
|
||
| # Priority order: pyproject_toml > python_version_file > python_version_env > python_version |
There was a problem hiding this comment.
This logic needs to be partially reverted, as its changing behavior.
The ENV attribute has highest precedence (this allows overriding on the command line), followed by version file. I'm not sure if .python-version or pyproject.toml should have higher precedence; I'm think pyproject.toml should be higher, though.
| # Check if pyproject_toml was specified in defaults | ||
| # If so, register a toolchain for it |
There was a problem hiding this comment.
The diff is hiding a lot of the content -- is this new logic specific to pyproject? It would make sense that this occurs for whatever default is used, not just if pyproject is.
There was a problem hiding this comment.
I removed the pyproject-specific auto-registration. _create_toolchain_attr_structs now receives the already-computed default_python_version and auto-registers a toolchain for it if no explicit python.toolchain() exists for that version. This is generic and works for any default source (pyproject, version file, env, static string). WDYT?
| When specified, reads the `requires-python` field from pyproject.toml. | ||
| The version must be specified as `==X.Y.Z` (exact version with full semver). | ||
|
|
||
| :::{versionadded} 1.8.0 |
There was a problem hiding this comment.
Specify VERSION_NEXT_FEATURE
There was a problem hiding this comment.
These tests are OK, but they're integration tests. We only add integration tests when absolutely necessary because we have a limited number of jobs we can run (and we're near that limit already).
I think there are some more unittest-style things with pieces mocked out.
Otherwise, we'll just have to leave the code paths as untested 🤷
653998b to
e5ffe1e
Compare
i decided to start with ==X.Y.Z only as the simplest useful subset. the usecase i had in mind was mainly that tools like renovate can easily update it with exactly pinned versions. IMO Support for >=, X.Y, could be added in follow-up PRs. WDYT?
|
|
any update on this @rickeylev ? |
| continue | ||
|
|
||
| for tag in mod.tags.default: | ||
| pyproject_toml = getattr(tag, "pyproject_toml", None) |
There was a problem hiding this comment.
+1 to update the mocks. In there setting it to None is good enough. There is a function where that would be easy to do.
| ), | ||
| "config_settings": attr.label_list( | ||
| mandatory = True, | ||
| mandatory = False, |
| default = "//tools/private/toml2json:toml2json.py", | ||
| ), | ||
| # TODO: Set a proper default once interpreter bootstrapping is resolved. | ||
| # See https://github.com/bazel-contrib/rules_python/pull/3557 |
There was a problem hiding this comment.
What does this mean? Should we have some better default here?
4002e56 to
edaeacc
Compare
|
@aignas any update here? |
4b8c563 to
5482aec
Compare
There was a problem hiding this comment.
Sorry it took a while to get to this.
Leaving some comments as rules_python has moved ahead a little since you created this PR.
My thinking is that the following structure would make sense:
- Create a helper that reads
pyproject.tomland parses the contents. - Pass
pyproject.tomlto thepython.bzlto parse and detect the default here. This will take care of everything during runtime. Since theDEFAULT_PYTHON_VERSIONcan be retrieved, no need for the extra repository. Also this plays well with only the root module being able to set the python version. - Pass
pyproject.tomland get the version for thehub_builderand friends. Again, no need to create a separate repo. In the future we can use therequires_pythonto support dynamic selection/registration. This is especially useful since we have introduceduv.locksupport.
Just to reiterate, I think this feature is really good and a welcome addition to rules_python. Thank you for your work!
| result = module_ctx.execute([ | ||
| "python3", | ||
| str(toml2json), | ||
| str(pyproject_path), | ||
| ]) |
There was a problem hiding this comment.
We no longer need this, we can use a pure starlark library to parse toml into JSON.
|
|
||
| return bare_version | ||
|
|
||
| def read_pyproject_version(module_ctx, pyproject_label, logger = None): |
There was a problem hiding this comment.
Maybe it would be good to have read_pyproject function instead of this. Then it could return a struct with important values:
struct(
version = "3.13.9"
)| bare_version = requires_python[2:].strip() | ||
|
|
||
| # Validate X.Y.Z format | ||
| parts = bare_version.split(".") |
There was a problem hiding this comment.
please use the python/private/version.bzl to parse and normalize the version here. It will ensure that the code fails in the right way.
| The bare version string (e.g. "3.13.9"). | ||
| """ | ||
| if not requires_python.startswith("=="): | ||
| fail("requires-python must use '==' for exact version, got: {}".format(requires_python)) |
There was a problem hiding this comment.
We could in theory just take the string instead of the version. Then we can use something like:
evaluate("{python_version}{requires_python}".format(...))
That will save us from a refactor in the future. In my experience it is best to read files and return requires_python as is and then delegate the logic of this to some other file.
There was a problem hiding this comment.
just tbc, do you want me to:
- just structure read_pyproject to return the raw requires_python string and keep the current effective behavior (single exact version for now) e.g. leaving full specifier support as a future change?
- actually wire up evaluate(...) for arbitrary specifiers as part of this PR?
my read is the the first but happy to go further if you'd prefer
| from a python-build-standalone manifest file using | ||
| `python.override(add_runtime_manifest_urls = ..., runtime_manifest_sha = ...)`. | ||
| * (pip,python) Added `pyproject_toml` attribute to `pip.default()` and `python.defaults()` | ||
| to read Python version from pyproject.toml `requires-python` field (must be `==X.Y.Z` format). |
There was a problem hiding this comment.
todo: a reminder to switch to news/3514.added.md file for this snippet.
|
|
||
| Example: | ||
| ```python | ||
| load("@python_version_from_pyproject//:version.bzl", "PYTHON_VERSION") |
There was a problem hiding this comment.
not sure we need this, we could just set the default version in the extension.
Then the following should be using the default version:
compile_pip_requirements(
name = "requirements",
requirements_txt = "requirements.txt",
)
Or am I missing something?
5482aec to
1dd2d63
Compare
dont worry about the timing, thank you for your thorough review
thank you! |
c864558 to
af9a093
Compare
|
@aignas, i restructured per your suggestions.
on (checks fail with the same macOS jobs as on |
python.defaults(pyproject_toml=...)andpip.default(pyproject_toml=...).py_binary/py_test,pip.parse,compile_pip_requirements) picks it up with no explicitpython_versionin BUILD files:Changes:
pyproject_tomlattribute topython.defaults()andpip.default(), reading the version fromrequires-pythonpyproject.tomlwith the pure-Starlark@toml.bzl (toml.decode)(no host Python interpreter required)python.toolchain()call existspython_versionoptional inpip.parse()when usingpip.default(pyproject_toml=...)ENV > pyproject_toml > python_version_file > python_versionRequirements:
requires-python = "==X.Y.Z"format in pyproject.tomlThanks to @aignas and @rickeylev for the review and design guidance