Use PythonEnvironment API in uv venv (#4029)

There's no reason to be reaching into the lower-level `find_interpreter`
manually here.
This commit is contained in:
Zanie Blue 2024-06-05 12:49:29 -04:00 committed by GitHub
parent b05a39c735
commit db3c36dbcc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 14 additions and 15 deletions

View file

@ -26,7 +26,9 @@ struct PythonEnvironmentShared {
}
impl PythonEnvironment {
/// Create a [`PythonEnvironment`] from a user request.
/// Find a [`PythonEnvironment`].
///
/// This is the standard interface for discovering a Python environment for use with uv.
pub fn find(
python: Option<&str>,
system: SystemPython,

View file

@ -20,9 +20,7 @@ use uv_configuration::{ConfigSettings, IndexStrategy, NoBinary, NoBuild, SetupPy
use uv_dispatch::BuildDispatch;
use uv_fs::Simplified;
use uv_git::GitResolver;
use uv_interpreter::{
find_default_interpreter, find_interpreter, InterpreterRequest, SourceSelector,
};
use uv_interpreter::{PythonEnvironment, SystemPython};
use uv_resolver::{ExcludeNewer, FlatIndex, InMemoryIndex, OptionsBuilder};
use uv_types::{BuildContext, BuildIsolation, HashStrategy, InFlight};
@ -121,16 +119,15 @@ async fn venv_impl(
cache: &Cache,
printer: Printer,
) -> miette::Result<ExitStatus> {
// Locate the Python interpreter.
let interpreter = if let Some(python) = python_request.as_ref() {
let system = uv_interpreter::SystemPython::Required;
let request = InterpreterRequest::parse(python);
let sources = SourceSelector::from_settings(system, preview);
find_interpreter(&request, system, &sources, cache)
// Locate the Python interpreter to use in the environment
// If a specific interpreter is requested, it is required to come from the system.
// Otherwise, we'll allow the interpeter from a virtual environment to be used.
let system = if python_request.is_some() {
SystemPython::Required
} else {
find_default_interpreter(preview, cache)
}
.into_diagnostic()?
SystemPython::Allowed
};
let interpreter = PythonEnvironment::find(python_request, system, preview, cache)
.into_diagnostic()?
.into_interpreter();