-
Notifications
You must be signed in to change notification settings - Fork 53
feat: auto-cap setuptools when setup.py uses removed APIs #1264
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import ast | ||
| import copy | ||
| import logging | ||
| import os | ||
|
|
@@ -102,9 +103,74 @@ def default_get_build_system_dependencies( | |
| """Get build system requirements | ||
|
|
||
| Defaults to ``[build-system] requires`` from ``pyproject.toml``. | ||
|
|
||
| When ``setup.py`` uses APIs removed in newer setuptools versions, | ||
| a version cap is appended automatically: | ||
|
|
||
| - ``setuptools<81`` when ``setup.py`` passes ``dry_run=`` keyword | ||
| arguments (removed in setuptools 81) | ||
| - ``setuptools<82`` when ``setup.py`` imports ``pkg_resources`` | ||
| (removed in setuptools 82) | ||
| """ | ||
| pyproject_toml = get_pyproject_contents(build_dir) | ||
| return typing.cast(list[str], get_build_backend(pyproject_toml)["requires"]) | ||
| requires = list( | ||
| typing.cast(list[str], get_build_backend(pyproject_toml)["requires"]) | ||
| ) | ||
| constraint = _get_setuptools_constraint(sdist_root_dir) | ||
| if constraint: | ||
| logger.info( | ||
| "%s: auto-adding %s (setup.py uses removed APIs)", req.name, constraint | ||
| ) | ||
| requires.append(constraint) | ||
| return requires | ||
|
|
||
|
|
||
| def _get_setuptools_constraint(sdist_root_dir: pathlib.Path) -> str | None: | ||
|
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. The function should look into |
||
| """Return a setuptools version cap if setup.py uses removed APIs. | ||
|
|
||
| - setuptools 81 removed ``distutils.spawn(dry_run=...)`` and | ||
| ``distutils.dir_util.remove_tree(dry_run=...)`` | ||
| - setuptools 82 removed ``pkg_resources`` entirely | ||
|
|
||
| Parses the AST to avoid false positives from string matches in | ||
| comments or string literals. | ||
|
|
||
| Returns ``"setuptools<81"``, ``"setuptools<82"``, or ``None``. | ||
| The tighter constraint wins when both apply. | ||
| """ | ||
| setup_py = sdist_root_dir / "setup.py" | ||
| if not setup_py.is_file(): | ||
| return None | ||
| try: | ||
| source = setup_py.read_text(encoding="utf-8", errors="replace") | ||
| tree = ast.parse(source, filename=str(setup_py)) | ||
| except (OSError, SyntaxError): | ||
| return None | ||
|
|
||
| findings: set[str] = set() | ||
| for node in ast.walk(tree): | ||
| if isinstance(node, ast.Import): | ||
| for alias in node.names: | ||
| if alias.name == "pkg_resources" or alias.name.startswith( | ||
| "pkg_resources." | ||
| ): | ||
| findings.add("pkg_resources") | ||
| elif isinstance(node, ast.ImportFrom): | ||
| if node.module is not None and ( | ||
| node.module == "pkg_resources" | ||
| or node.module.startswith("pkg_resources.") | ||
| ): | ||
| findings.add("pkg_resources") | ||
| elif isinstance(node, ast.Call): | ||
| for kw in node.keywords: | ||
| if kw.arg == "dry_run": | ||
| findings.add("dry_run") | ||
|
Comment on lines
+164
to
+167
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. 🎯 Functional Correctness | 🟠 Major | 🏗️ Heavy lift Restrict Any local call such as 🤖 Prompt for AI Agents |
||
|
|
||
| if "dry_run" in findings: | ||
| return "setuptools<81" | ||
| if "pkg_resources" in findings: | ||
| return "setuptools<82" | ||
| return None | ||
|
|
||
|
|
||
| def get_build_backend_dependencies( | ||
|
|
||
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.
What if there is already a setuptools requirement in the list? Shouldn't we modify that requirement instead of just adding another one?
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.
Yes, the code has to carefully merge constraints. If the upstream project or our downstream project overrides set a lower ceiling, then the new code must not raise the ceiling.