uv/scripts/scenarios/generate.py
Charlie Marsh 5a95f50619
Add support for PyTorch-style local version semantics (#2430)
## Summary

This PR adds limited support for PEP 440-compatible local version
testing. Our behavior is _not_ comprehensively in-line with the spec.
However, it does fix by _far_ the biggest practical limitation, and
resolves all the issues that've been raised on uv related to local
versions without introducing much complexity into the resolver, so it
feels like a good tradeoff for me.

I'll summarize the change here, but for more context, see [Andrew's
write-up](https://github.com/astral-sh/uv/issues/1855#issuecomment-1967024866)
in the linked issue.

Local version identifiers are really tricky because of asymmetry.
`==1.2.3` should allow `1.2.3+foo`, but `==1.2.3+foo` should not allow
`1.2.3`. It's very hard to map them to PubGrub, because PubGrub doesn't
think of things in terms of individual specifiers (unlike the PEP 440
spec) -- it only thinks in terms of ranges.

Right now, resolving PyTorch and friends fails, because...

- The user provides requirements like `torch==2.0.0+cu118` and
`torchvision==0.15.1+cu118`.
- We then match those exact versions.
- We then look at the requirements of `torchvision==0.15.1+cu118`, which
includes `torch==2.0.0`.
- Under PEP 440, this is fine, because `torch @ 2.0.0+cu118` should be
compatible with `torch==2.0.0`.
- In our model, though, it's not, because these are different versions.
If we change our comparison logic in various places to allow this, we
risk breaking some fundamental assumptions of PubGrub around version
continuity.
- Thus, we fail to resolve, because we can't accept both `torch @ 2.0.0`
and `torch @ 2.0.0+cu118`.

As compared to the solutions we explored in
https://github.com/astral-sh/uv/issues/1855#issuecomment-1967024866, at
a high level, this approach differs in that we lie about the
_dependencies_ of packages that rely on our local-version-using package,
rather than lying about the versions that exist, or the version we're
returning, etc.

In short:

- When users specify local versions upfront, we keep track of them. So,
above, we'd take note of `torch` and `torchvision`.
- When we convert the dependencies of a package to PubGrub ranges, we
check if the requirement matches `torch` or `torchvision`. If it's
an`==`, we check if it matches (in the above example) for
`torch==2.0.0`. If so, we _change_ the requirement to
`torch==2.0.0+cu118`. (If it's `==` some other version, we return an
incompatibility.)

In other words, we selectively override the declared dependencies by
making them _more specific_ if a compatible local version was specified
upfront.

The net effect here is that the motivating PyTorch resolutions all work.
And, in general, transitive local versions work as expected.

The thing that still _doesn't_ work is: imagine if there were _only_
local versions of `torch` available. Like, `torch @ 2.0.0` didn't exist,
but `torch @ 2.0.0+cpu` did, and `torch @ 2.0.0+gpu` did, and so on.
`pip install torch==2.0.0` would arbitrarily choose one one `2.0.0+cpu`
or `2.0.0+gpu`, and that's correct as per PEP 440 (local version
segments should be completely ignored on `torch==2.0.0`). However, uv
would fail to identify a compatible version. I'd _probably_ prefer to
fix this, although candidly I think our behavior is _ok_ in practice,
and it's never been reported as an issue.

Closes https://github.com/astral-sh/uv/issues/1855.

Closes https://github.com/astral-sh/uv/issues/2080.

Closes https://github.com/astral-sh/uv/issues/2328.
2024-03-16 10:24:50 -04:00

271 lines
7.9 KiB
Python
Executable file

