-
-
Notifications
You must be signed in to change notification settings - Fork 681
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
Open
janwinkler1
wants to merge
16
commits into
bazel-contrib:main
Choose a base branch
from
janwinkler1:pyproject-toml-handling
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
8b286a5
feat: enable pyproject.toml as single source of truth for Python version
96eee53
add toml2json
rickeylev 9827e6b
replace version marker, remove allow_single_file=True
rickeylev 98f779b
add run_toml2json and attrs to pip.parse extension
rickeylev 59a6162
Fix precedence logic
janwinkler1 b02e1ac
Remove integration tests
janwinkler1 f0ec532
Use toml2json
janwinkler1 cc09715
Reformat
janwinkler1 c803fa2
Remove accidentally commited file
janwinkler1 3fe99fe
Replace pyproject specific logic
janwinkler1 d165cdd
Revert formatting diff
janwinkler1 c6420f8
Remove unused _run_toml2json and related attrs
janwinkler1 1cf9459
Add pyproject_toml to extension test mock and use direct attr access
janwinkler1 702e651
Remove evaluate_marers_srcs leftovers and fix syntax error from rebase
janwinkler1 edaeacc
Fix changelog
janwinkler1 c3a67e6
ci: retrigger
janwinkler1 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
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 |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| python/features.bzl export-subst | ||
| tools/publish/*.txt linguist-generated=true | ||
| requirements_lock.txt linguist-generated=true |
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 |
|---|---|---|
|
|
@@ -19,6 +19,7 @@ load("@pythons_hub//:versions.bzl", "MINOR_MAPPING") | |
| load("@rules_python_internal//:rules_python_config.bzl", rp_config = "config") | ||
| load("//python/private:auth.bzl", "AUTH_ATTRS") | ||
| load("//python/private:normalize_name.bzl", "normalize_name") | ||
| load("//python/private:pyproject_utils.bzl", "read_pyproject_version") | ||
| load("//python/private:repo_utils.bzl", "repo_utils") | ||
| load(":hub_builder.bzl", "hub_builder") | ||
| load(":hub_repository.bzl", "hub_repository", "whl_config_settings_to_json") | ||
|
|
@@ -205,12 +206,23 @@ def build_config( | |
| """ | ||
| defaults = { | ||
| "platforms": default_platforms(), | ||
| "python_version": None, | ||
| } | ||
| for mod in module_ctx.modules: | ||
| if not (mod.is_root or mod.name == "rules_python"): | ||
| continue | ||
|
|
||
| for tag in mod.tags.default: | ||
| pyproject_toml = tag.pyproject_toml | ||
| if pyproject_toml: | ||
| pyproject_version = read_pyproject_version( | ||
| module_ctx, | ||
| pyproject_toml, | ||
| logger = None, | ||
| ) | ||
| if pyproject_version: | ||
| defaults["python_version"] = pyproject_version | ||
|
|
||
| platform = tag.platform | ||
| if platform: | ||
| specific_config = defaults["platforms"].setdefault(platform, {}) | ||
|
|
@@ -246,6 +258,7 @@ def build_config( | |
| auth_patterns = defaults.get("auth_patterns", {}), | ||
| index_url = defaults.get("index_url", "https://pypi.org/simple").rstrip("/"), | ||
| netrc = defaults.get("netrc", None), | ||
| python_version = defaults.get("python_version", None), | ||
| platforms = { | ||
| name: _plat(**values) | ||
| for name, values in defaults["platforms"].items() | ||
|
|
@@ -345,6 +358,10 @@ You cannot use both the additive_build_content and additive_build_content_file a | |
|
|
||
| for mod in module_ctx.modules: | ||
| for pip_attr in mod.tags.parse: | ||
| python_version = pip_attr.python_version or config.python_version | ||
| if not python_version: | ||
| _fail("pip.parse() requires either python_version attribute or pip.default(pyproject_toml=...) to be set") | ||
|
|
||
| hub_name = pip_attr.hub_name | ||
| if hub_name not in pip_hub_map: | ||
| builder = hub_builder( | ||
|
|
@@ -381,6 +398,7 @@ You cannot use both the additive_build_content and additive_build_content_file a | |
| builder.pip_parse( | ||
| module_ctx, | ||
| pip_attr = pip_attr, | ||
| python_version = python_version, | ||
| ) | ||
|
|
||
| # Keeps track of all the hub's whl repos across the different versions. | ||
|
|
@@ -536,7 +554,7 @@ Either this or {attr}`env` `platform_machine` key should be specified. | |
| """, | ||
| ), | ||
| "config_settings": attr.label_list( | ||
| mandatory = True, | ||
| mandatory = False, | ||
| doc = """\ | ||
| The list of labels to `config_setting` targets that need to be matched for the platform to be | ||
| selected. | ||
|
|
@@ -618,6 +636,21 @@ If you are defining custom platforms in your project and don't want things to cl | |
| [isolation] feature. | ||
|
|
||
| [isolation]: https://bazel.build/rules/lib/globals/module#use_extension.isolate | ||
| """, | ||
| ), | ||
| "pyproject_toml": attr.label( | ||
| mandatory = False, | ||
| doc = """\ | ||
| Label pointing to pyproject.toml file to read the default Python version from. | ||
| When specified, reads the `requires-python` field from pyproject.toml and uses | ||
| it as the default python_version for all `pip.parse()` calls that don't | ||
| explicitly specify one. | ||
|
|
||
| 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} VERSION_NEXT_FEATURE | ||
| ::: | ||
| """, | ||
| ), | ||
| "whl_abi_tags": attr.string_list( | ||
|
|
@@ -778,14 +811,18 @@ find in case extra indexes are specified. | |
| default = True, | ||
| ), | ||
| "python_version": attr.string( | ||
| mandatory = True, | ||
| mandatory = False, | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. update python_version attr doc to indicate the other option add e.g. |
||
| doc = """ | ||
| The Python version the dependencies are targetting, in Major.Minor format | ||
| (e.g., "3.11") or patch level granularity (e.g. "3.11.1"). | ||
|
|
||
| If an interpreter isn't explicitly provided (using `python_interpreter` or | ||
| `python_interpreter_target`), then the version specified here must have | ||
| a corresponding `python.toolchain()` configured. | ||
|
|
||
| :::{seealso} | ||
| The {obj}`pyproject_toml` attribute for getting the version from a project file. | ||
| ::: | ||
| """, | ||
| ), | ||
| "simpleapi_skip": attr.string_list( | ||
|
|
||
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,80 @@ | ||
| """Repository rule to expose Python version from pyproject.toml.""" | ||
|
|
||
| _TOML2JSON = Label("//tools/private/toml2json:toml2json.py") | ||
|
|
||
| def _parse_requires_python(requires_python): | ||
| """Parse and validate the requires-python field.""" | ||
| if not requires_python.startswith("=="): | ||
| fail("requires-python must use '==' for exact version, got: {}".format(requires_python)) | ||
|
|
||
| bare_version = requires_python[2:].strip() | ||
| parts = bare_version.split(".") | ||
| if len(parts) != 3: | ||
| fail("requires-python must be in X.Y.Z format, got: {}".format(bare_version)) | ||
| for part in parts: | ||
| if not part.isdigit(): | ||
| fail("requires-python must be in X.Y.Z format, got: {}".format(bare_version)) | ||
|
|
||
| return bare_version | ||
|
|
||
| def _pyproject_version_repo_impl(rctx): | ||
| """Create a repository that exports PYTHON_VERSION from pyproject.toml.""" | ||
| pyproject_path = rctx.path(rctx.attr.pyproject_toml) | ||
| rctx.read(pyproject_path, watch = "yes") | ||
|
|
||
| toml2json = rctx.path(_TOML2JSON) | ||
| result = rctx.execute([ | ||
| "python3", | ||
| str(toml2json), | ||
| str(pyproject_path), | ||
| ]) | ||
|
|
||
| if result.return_code != 0: | ||
| fail("Failed to parse pyproject.toml: " + result.stderr) | ||
|
|
||
| data = json.decode(result.stdout) | ||
| requires_python = data.get("project", {}).get("requires-python") | ||
| if not requires_python: | ||
| fail("pyproject.toml must contain [project] requires-python field") | ||
|
|
||
| version = _parse_requires_python(requires_python) | ||
|
|
||
| rctx.file("version.bzl", """\ | ||
| \"\"\"Python version from pyproject.toml. | ||
|
|
||
| This file is automatically generated. Do not edit. | ||
| \"\"\" | ||
|
|
||
| PYTHON_VERSION = "{version}" | ||
| """.format(version = version)) | ||
|
|
||
| rctx.file("BUILD.bazel", """\ | ||
| # Automatically generated from pyproject.toml | ||
| exports_files(["version.bzl"]) | ||
| """) | ||
|
|
||
| pyproject_version_repo = repository_rule( | ||
| implementation = _pyproject_version_repo_impl, | ||
| attrs = { | ||
| "pyproject_toml": attr.label( | ||
| mandatory = True, | ||
| doc = "Label pointing to pyproject.toml file.", | ||
| ), | ||
| }, | ||
| doc = """Repository rule that reads Python version from pyproject.toml. | ||
|
|
||
| This rule creates a repository with a `version.bzl` file that exports | ||
| `PYTHON_VERSION` constant. | ||
|
|
||
| Example: | ||
| ```python | ||
| load("@python_version_from_pyproject//:version.bzl", "PYTHON_VERSION") | ||
|
|
||
| compile_pip_requirements( | ||
| name = "requirements", | ||
| python_version = PYTHON_VERSION, | ||
| requirements_txt = "requirements.txt", | ||
| ) | ||
| ``` | ||
| """, | ||
| ) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think a recent PR fixed this?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, in
mainit isTrue.