From f296ef08d667b9720709c332675ab2c39d24ccbf Mon Sep 17 00:00:00 2001 From: Zanie Blue Date: Mon, 10 Jun 2024 21:32:40 -0400 Subject: [PATCH] Refactor project interpreter request for `requires-python` specifiers (#4216) Refactor following #4214 to avoid parsing the specifiers again --- crates/uv/src/commands/project/mod.rs | 30 ++++++++++++++++++--------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/crates/uv/src/commands/project/mod.rs b/crates/uv/src/commands/project/mod.rs index 38b207847..da15801ee 100644 --- a/crates/uv/src/commands/project/mod.rs +++ b/crates/uv/src/commands/project/mod.rs @@ -21,7 +21,7 @@ use uv_git::GitResolver; use uv_installer::{SatisfiesResult, SitePackages}; use uv_requirements::{RequirementsSource, RequirementsSpecification}; use uv_resolver::{FlatIndex, InMemoryIndex, Options, RequiresPython}; -use uv_toolchain::{PythonEnvironment, SystemPython, Toolchain, ToolchainRequest}; +use uv_toolchain::{PythonEnvironment, SystemPython, Toolchain, ToolchainRequest, VersionRequest}; use uv_types::{BuildIsolation, HashStrategy, InFlight}; use uv_warnings::warn_user; @@ -125,15 +125,25 @@ pub(crate) fn init_environment( Err(e) => return Err(e.into()), } - // TODO(konsti): Extend `VersionRequest` to support `VersionSpecifiers`. - let requires_python_str = requires_python.map(ToString::to_string); - let interpreter = Toolchain::find( - python.or(requires_python_str.as_deref()), - // Otherwise we'll try to use the venv we just deleted. - SystemPython::Required, - preview, - cache, - )? + let interpreter = if let Some(request) = python.map(ToolchainRequest::parse).or(requires_python + .map(|specifiers| ToolchainRequest::Version(VersionRequest::Range(specifiers.clone())))) + { + Toolchain::find_requested( + &request, + // Otherwise we'll try to use the venv we just deleted. + SystemPython::Required, + preview, + cache, + ) + } else { + Toolchain::find( + None, + // Otherwise we'll try to use the venv we just deleted. + SystemPython::Required, + preview, + cache, + ) + }? .into_interpreter(); if let Some(requires_python) = requires_python {