Repository navigation
Python: validate dependency bounds on version bumps and new packages #9801
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
Eduard van Valkenburg (eavanvalkenburg)
merged 2 commits into
microsoft:main
from
eavanvalkenburg:dependency-bounds-ci
Oct 29, 2026
+408
−2
Merged
Changes from all commits
Commits
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 |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| name: Python - Dependency Bounds | ||
|
|
||
| on: | ||
| pull_request: | ||
| branches: ["main"] | ||
| merge_group: | ||
| workflow_dispatch: | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
| env: | ||
| UV_CACHE_DIR: /tmp/.uv-cache | ||
|
|
||
| jobs: | ||
| dependency-bounds: | ||
| name: Dependency Bounds Validation | ||
| runs-on: ubuntu-latest | ||
| timeout-minutes: 60 | ||
| env: | ||
| UV_PYTHON: "3.13" | ||
| steps: | ||
| - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 | ||
| with: | ||
| fetch-depth: 0 | ||
| persist-credentials: false | ||
| - name: Test version-bump detection | ||
| working-directory: ./python | ||
| run: python3 -m unittest discover -s scripts/dependencies/tests -p test_detect_version_bumps.py | ||
| - name: Detect new packages and package version changes | ||
| id: versions | ||
| env: | ||
| EVENT_NAME: ${{ github.event_name }} | ||
| BASE_SHA: ${{ github.event.pull_request.base.sha || github.event.merge_group.base_sha }} | ||
| run: | | ||
| if [ "$EVENT_NAME" = "workflow_dispatch" ]; then | ||
| echo "required=true" >> "$GITHUB_OUTPUT" | ||
| else | ||
| python3 python/scripts/dependencies/detect_version_bumps.py \ | ||
| --base-ref "$BASE_SHA" --github-output "$GITHUB_OUTPUT" | ||
| fi | ||
| - name: Set up python and install the project | ||
| if: steps.versions.outputs.required == 'true' | ||
| uses: ./.github/actions/python-setup | ||
| with: | ||
| python-version: ${{ env.UV_PYTHON }} | ||
| os: ${{ runner.os }} | ||
| - name: Run lower and upper dependency tests and typing | ||
| if: steps.versions.outputs.required == 'true' | ||
| working-directory: ./python | ||
| run: uv run --locked --group tool-runtime poe validate-dependency-bounds-test --package "*" | ||
| - name: Upload dependency bounds report | ||
| if: always() && steps.versions.outputs.required == 'true' | ||
| uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 | ||
| with: | ||
| name: dependency-bounds-test-results | ||
| path: python/scripts/dependencies/dependency-bounds-test-results.json | ||
| if-no-files-found: warn |
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,92 @@ | ||
| # Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| """Detect new packages and release version changes without installing the Python workspace (Python 3.11+).""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import argparse | ||
| import fnmatch | ||
| import subprocess | ||
| import tomllib | ||
| from pathlib import Path | ||
|
|
||
|
|
||
| def _git(repo_root: Path, *args: str) -> str: | ||
| result = subprocess.run(["git", *args], cwd=repo_root, check=False, capture_output=True, text=True) | ||
| if result.returncode: | ||
| raise RuntimeError(f"Git comparison failed: {result.stderr.strip()}") | ||
| return result.stdout | ||
|
|
||
|
|
||
| def _version(content: str, manifest: str) -> str: | ||
| version = tomllib.loads(content).get("project", {}).get("version") | ||
| if not isinstance(version, str) or not version.strip(): | ||
| raise ValueError(f"{manifest} must declare a non-empty project.version") | ||
| return version | ||
|
|
||
|
|
||
| def _workspace_manifests(repo_root: Path, ref: str) -> set[str]: | ||
| workspace = tomllib.loads(_git(repo_root, "show", f"{ref}:python/pyproject.toml"))["tool"]["uv"]["workspace"] | ||
| manifests = {"python/pyproject.toml"} | ||
| tracked = _git(repo_root, "ls-tree", "-r", "--name-only", "-z", ref, "--", "python/packages") | ||
| for manifest in filter(None, tracked.split("\0")): | ||
| relative = Path(manifest).relative_to("python") | ||
| if len(relative.parts) != 3 or relative.name != "pyproject.toml": | ||
| continue | ||
| project_path = relative.parent.as_posix() | ||
| if not any(fnmatch.fnmatchcase(project_path, pattern) for pattern in workspace["members"]): | ||
| continue | ||
| if any(fnmatch.fnmatchcase(project_path, pattern) for pattern in workspace.get("exclude", [])): | ||
| continue | ||
| manifests.add(manifest) | ||
| return manifests | ||
|
|
||
|
|
||
| def changed_package_versions(repo_root: Path, base_ref: str) -> list[str]: | ||
| """Return newly included workspace manifests and those with changed versions.""" | ||
| if not base_ref.strip(): | ||
| raise ValueError("An explicit PR or merge-group base commit is required") | ||
| base_sha = _git(repo_root, "rev-parse", "--verify", "--end-of-options", f"{base_ref}^{{commit}}").strip() | ||
| base_manifests = _workspace_manifests(repo_root, base_sha) | ||
| head_manifests = _workspace_manifests(repo_root, "HEAD") | ||
| newly_included = head_manifests - base_manifests | ||
| changed = _git( | ||
| repo_root, | ||
| "diff", | ||
| "--name-only", | ||
| "-z", | ||
| "--no-renames", | ||
| "--diff-filter=AM", | ||
| base_sha, | ||
| "HEAD", | ||
| "--", | ||
| "python/pyproject.toml", | ||
| "python/packages/*/pyproject.toml", | ||
| ) | ||
| bumps: list[str] = [] | ||
| candidates = (set(changed.split("\0")) & head_manifests) | newly_included | ||
| for manifest in sorted(candidates): | ||
| current = _version(_git(repo_root, "show", f"HEAD:{manifest}"), manifest) | ||
| # A downgrade also runs validation; it must not provide a way to bypass the gate. | ||
| if manifest in newly_included or current != _version(_git(repo_root, "show", f"{base_sha}:{manifest}"), manifest): | ||
| bumps.append(manifest) | ||
| return sorted(bumps) | ||
|
|
||
|
|
||
| def main() -> None: | ||
| """Write the gate decision as a GitHub Actions step output.""" | ||
| parser = argparse.ArgumentParser(description=__doc__) | ||
| parser.add_argument("--base-ref", required=True) | ||
| parser.add_argument("--github-output", type=Path, required=True) | ||
| args = parser.parse_args() | ||
| bumps = changed_package_versions(Path(__file__).resolve().parents[3], args.base_ref) | ||
| with args.github_output.open("a") as output: | ||
| output.write(f"required={'true' if bumps else 'false'}\n") | ||
| if bumps: | ||
| print("New packages or package version changes require dependency-bounds tests:\n" + "\n".join(bumps)) | ||
| else: | ||
| print("No new workspace packages or package version changes; dependency-bounds tests are not required.") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
Oops, something went wrong.
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.