| 1 |
"""Ensure every production and build dependency in pyproject.toml has an upper version bound. |
| 2 |
|
| 3 |
Unbounded requirements (e.g. `httpx>=0.25.0`) mean a future major release of a |
| 4 |
dependency can break users at install time without us noticing, so we require an |
| 5 |
explicit cap (e.g. `httpx>=0.25.0,<1`). |
| 6 |
""" |
| 7 |
|
| 8 |
from __future__ import annotations |
| 9 |
|
| 10 |
import sys |
| 11 |
from typing import Any |
| 12 |
from pathlib import Path |
| 13 |
|
| 14 |
from packaging.requirements import Requirement |
| 15 |
|
| 16 |
if sys.version_info >= (3, 11): |
| 17 |
import tomllib |
| 18 |
else: |
| 19 |
import tomli as tomllib |
| 20 |
|
| 21 |
# operators that constrain the maximum installable version |
| 22 |
UPPER_BOUND_OPERATORS = {"<", "<=", "==", "===", "~="} |
| 23 |
|
| 24 |
|
| 25 |
def has_upper_bound(requirement: Requirement) -> bool: |
| 26 |
return any(spec.operator in UPPER_BOUND_OPERATORS for spec in requirement.specifier) |
| 27 |
|
| 28 |
|
| 29 |
def main() -> None: |
| 30 |
pyproject_path = Path(__file__).parent.parent.parent / "pyproject.toml" |
| 31 |
with open(pyproject_path, "rb") as f: |
| 32 |
pyproject = tomllib.load(f) |
| 33 |
|
| 34 |
project: dict[str, Any] = pyproject["project"] |
| 35 |
build_system: dict[str, Any] = pyproject.get("build-system", {}) |
| 36 |
|
| 37 |
requirement_tables: dict[str, list[str]] = {"project.dependencies": project.get("dependencies", [])} |
| 38 |
optional_dependencies: dict[str, list[str]] = project.get("optional-dependencies", {}) |
| 39 |
for extra, requirements in optional_dependencies.items(): |
| 40 |
requirement_tables[f"project.optional-dependencies.{extra}"] = requirements |
| 41 |
requirement_tables["build-system.requires"] = build_system.get("requires", []) |
| 42 |
|
| 43 |
missing_caps: list[str] = [] |
| 44 |
for group, requirements in requirement_tables.items(): |
| 45 |
for raw in requirements: |
| 46 |
requirement = Requirement(raw) |
| 47 |
if not has_upper_bound(requirement): |
| 48 |
missing_caps.append(f" [{group}] {raw}") |
| 49 |
|
| 50 |
if missing_caps: |
| 51 |
print("The following dependencies are missing an upper version bound (e.g. `>=2, <3`):") |
| 52 |
print("\n".join(missing_caps)) |
| 53 |
sys.exit(1) |
| 54 |
|
| 55 |
|
| 56 |
if __name__ == "__main__": |
| 57 |
main() |