-
Notifications
You must be signed in to change notification settings - Fork 1k
Add pyproject direct dependency lint #19857
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
JacobSzwejbka
wants to merge
1
commit into
main
Choose a base branch
from
lint-pyproject-direct-url-deps
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.
+246
−4
Open
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,114 @@ | ||
| import importlib.util | ||
| import sys | ||
| import tempfile | ||
| import textwrap | ||
| import unittest | ||
| from pathlib import Path | ||
|
|
||
| REPO_ROOT = Path(__file__).resolve().parents[3] | ||
| SCRIPT_PATH = REPO_ROOT / "scripts" / "lint_pyproject_dependencies.py" | ||
| spec = importlib.util.spec_from_file_location( | ||
| "lint_pyproject_dependencies", SCRIPT_PATH | ||
| ) | ||
| assert spec is not None | ||
| assert spec.loader is not None | ||
| lint_pyproject_dependencies = importlib.util.module_from_spec(spec) | ||
| sys.modules[spec.name] = lint_pyproject_dependencies | ||
| spec.loader.exec_module(lint_pyproject_dependencies) | ||
| find_direct_reference_dependencies = ( | ||
| lint_pyproject_dependencies.find_direct_reference_dependencies | ||
| ) | ||
|
|
||
|
|
||
| class TestLintPyprojectDependencies(unittest.TestCase): | ||
| def write_pyproject(self, content: str) -> Path: | ||
| path = Path(self.tempdir.name) / "pyproject.toml" | ||
| path.write_text(textwrap.dedent(content)) | ||
| return path | ||
|
|
||
| def setUp(self) -> None: | ||
| self.tempdir = tempfile.TemporaryDirectory() | ||
|
|
||
| def tearDown(self) -> None: | ||
| self.tempdir.cleanup() | ||
|
|
||
| def test_allows_versioned_dependencies(self) -> None: | ||
| path = self.write_pyproject( | ||
| """ | ||
| [project] | ||
| dependencies = [ | ||
| "torch>=2.12.0a0", | ||
| ] | ||
|
|
||
| [project.optional-dependencies] | ||
| openvino = [ | ||
| "openvino>=2025.1.0,<2026.0.0; platform_system == 'Linux'", | ||
| ] | ||
| """ | ||
| ) | ||
|
|
||
| self.assertEqual(find_direct_reference_dependencies(path), []) | ||
|
|
||
| def test_rejects_project_direct_reference_dependency(self) -> None: | ||
| path = self.write_pyproject( | ||
| """ | ||
| [project] | ||
| dependencies = [ | ||
| "example @ git+https://github.com/example/example.git@abc123", | ||
| ] | ||
| """ | ||
| ) | ||
|
|
||
| violations = find_direct_reference_dependencies(path) | ||
| self.assertEqual(len(violations), 1) | ||
| self.assertEqual(violations[0].section, "project.dependencies") | ||
| self.assertEqual( | ||
| violations[0].dependency, | ||
| "example @ git+https://github.com/example/example.git@abc123", | ||
| ) | ||
|
|
||
| def test_rejects_direct_reference_dependency_with_spaced_extras(self) -> None: | ||
| path = self.write_pyproject( | ||
| """ | ||
| [project] | ||
| dependencies = [ | ||
| "example [dev] @ git+https://github.com/example/example.git@abc123", | ||
| ] | ||
| """ | ||
| ) | ||
|
|
||
| violations = find_direct_reference_dependencies(path) | ||
| self.assertEqual(len(violations), 1) | ||
| self.assertEqual(violations[0].section, "project.dependencies") | ||
| self.assertEqual( | ||
| violations[0].dependency, | ||
| "example [dev] @ git+https://github.com/example/example.git@abc123", | ||
| ) | ||
|
|
||
| def test_rejects_optional_direct_reference_dependency(self) -> None: | ||
| path = self.write_pyproject( | ||
| """ | ||
| [project] | ||
| dependencies = [] | ||
|
|
||
| [project.optional-dependencies] | ||
| cortex_m = [ | ||
| "cmsis_nn @ git+https://github.com/ARM-software/CMSIS-NN.git@abc123", | ||
| ] | ||
| """ | ||
| ) | ||
|
|
||
| violations = find_direct_reference_dependencies(path) | ||
| self.assertEqual(len(violations), 1) | ||
| self.assertEqual( | ||
| violations[0].section, | ||
| "project.optional-dependencies.cortex_m", | ||
| ) | ||
| self.assertEqual( | ||
| violations[0].dependency, | ||
| "cmsis_nn @ git+https://github.com/ARM-software/CMSIS-NN.git@abc123", | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
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,116 @@ | ||
| #!/usr/bin/env python3 | ||
| # Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| # All rights reserved. | ||
| # | ||
| # This source code is licensed under the BSD-style license found in the | ||
| # LICENSE file in the root directory of this source tree. | ||
|
|
||
| import argparse | ||
| import re | ||
| import sys | ||
| import tomllib | ||
| from dataclasses import dataclass | ||
| from pathlib import Path | ||
| from typing import Any | ||
|
|
||
|
|
||
| _DIRECT_REFERENCE = re.compile(r"^[A-Za-z0-9_.-]+(?:\s*\[[^\]]+\])?\s*@\s*\S+") | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class DirectReferenceDependency: | ||
| section: str | ||
| dependency: str | ||
| line: int | ||
|
|
||
|
|
||
| def _line_number_for_dependency(pyproject_text: str, dependency: str) -> int: | ||
| quoted_dependency = f'"{dependency}"' | ||
| for line_number, line in enumerate(pyproject_text.splitlines(), start=1): | ||
| if quoted_dependency in line or dependency in line: | ||
| return line_number | ||
| return 1 | ||
|
|
||
|
|
||
| def _record_if_direct_reference( | ||
| *, | ||
| pyproject_text: str, | ||
| section: str, | ||
| dependency: Any, | ||
| violations: list[DirectReferenceDependency], | ||
| ) -> None: | ||
| if not isinstance(dependency, str): | ||
| return | ||
| normalized_dependency = dependency.strip() | ||
| if _DIRECT_REFERENCE.match(normalized_dependency): | ||
| violations.append( | ||
| DirectReferenceDependency( | ||
| section=section, | ||
| dependency=dependency, | ||
| line=_line_number_for_dependency(pyproject_text, dependency), | ||
| ) | ||
| ) | ||
|
|
||
|
|
||
| def find_direct_reference_dependencies( | ||
| pyproject_path: Path, | ||
| ) -> list[DirectReferenceDependency]: | ||
| pyproject_text = pyproject_path.read_text() | ||
| pyproject = tomllib.loads(pyproject_text) | ||
| project = pyproject.get("project", {}) | ||
|
|
||
| violations: list[DirectReferenceDependency] = [] | ||
| for dependency in project.get("dependencies", []): | ||
| _record_if_direct_reference( | ||
| pyproject_text=pyproject_text, | ||
| section="project.dependencies", | ||
| dependency=dependency, | ||
| violations=violations, | ||
| ) | ||
|
|
||
| optional_dependencies = project.get("optional-dependencies", {}) | ||
| for extra, dependencies in optional_dependencies.items(): | ||
| for dependency in dependencies: | ||
| _record_if_direct_reference( | ||
| pyproject_text=pyproject_text, | ||
| section=f"project.optional-dependencies.{extra}", | ||
| dependency=dependency, | ||
| violations=violations, | ||
| ) | ||
|
|
||
| return violations | ||
|
|
||
|
|
||
| def main() -> int: | ||
| parser = argparse.ArgumentParser( | ||
| description=( | ||
| "Reject direct URL dependencies in project metadata. PyPI rejects " | ||
| "packages that publish dependencies like 'pkg @ git+https://...'." | ||
| ) | ||
| ) | ||
| parser.add_argument( | ||
| "pyproject", | ||
| type=Path, | ||
| nargs="?", | ||
| default=Path("pyproject.toml"), | ||
| help="Path to pyproject.toml", | ||
| ) | ||
| args = parser.parse_args() | ||
|
|
||
| violations = find_direct_reference_dependencies(args.pyproject) | ||
| for violation in violations: | ||
| print( | ||
| "::error " | ||
| f"file={args.pyproject},line={violation.line}," | ||
| "title=Direct URL dependency in pyproject.toml::" | ||
| f"{violation.section} contains '{violation.dependency}'. " | ||
| "PyPI rejects direct URL dependencies in published package " | ||
| "metadata; move it to a requirements file or install script.", | ||
| file=sys.stderr, | ||
| ) | ||
|
|
||
| return 1 if violations else 0 | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| sys.exit(main()) | ||
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.
is there a dry-run publish? If yes, it might be catch-all for these issues.