#!/usr/bin/env python3
"""
Generates and updates snapshot test cases from packse scenarios.
Important:
This script is the backend called by `./scripts/scenarios/sync.sh`, consider using that
if not developing scenarios.
Requirements:
$ uv pip install -r scripts/scenarios/requirements.txt
Uses `git`, `rustfmt`, and `cargo insta test` requirements from the project.
Usage:
Regenerate the scenario test files using the given scenarios:
$ ./scripts/scenarios/generate.py <path>
Scenarios can be developed locally with the following workflow:
Serve scenarios on a local index using packse
$ packse serve --no-hash <path to scenarios>
Override the uv package index and update the tests
$ UV_INDEX_URL="http://localhost:3141" ./scripts/scenarios/generate.py <path to scenarios>
If an editable version of packse is installed, this script will use its bundled scenarios by default.
Use
"""
import argparse
import importlib.metadata
import logging
import os
import subprocess
import sys
import textwrap
from pathlib import Path
TOOL_ROOT = Path(__file__).parent
TEMPLATES = TOOL_ROOT / "templates"
INSTALL_TEMPLATE = TEMPLATES / "install.mustache"
COMPILE_TEMPLATE = TEMPLATES / "compile.mustache"
PACKSE = TOOL_ROOT / "packse-scenarios"
REQUIREMENTS = TOOL_ROOT / "requirements.txt"
PROJECT_ROOT = TOOL_ROOT.parent.parent
TESTS = PROJECT_ROOT / "crates" / "uv" / "tests"
INSTALL_TESTS = TESTS / "pip_install_scenarios.rs"
COMPILE_TESTS = TESTS / "pip_compile_scenarios.rs"
try:
import packse
import packse.inspect
except ImportError:
print(
f"missing requirement `packse`: install the requirements at {REQUIREMENTS.relative_to(PROJECT_ROOT)}",
file=sys.stderr,
)
exit(1)
try:
import chevron_blue
except ImportError:
print(
f"missing requirement `chevron-blue`: install the requirements at {REQUIREMENTS.relative_to(PROJECT_ROOT)}",
file=sys.stderr,
)
exit(1)
def main(scenarios: list[Path], snapshot_update: bool = True):
# Fetch packse version
packse_version = importlib.metadata.version("packse")
debug = logging.getLogger().getEffectiveLevel() <= logging.DEBUG
if not scenarios:
if packse_version == "0.0.0":
path = packse.__development_base_path__ / "scenarios"
if path.exists():
logging.info(
"Detected development version of packse, using scenarios from %s",
path,
)
scenarios = path.glob("*.json")
else:
logging.error(
"No scenarios provided. Found development version of packse but is missing scenarios. Is it installed as an editable?"
)
sys.exit(1)
else:
logging.error("No scenarios provided, nothing to do.")
return
targets = []
for target in scenarios:
if target.is_dir():
targets.extend(target.glob("*.json"))
else:
targets.append(target)
logging.info("Loading scenario metadata...")
data = packse.inspect.inspect(
targets=targets,
no_hash=True,
)
data["scenarios"] = [
scenario
for scenario in data["scenarios"]
# Drop the example scenario
if scenario["name"] != "example"
]
# Wrap the description onto multiple lines
for scenario in data["scenarios"]:
scenario["description_lines"] = textwrap.wrap(scenario["description"], width=80)
# Wrap the expected explanation onto multiple lines
for scenario in data["scenarios"]:
expected = scenario["expected"]
expected["explanation_lines"] = (
textwrap.wrap(expected["explanation"], width=80)
if expected["explanation"]
else []
)
# We don't yet support local versions that aren't expressed as direct dependencies.
for scenario in data["scenarios"]:
expected = scenario["expected"]
if scenario["name"] in (
"local-less-than-or-equal",
"local-simple",
"local-transitive-confounding",
"local-used-without-sdist",
):
expected["satisfiable"] = False
expected[
"explanation"
] = "We do not have correct behavior for local version identifiers yet"
# Split scenarios into `install` and `compile` cases
install_scenarios = []
compile_scenarios = []
for scenario in data["scenarios"]:
if (scenario["resolver_options"] or {}).get("python") is not None:
compile_scenarios.append(scenario)
else:
install_scenarios.append(scenario)
for template, tests, scenarios in [
(INSTALL_TEMPLATE, INSTALL_TESTS, install_scenarios),
(COMPILE_TEMPLATE, COMPILE_TESTS, compile_scenarios),
]:
data = {"scenarios": scenarios}
ref = "HEAD" if packse_version == "0.0.0" else packse_version
# Add generated metadata
data[
"generated_from"
] = f"https://github.com/zanieb/packse/tree/{ref}/scenarios"
data["generated_with"] = "./scripts/scenarios/sync.sh"
data[
"vendor_links"
] = f"https://raw.githubusercontent.com/zanieb/packse/{ref}/vendor/links.html"
data["index_url"] = f"https://astral-sh.github.io/packse/{ref}/simple-html/"
# Render the template
logging.info(f"Rendering template {template.name}")
output = chevron_blue.render(
template=template.read_text(), data=data, no_escape=True, warn=True
)
# Update the test files
logging.info(
f"Updating test file at `{tests.relative_to(PROJECT_ROOT)}`...",
)
with open(tests, "wt") as test_file:
test_file.write(output)
# Format
logging.info(
"Formatting test file...",
)
subprocess.check_call(
["rustfmt", str(tests)],
stderr=subprocess.STDOUT,
stdout=sys.stderr if debug else subprocess.DEVNULL,
)
# Update snapshots
if snapshot_update:
logging.info("Updating snapshots...")
env = os.environ.copy()
env["UV_TEST_PYTHON_PATH"] = str(PROJECT_ROOT / "bin")
subprocess.call(
[
"cargo",
"insta",
"test",
"--features",
"pypi,python",
"--accept",
"--test-runner",
"nextest",
"--test",
tests.with_suffix("").name,
],
cwd=PROJECT_ROOT,
stderr=subprocess.STDOUT,
stdout=sys.stderr if debug else subprocess.DEVNULL,
env=env,
)
else:
logging.info("Skipping snapshot update")
logging.info("Done!")
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Generates and updates snapshot test cases from packse scenarios.",
)
parser.add_argument(
"scenarios",
type=Path,
nargs="*",
help="The scenario files to use",
)
parser.add_argument(
"-v",
"--verbose",
action="store_true",
help="Enable debug logging",
)
parser.add_argument(
"-q",
"--quiet",
action="store_true",
help="Disable logging",
)
parser.add_argument(
"--no-snapshot-update",
action="store_true",
help="Disable automatic snapshot updates",
)
args = parser.parse_args()
if args.quiet:
log_level = logging.CRITICAL
elif args.verbose:
log_level = logging.DEBUG
else:
log_level = logging.INFO
logging.basicConfig(level=log_level, format="%(message)s")
main(args.scenarios, snapshot_update=not args.no_snapshot_update)