-
-
Notifications
You must be signed in to change notification settings - Fork 700
feat: enable pyproject.toml as single source of truth for Python version #3514
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
+242
−17
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
627bfb3
feat: enable pyproject.toml as single source of truth for Python version
golithe a72364d
Address review: use toml.decode and version.bzl, drop repo rule and t…
golithe cbfc9cd
Add pyproject_toml to pip.parse; revert extension_metadata root deps
golithe 130e8c9
Update 3514.added.md
aignas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| (pip,python) Added `pyproject_toml` attribute to {obj}`pip.default`, {obj}`pip.parse` and {obj}`python.defaults` to read the default Python version from the `requires-python` field of `pyproject.toml`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| """Utilities for reading values from pyproject.toml.""" | ||
|
|
||
| load("@toml.bzl", "toml") | ||
| load(":version.bzl", "version") | ||
|
|
||
| def read_pyproject(module_ctx, pyproject): | ||
| """Read a pyproject.toml file and return the relevant fields. | ||
|
|
||
| The file is parsed with a pure-Starlark TOML decoder; no Python | ||
| interpreter is required. The raw `requires-python` value is returned | ||
| as-is so that callers can decide how to interpret it. | ||
|
|
||
| Args: | ||
| module_ctx: the module extension context (needs `path` and `read`). | ||
| pyproject: {type}`Label` pointing at the pyproject.toml file. | ||
|
|
||
| Returns: | ||
| {type}`struct` with the attributes: | ||
| * `requires_python`: {type}`str | None` the raw `requires-python` | ||
| value (e.g. `"==3.13.9"`), or `None` if it is not set. | ||
| """ | ||
| data = toml.decode(module_ctx.read(module_ctx.path(pyproject), watch = "yes")) | ||
| return struct( | ||
| requires_python = data.get("project", {}).get("requires-python"), | ||
| ) | ||
|
|
||
| def version_from_requires_python(requires_python): | ||
| """Derive a concrete Python version from a `requires-python` value. | ||
|
|
||
| Currently only an exact `==X.Y.Z` specifier is supported. The value is | ||
| validated and normalized via {obj}`//python/private:version.bzl` so that | ||
| malformed input fails in a consistent way. Broader specifier support | ||
| (e.g. `>=`, `X.Y`) can be layered on here in the future. | ||
|
|
||
| Args: | ||
| requires_python: {type}`str` the raw `requires-python` value. | ||
|
|
||
| Returns: | ||
| {type}`str` the normalized version string (e.g. `"3.13.9"`). | ||
| """ | ||
|
aignas marked this conversation as resolved.
|
||
| if not requires_python: | ||
| fail("`requires-python` must be specified") | ||
|
|
||
| if not requires_python.startswith("=="): | ||
| fail("`requires-python` must pin an exact version with `==`, got: {}".format(requires_python)) | ||
|
|
||
| bare_version = requires_python[len("=="):].strip() | ||
|
|
||
| # Parse strictly so malformed versions fail cleanly, then normalize. | ||
| version.parse(bare_version, strict = True) | ||
| return version.normalize(bare_version) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.