From 473e27427555820108a037801b5656e3df7a3df4 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Mon, 23 Dec 2024 19:45:21 -0500 Subject: [PATCH] Rename `requires-python` validation method (#10133) ## Summary I want to differentiate this from `validate_script_requires_python`. --- crates/uv/src/commands/project/mod.rs | 84 +++++++++++++-------------- crates/uv/src/commands/project/run.rs | 4 +- crates/uv/src/commands/python/find.rs | 4 +- crates/uv/src/commands/venv.rs | 4 +- 4 files changed, 47 insertions(+), 49 deletions(-) diff --git a/crates/uv/src/commands/project/mod.rs b/crates/uv/src/commands/project/mod.rs index e9243651652a..3c218fd3322d 100644 --- a/crates/uv/src/commands/project/mod.rs +++ b/crates/uv/src/commands/project/mod.rs @@ -315,8 +315,11 @@ pub(crate) fn find_requires_python(workspace: &Workspace) -> Option, requires_python: &RequiresPython, @@ -399,41 +402,31 @@ pub(crate) fn validate_requires_python( #[allow(clippy::result_large_err)] fn validate_script_requires_python( interpreter: &Interpreter, - workspace: Option<&Workspace>, requires_python: &RequiresPython, - requires_python_source: &RequiresPythonSource, - request_source: &PythonRequestSource, + source: &PythonRequestSource, ) -> Result<(), ProjectError> { - match requires_python_source { - RequiresPythonSource::Project => { - validate_requires_python(interpreter, workspace, requires_python, request_source) + if requires_python.contains(interpreter.python_version()) { + return Ok(()); + } + match source { + PythonRequestSource::UserRequest => { + Err(ProjectError::RequestedPythonScriptIncompatibility( + interpreter.python_version().clone(), + requires_python.clone(), + )) } - RequiresPythonSource::Script => { - if requires_python.contains(interpreter.python_version()) { - return Ok(()); - } - - match request_source { - PythonRequestSource::UserRequest => { - Err(ProjectError::RequestedPythonScriptIncompatibility( - interpreter.python_version().clone(), - requires_python.clone(), - )) - } - PythonRequestSource::DotPythonVersion(file) => { - Err(ProjectError::DotPythonVersionScriptIncompatibility( - file.file_name().to_string(), - interpreter.python_version().clone(), - requires_python.clone(), - )) - } - PythonRequestSource::RequiresPython => { - Err(ProjectError::RequiresPythonScriptIncompatibility( - interpreter.python_version().clone(), - requires_python.clone(), - )) - } - } + PythonRequestSource::DotPythonVersion(file) => { + Err(ProjectError::DotPythonVersionScriptIncompatibility( + file.file_name().to_string(), + interpreter.python_version().clone(), + requires_python.clone(), + )) + } + PythonRequestSource::RequiresPython => { + Err(ProjectError::RequiresPythonScriptIncompatibility( + interpreter.python_version().clone(), + requires_python.clone(), + )) } } } @@ -487,16 +480,16 @@ impl ScriptInterpreter { .await? .into_interpreter(); - if let Some((requires_python, requires_python_source)) = requires_python { - if let Err(err) = validate_script_requires_python( - &interpreter, - workspace, - &requires_python, - &requires_python_source, - &source, - ) { - warn_user!("{err}"); + if let Err(err) = match requires_python { + Some((requires_python, RequiresPythonSource::Project)) => { + validate_project_requires_python(&interpreter, workspace, &requires_python, &source) + } + Some((requires_python, RequiresPythonSource::Script)) => { + validate_script_requires_python(&interpreter, &requires_python, &source) } + None => Ok(()), + } { + warn_user!("{err}"); } Ok(Self(interpreter)) @@ -653,7 +646,12 @@ impl ProjectInterpreter { } if let Some(requires_python) = requires_python.as_ref() { - validate_requires_python(&interpreter, Some(workspace), requires_python, &source)?; + validate_project_requires_python( + &interpreter, + Some(workspace), + requires_python, + &source, + )?; } Ok(Self::Interpreter(interpreter)) diff --git a/crates/uv/src/commands/project/run.rs b/crates/uv/src/commands/project/run.rs index 4c68751c476f..fccbe068afc8 100644 --- a/crates/uv/src/commands/project/run.rs +++ b/crates/uv/src/commands/project/run.rs @@ -45,7 +45,7 @@ use crate::commands::pip::operations::Modifications; use crate::commands::project::environment::CachedEnvironment; use crate::commands::project::lock::LockMode; use crate::commands::project::{ - default_dependency_groups, validate_requires_python, DependencyGroupsTarget, + default_dependency_groups, validate_project_requires_python, DependencyGroupsTarget, EnvironmentSpecification, ProjectError, ScriptInterpreter, WorkspacePython, }; use crate::commands::reporters::PythonDownloadReporter; @@ -538,7 +538,7 @@ pub(crate) async fn run( .into_interpreter(); if let Some(requires_python) = requires_python.as_ref() { - validate_requires_python( + validate_project_requires_python( &interpreter, Some(project.workspace()), requires_python, diff --git a/crates/uv/src/commands/python/find.rs b/crates/uv/src/commands/python/find.rs index 6844c0fc7247..327c703d76cf 100644 --- a/crates/uv/src/commands/python/find.rs +++ b/crates/uv/src/commands/python/find.rs @@ -9,7 +9,7 @@ use uv_warnings::{warn_user, warn_user_once}; use uv_workspace::{DiscoveryOptions, VirtualProject, WorkspaceError}; use crate::commands::{ - project::{validate_requires_python, WorkspacePython}, + project::{validate_project_requires_python, WorkspacePython}, ExitStatus, }; @@ -65,7 +65,7 @@ pub(crate) async fn find( // Warn if the discovered Python version is incompatible with the current workspace if let Some(requires_python) = requires_python { - match validate_requires_python( + match validate_project_requires_python( python.interpreter(), project.as_ref().map(VirtualProject::workspace), &requires_python, diff --git a/crates/uv/src/commands/venv.rs b/crates/uv/src/commands/venv.rs index e54f7bccfd70..71ff32b6260f 100644 --- a/crates/uv/src/commands/venv.rs +++ b/crates/uv/src/commands/venv.rs @@ -32,7 +32,7 @@ use uv_workspace::{DiscoveryOptions, VirtualProject, WorkspaceError}; use crate::commands::pip::loggers::{DefaultInstallLogger, InstallLogger}; use crate::commands::pip::operations::{report_interpreter, Changelog}; -use crate::commands::project::{validate_requires_python, WorkspacePython}; +use crate::commands::project::{validate_project_requires_python, WorkspacePython}; use crate::commands::reporters::PythonDownloadReporter; use crate::commands::ExitStatus; use crate::printer::Printer; @@ -231,7 +231,7 @@ async fn venv_impl( // Check if the discovered Python version is incompatible with the current workspace if let Some(requires_python) = requires_python { - match validate_requires_python( + match validate_project_requires_python( &interpreter, project.as_ref().map(VirtualProject::workspace), &requires_python,