Skip to content

Commit 8665c06

Browse files
authored
Write requested python variant to pin file in uv init (#12870)
## Summary Closes #12855 This PR also fixed an issue, where `python_request` was matched against `PythonVersion::Default`. Previously, if `python_request` was `3.13t`, it would match the last branch, triggering a download of the Python version if it wasn't already installed. https://github.com/astral-sh/uv/blob/6b7f60c1eaa840c2e933a0fb056ab46f99c991a5/crates/uv/src/commands/project/init.rs#L421-L448 ```console ❯ uv init -v --managed-python --python 3.13t foo DEBUG uv 0.6.14 (a4cec56 2025-04-09) DEBUG Searching for Python 3.13t in managed installations DEBUG Searching for managed installations at `/Users/Jo/.local/share/uv/python` DEBUG Found managed installation `cpython-3.13.1-macos-aarch64-none` DEBUG Found `cpython-3.13.1-macos-aarch64-none` at `/Users/Jo/.local/share/uv/python/cpython-3.13.1-macos-aarch64-none/bin/python3.13` (managed installations) DEBUG Skipping interpreter at `/Users/Jo/.local/share/uv/python/cpython-3.13.1-macos-aarch64-none/bin/python3.13` from managed installations: does not satisfy request `3.13t` DEBUG Skipping incompatible managed installation `cpython-3.12.8-macos-aarch64-none` DEBUG Skipping incompatible managed installation `pypy-3.11.11-macos-aarch64-none` DEBUG Requested Python not found, checking for available download... DEBUG Acquired lock for `/Users/Jo/.local/share/uv/python` DEBUG Using request timeout of 30s INFO Fetching requested Python... Downloading cpython-3.13.3+freethreaded-macos-aarch64-none (49.9MiB) DEBUG Downloading https://github.com/astral-sh/python-build-standalone/releases/download/20250409/cpython-3.13.3%2B20250409-aarch64-apple-darwin-freethreaded%2Bpgo%2Blto-full.tar.zst to temporary location: /Users/Jo/.local/share/uv/python/.temp/.tmpfoOLkE DEBUG Extracting cpython-3.13.3%2B20250409-aarch64-apple-darwin-freethreaded%2Bpgo%2Blto-full.tar.zst Downloaded cpython-3.13.3+freethreaded-macos-aarch64-none DEBUG Moving /Users/Jo/.local/share/uv/python/.temp/.tmpfoOLkE/python/install to /Users/Jo/.local/share/uv/python/cpython-3.13.3+freethreaded-macos-aarch64-none DEBUG Released lock at `/Users/Jo/.local/share/uv/python/.lock` DEBUG Writing Python versions to `/private/tmp/foo/.python-version` Initialized project `foo` at `/private/tmp/foo` ❯ cat foo/.python-version 3.13 ``` After this PR, uv will not try to download it: ```console ❯ uv python uninstall 3.13t ❯ cargo run -- init -v --managed-python --python 3.13t bar DEBUG uv 0.6.14+15 (6b7f60c 2025-04-12) DEBUG Writing Python versions to `/private/tmp/bar/.python-version` Initialized project `bar` at `/private/tmp/bar` ❯ cat bar/.python_version 3.13t ```
1 parent 278a136 commit 8665c06

File tree

2 files changed

+28
-16
lines changed

2 files changed

+28
-16
lines changed

crates/uv/src/commands/project/init.rs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -370,21 +370,15 @@ async fn init_project(
370370
// This can be arbitrary, i.e., not a version — in which case we may need to resolve the
371371
// interpreter
372372
match python_request {
373-
PythonRequest::Version(VersionRequest::MajorMinor(
374-
major,
375-
minor,
376-
PythonVariant::Default,
377-
)) => {
373+
PythonRequest::Version(VersionRequest::MajorMinor(major, minor, variant)) => {
378374
let requires_python = RequiresPython::greater_than_equal_version(&Version::new([
379375
u64::from(major),
380376
u64::from(minor),
381377
]));
382378

383379
let python_request = if pin_python {
384380
Some(PythonRequest::Version(VersionRequest::MajorMinor(
385-
major,
386-
minor,
387-
PythonVariant::Default,
381+
major, minor, variant,
388382
)))
389383
} else {
390384
None
@@ -396,7 +390,7 @@ async fn init_project(
396390
major,
397391
minor,
398392
patch,
399-
PythonVariant::Default,
393+
variant,
400394
)) => {
401395
let requires_python = RequiresPython::greater_than_equal_version(&Version::new([
402396
u64::from(major),
@@ -406,19 +400,18 @@ async fn init_project(
406400

407401
let python_request = if pin_python {
408402
Some(PythonRequest::Version(VersionRequest::MajorMinorPatch(
409-
major,
410-
minor,
411-
patch,
412-
PythonVariant::Default,
403+
major, minor, patch, variant,
413404
)))
414405
} else {
415406
None
416407
};
417408

418409
(requires_python, python_request)
419410
}
420-
ref
421-
python_request @ PythonRequest::Version(VersionRequest::Range(ref specifiers, _)) => {
411+
ref python_request @ PythonRequest::Version(VersionRequest::Range(
412+
ref specifiers,
413+
variant,
414+
)) => {
422415
let requires_python = RequiresPython::from_specifiers(specifiers);
423416

424417
let python_request = if pin_python {
@@ -439,7 +432,7 @@ async fn init_project(
439432
Some(PythonRequest::Version(VersionRequest::MajorMinor(
440433
interpreter.python_major(),
441434
interpreter.python_minor(),
442-
PythonVariant::Default,
435+
variant,
443436
)))
444437
} else {
445438
None

crates/uv/tests/it/init.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3713,3 +3713,22 @@ fn init_without_description() -> Result<()> {
37133713

37143714
Ok(())
37153715
}
3716+
3717+
/// Run `uv init --python 3.13t` to create a pin to a freethreaded Python.
3718+
#[test]
3719+
fn init_python_variant() -> Result<()> {
3720+
let context = TestContext::new("3.13");
3721+
uv_snapshot!(context.filters(), context.init().arg("foo").arg("--python").arg("3.13t"), @r###"
3722+
success: true
3723+
exit_code: 0
3724+
----- stdout -----
3725+
3726+
----- stderr -----
3727+
Initialized project `foo` at `[TEMP_DIR]/foo`
3728+
"###);
3729+
3730+
let python_version = fs_err::read_to_string(context.temp_dir.join("foo/.python-version"))?;
3731+
assert_eq!(python_version, "3.13t\n");
3732+
3733+
Ok(())
3734+
}

0 commit comments

Comments
 (0)