Skip to content

Commit

Permalink
Show non-critical Python discovery errors if no other interpreter is …
Browse files Browse the repository at this point in the history
…found

We show the first one, sort of arbitrarily but I think it matches user expectation
  • Loading branch information
zanieb committed Jan 17, 2025
1 parent 8111650 commit 5434cc0
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
11 changes: 11 additions & 0 deletions crates/uv-python/src/discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -971,9 +971,14 @@ pub(crate) fn find_python_installation(
) -> Result<FindPythonResult, Error> {
let installations = find_python_installations(request, environments, preference, cache);
let mut first_prerelease = None;
let mut first_error = None;
for result in installations {
// Iterate until the first critical error or happy result
if !result.as_ref().err().map_or(true, Error::is_critical) {
// Track the first non-critical error
if first_error.is_none() {
first_error = Some(result);
}
continue;
}

Expand Down Expand Up @@ -1026,6 +1031,12 @@ pub(crate) fn find_python_installation(
return Ok(Ok(installation));
}

// If we found a Python, but it was unusable for some reason, report that instead of saying we
// couldn't find any Python interpreters.
if let Some(result) = first_error {
return result;
}

Ok(Err(PythonNotFound {
request: request.clone(),
environment_preference: environments,
Expand Down
9 changes: 4 additions & 5 deletions crates/uv-python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ mod tests {

use crate::{
discovery::{
find_best_python_installation, find_python_installation, EnvironmentPreference,
self, find_best_python_installation, find_python_installation, EnvironmentPreference,
},
PythonPreference,
};
Expand Down Expand Up @@ -589,11 +589,10 @@ mod tests {
PythonPreference::default(),
&context.cache,
)
})?;
});
assert!(
matches!(result, Err(PythonNotFound { .. })),
// TODO(zanieb): We could improve the error handling to hint this to the user
"If only Python 2 is available, we should not find a python; got {result:?}"
matches!(result, Err(discovery::Error::Query(..))),
"If only Python 2 is available, we should report the interpreter query error; got {result:?}"
);

Ok(())
Expand Down
12 changes: 8 additions & 4 deletions crates/uv/tests/it/python_find.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ fn python_find() {
----- stdout -----
----- stderr -----
error: No interpreter found in virtual environments, managed installations, or search path
error: Failed to inspect Python interpreter from active virtual environment at `.venv/bin/python3`
Caused by: Python interpreter not found at `[VENV]/bin/python3`
"###);
}

Expand Down Expand Up @@ -124,7 +125,8 @@ fn python_find() {
----- stdout -----
----- stderr -----
error: No interpreter found for PyPy in virtual environments, managed installations, or search path
error: Failed to inspect Python interpreter from active virtual environment at `.venv/bin/python3`
Caused by: Python interpreter not found at `[VENV]/bin/python3`
"###);
}

Expand Down Expand Up @@ -536,7 +538,8 @@ fn python_find_unsupported_version() {
----- stdout -----
----- stderr -----
error: No interpreter found for Python 4.2 in virtual environments, managed installations, or search path
error: Failed to inspect Python interpreter from active virtual environment at `.venv/bin/python3`
Caused by: Python interpreter not found at `[VENV]/bin/python3`
"###);

// Request a low version with a range
Expand All @@ -546,7 +549,8 @@ fn python_find_unsupported_version() {
----- stdout -----
----- stderr -----
error: No interpreter found for Python <3.0 in virtual environments, managed installations, or search path
error: Failed to inspect Python interpreter from active virtual environment at `.venv/bin/python3`
Caused by: Python interpreter not found at `[VENV]/bin/python3`
"###);

// Request free-threaded Python on unsupported version
Expand Down

0 comments on commit 5434cc0

Please sign in to comment.