Skip to content

Commit

Permalink
Rename requires-python validation method (#10133)
Browse files Browse the repository at this point in the history
## Summary

I want to differentiate this from `validate_script_requires_python`.
  • Loading branch information
charliermarsh authored Dec 24, 2024
1 parent 9279a12 commit 473e274
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 49 deletions.
84 changes: 41 additions & 43 deletions crates/uv/src/commands/project/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,11 @@ pub(crate) fn find_requires_python(workspace: &Workspace) -> Option<RequiresPyth
}

/// Returns an error if the [`Interpreter`] does not satisfy the [`Workspace`] `requires-python`.
///
/// If no [`Workspace`] is provided, the `requires-python` will be validated against the originating
/// source (e.g., a `.python-version` file or a `--python` command-line argument).
#[allow(clippy::result_large_err)]
pub(crate) fn validate_requires_python(
pub(crate) fn validate_project_requires_python(
interpreter: &Interpreter,
workspace: Option<&Workspace>,
requires_python: &RequiresPython,
Expand Down Expand Up @@ -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(),
))
}
}
}
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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))
Expand Down
4 changes: 2 additions & 2 deletions crates/uv/src/commands/project/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions crates/uv/src/commands/python/find.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions crates/uv/src/commands/venv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 473e274

Please sign in to comment.