From 8cf9822e624994da5e3ec358bfe3841c14a6b225 Mon Sep 17 00:00:00 2001 From: Zanie Blue Date: Wed, 18 Sep 2024 13:52:30 -0500 Subject: [PATCH] Rename `PythonRequest::Any` -> `PythonRequest::Default` --- crates/uv-python/src/discovery.rs | 27 +++++---- crates/uv-python/src/downloads.rs | 2 +- crates/uv-python/src/environment.rs | 2 +- crates/uv-python/src/installation.rs | 2 +- crates/uv-python/src/lib.rs | 68 +++++++++++----------- crates/uv-python/src/managed.rs | 2 +- crates/uv/src/commands/project/add.rs | 2 +- crates/uv/src/commands/python/install.rs | 8 +-- crates/uv/src/commands/python/list.rs | 2 +- crates/uv/src/commands/python/uninstall.rs | 6 +- 10 files changed, 62 insertions(+), 59 deletions(-) diff --git a/crates/uv-python/src/discovery.rs b/crates/uv-python/src/discovery.rs index 9ad49788cf12..1dce821ad665 100644 --- a/crates/uv-python/src/discovery.rs +++ b/crates/uv-python/src/discovery.rs @@ -36,9 +36,12 @@ use crate::{Interpreter, PythonVersion}; /// See [`PythonRequest::from_str`]. #[derive(Debug, Clone, PartialEq, Eq, Default)] pub enum PythonRequest { - /// Use any discovered Python installation + /// Use an appropriate default Python installation + /// + /// This may skip some Python installations, such as pre-release versions or alternative + /// implementations. #[default] - Any, + Default, /// A Python version without an implementation name e.g. `3.10` or `>=3.12,<3.13` Version(VersionRequest), /// A path to a directory containing a Python installation, e.g. `.venv` @@ -776,7 +779,7 @@ pub fn find_python_installations<'a>( )))) } } - PythonRequest::Any => Box::new({ + PythonRequest::Default => Box::new({ debug!("Searching for Python interpreter in {preference}"); python_interpreters(None, None, environments, preference, cache).map(|result| { result @@ -976,7 +979,7 @@ pub fn find_best_python_installation( // If a Python version was requested but cannot be fulfilled, just take any version debug!("Looking for Python installation with any version"); - let request = PythonRequest::Any; + let request = PythonRequest::Default; Ok( find_python_installation(&request, environments, preference, cache)?.map_err(|err| { // Use a more general error in this case since we looked for multiple versions @@ -1142,7 +1145,7 @@ impl PythonRequest { pub fn parse(value: &str) -> Self { // e.g. `any` if value.eq_ignore_ascii_case("any") { - return Self::Any; + return Self::Default; } // e.g. `3.12.1`, `312`, or `>=3.12` @@ -1246,7 +1249,7 @@ impl PythonRequest { } match self { - PythonRequest::Any => true, + PythonRequest::Default => true, PythonRequest::Version(version_request) => { version_request.matches_interpreter(interpreter) } @@ -1330,7 +1333,7 @@ impl PythonRequest { pub(crate) fn allows_prereleases(&self) -> bool { match self { - Self::Any => false, + Self::Default => false, Self::Version(version) => version.allows_prereleases(), Self::Directory(_) | Self::File(_) | Self::ExecutableName(_) => true, Self::Implementation(_) => false, @@ -1348,7 +1351,7 @@ impl PythonRequest { /// [`Self::parse`] should always return the same request when given the output of this method. pub fn to_canonical_string(&self) -> String { match self { - Self::Any => "any".to_string(), + Self::Default => "any".to_string(), Self::Version(version) => version.to_string(), Self::Directory(path) => path.display().to_string(), Self::File(path) => path.display().to_string(), @@ -1801,7 +1804,7 @@ impl fmt::Display for VersionRequest { impl fmt::Display for PythonRequest { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { match self { - Self::Any => write!(f, "any Python"), + Self::Default => write!(f, "any Python"), Self::Version(version) => write!(f, "Python {version}"), Self::Directory(path) => write!(f, "directory `{}`", path.user_display()), Self::File(path) => write!(f, "path `{}`", path.user_display()), @@ -1883,7 +1886,7 @@ impl fmt::Display for PythonNotFound { }; match self.request { - PythonRequest::Any => { + PythonRequest::Default => { write!(f, "No interpreter found in {sources}") } _ => { @@ -1961,7 +1964,7 @@ mod tests { #[test] fn interpreter_request_from_str() { - assert_eq!(PythonRequest::parse("any"), PythonRequest::Any); + assert_eq!(PythonRequest::parse("any"), PythonRequest::Default); assert_eq!( PythonRequest::parse("3.12"), PythonRequest::Version(VersionRequest::from_str("3.12").unwrap()) @@ -2123,7 +2126,7 @@ mod tests { #[test] fn interpreter_request_to_canonical_string() { - assert_eq!(PythonRequest::Any.to_canonical_string(), "any"); + assert_eq!(PythonRequest::Default.to_canonical_string(), "any"); assert_eq!( PythonRequest::Version(VersionRequest::from_str("3.12").unwrap()).to_canonical_string(), "3.12" diff --git a/crates/uv-python/src/downloads.rs b/crates/uv-python/src/downloads.rs index 99c57dc422ac..174d48707e67 100644 --- a/crates/uv-python/src/downloads.rs +++ b/crates/uv-python/src/downloads.rs @@ -173,7 +173,7 @@ impl PythonDownloadRequest { .with_version(version.clone()), ), PythonRequest::Key(request) => Some(request.clone()), - PythonRequest::Any => Some(Self::default()), + PythonRequest::Default => Some(Self::default()), // We can't download a managed installation for these request kinds PythonRequest::Directory(_) | PythonRequest::ExecutableName(_) diff --git a/crates/uv-python/src/environment.rs b/crates/uv-python/src/environment.rs index 95437e41ccb7..ec1eafd53662 100644 --- a/crates/uv-python/src/environment.rs +++ b/crates/uv-python/src/environment.rs @@ -80,7 +80,7 @@ impl fmt::Display for EnvironmentNotFound { EnvironmentPreference::OnlyVirtual => SearchType::Virtual, }; - if matches!(self.request, PythonRequest::Any) { + if matches!(self.request, PythonRequest::Default) { write!(f, "No {search_type} found")?; } else { write!(f, "No {search_type} found for {}", self.request)?; diff --git a/crates/uv-python/src/installation.rs b/crates/uv-python/src/installation.rs index 8f61c4de6cbe..c33cb5fa5fa4 100644 --- a/crates/uv-python/src/installation.rs +++ b/crates/uv-python/src/installation.rs @@ -87,7 +87,7 @@ impl PythonInstallation { cache: &Cache, reporter: Option<&dyn Reporter>, ) -> Result { - let request = request.unwrap_or_else(|| &PythonRequest::Any); + let request = request.unwrap_or_else(|| &PythonRequest::Default); // Search for the installation match Self::find(request, environments, preference, cache) { diff --git a/crates/uv-python/src/lib.rs b/crates/uv-python/src/lib.rs index dafb3d192f15..5917671db40a 100644 --- a/crates/uv-python/src/lib.rs +++ b/crates/uv-python/src/lib.rs @@ -416,7 +416,7 @@ mod tests { context.search_path = Some(vec![]); let result = context.run(|| { find_python_installation( - &PythonRequest::Any, + &PythonRequest::Default, EnvironmentPreference::OnlySystem, PythonPreference::default(), &context.cache, @@ -430,7 +430,7 @@ mod tests { context.search_path = None; let result = context.run(|| { find_python_installation( - &PythonRequest::Any, + &PythonRequest::Default, EnvironmentPreference::OnlySystem, PythonPreference::default(), &context.cache, @@ -454,7 +454,7 @@ mod tests { let result = context.run(|| { find_python_installation( - &PythonRequest::Any, + &PythonRequest::Default, EnvironmentPreference::OnlySystem, PythonPreference::default(), &context.cache, @@ -478,7 +478,7 @@ mod tests { let interpreter = context.run(|| { find_python_installation( - &PythonRequest::Any, + &PythonRequest::Default, EnvironmentPreference::OnlySystem, PythonPreference::default(), &context.cache, @@ -538,7 +538,7 @@ mod tests { let python = context.run(|| { find_python_installation( - &PythonRequest::Any, + &PythonRequest::Default, EnvironmentPreference::OnlySystem, PythonPreference::default(), &context.cache, @@ -569,7 +569,7 @@ mod tests { let result = context.run(|| { find_python_installation( - &PythonRequest::Any, + &PythonRequest::Default, EnvironmentPreference::OnlySystem, PythonPreference::default(), &context.cache, @@ -605,7 +605,7 @@ mod tests { let python = context.run(|| { find_python_installation( - &PythonRequest::Any, + &PythonRequest::Default, EnvironmentPreference::OnlySystem, PythonPreference::default(), &context.cache, @@ -636,7 +636,7 @@ mod tests { let python = context.run(|| { find_python_installation( - &PythonRequest::Any, + &PythonRequest::Default, EnvironmentPreference::Any, PythonPreference::OnlySystem, &context.cache, @@ -657,7 +657,7 @@ mod tests { let python = context.run(|| { find_python_installation( - &PythonRequest::Any, + &PythonRequest::Default, EnvironmentPreference::Any, PythonPreference::OnlySystem, &context.cache, @@ -682,7 +682,7 @@ mod tests { let python = context.run(|| { find_python_installation( - &PythonRequest::Any, + &PythonRequest::Default, EnvironmentPreference::OnlySystem, PythonPreference::OnlySystem, &context.cache, @@ -707,7 +707,7 @@ mod tests { let python = context.run(|| { find_python_installation( - &PythonRequest::Any, + &PythonRequest::Default, EnvironmentPreference::Any, PythonPreference::OnlySystem, &context.cache, @@ -970,7 +970,7 @@ mod tests { let python = context.run_with_vars(&[("VIRTUAL_ENV", Some(venv.as_os_str()))], || { find_python_installation( - &PythonRequest::Any, + &PythonRequest::Default, EnvironmentPreference::Any, PythonPreference::OnlySystem, &context.cache, @@ -995,7 +995,7 @@ mod tests { let python = context.run_with_vars(&[("VIRTUAL_ENV", Some(venv.as_os_str()))], || { find_python_installation( - &PythonRequest::Any, + &PythonRequest::Default, EnvironmentPreference::Any, PythonPreference::OnlySystem, &context.cache, @@ -1020,7 +1020,7 @@ mod tests { context.run_with_vars(&[("CONDA_PREFIX", Some(condaenv.as_os_str()))], || { // Note this python is not treated as a system interpreter find_python_installation( - &PythonRequest::Any, + &PythonRequest::Default, EnvironmentPreference::OnlyVirtual, PythonPreference::OnlySystem, &context.cache, @@ -1050,7 +1050,7 @@ mod tests { ], || { find_python_installation( - &PythonRequest::Any, + &PythonRequest::Default, EnvironmentPreference::Any, PythonPreference::OnlySystem, &context.cache, @@ -1069,7 +1069,7 @@ mod tests { let python = context.run_with_vars(&[("CONDA_PREFIX", Some(condaenv.as_os_str()))], || { find_python_installation( - &PythonRequest::Any, + &PythonRequest::Default, EnvironmentPreference::Any, PythonPreference::OnlySystem, &context.cache, @@ -1094,7 +1094,7 @@ mod tests { let python = context.run(|| { find_python_installation( - &PythonRequest::Any, + &PythonRequest::Default, EnvironmentPreference::Any, PythonPreference::OnlySystem, &context.cache, @@ -1111,7 +1111,7 @@ mod tests { context.add_python_versions(&["3.12.1", "3.12.2"])?; let python = context.run(|| { find_python_installation( - &PythonRequest::Any, + &PythonRequest::Default, EnvironmentPreference::Any, PythonPreference::OnlySystem, &context.cache, @@ -1139,7 +1139,7 @@ mod tests { let python = context.run_with_vars(&[("VIRTUAL_ENV", Some(venv.as_os_str()))], || { find_python_installation( - &PythonRequest::Any, + &PythonRequest::Default, EnvironmentPreference::Any, PythonPreference::OnlySystem, &context.cache, @@ -1172,7 +1172,7 @@ mod tests { &[("UV_INTERNAL__PARENT_INTERPRETER", Some(parent.as_os_str()))], || { find_python_installation( - &PythonRequest::Any, + &PythonRequest::Default, EnvironmentPreference::Any, PythonPreference::OnlySystem, &context.cache, @@ -1196,7 +1196,7 @@ mod tests { ], || { find_python_installation( - &PythonRequest::Any, + &PythonRequest::Default, EnvironmentPreference::Any, PythonPreference::OnlySystem, &context.cache, @@ -1217,7 +1217,7 @@ mod tests { ], || { find_python_installation( - &PythonRequest::Any, + &PythonRequest::Default, EnvironmentPreference::ExplicitSystem, PythonPreference::OnlySystem, &context.cache, @@ -1238,7 +1238,7 @@ mod tests { ], || { find_python_installation( - &PythonRequest::Any, + &PythonRequest::Default, EnvironmentPreference::OnlySystem, PythonPreference::OnlySystem, &context.cache, @@ -1259,7 +1259,7 @@ mod tests { ], || { find_python_installation( - &PythonRequest::Any, + &PythonRequest::Default, EnvironmentPreference::OnlyVirtual, PythonPreference::OnlySystem, &context.cache, @@ -1292,7 +1292,7 @@ mod tests { &[("UV_INTERNAL__PARENT_INTERPRETER", Some(parent.as_os_str()))], || { find_python_installation( - &PythonRequest::Any, + &PythonRequest::Default, EnvironmentPreference::Any, PythonPreference::OnlySystem, &context.cache, @@ -1319,7 +1319,7 @@ mod tests { let python = context.run_with_vars(&[("VIRTUAL_ENV", Some(venv.as_os_str()))], || { find_python_installation( - &PythonRequest::Any, + &PythonRequest::Default, EnvironmentPreference::OnlySystem, PythonPreference::OnlySystem, &context.cache, @@ -1371,7 +1371,7 @@ mod tests { let result = context.run(|| { find_python_installation( - &PythonRequest::Any, + &PythonRequest::Default, EnvironmentPreference::OnlyVirtual, PythonPreference::OnlySystem, &context.cache, @@ -1422,7 +1422,7 @@ mod tests { let result = context.run(|| { find_python_installation( - &PythonRequest::Any, + &PythonRequest::Default, EnvironmentPreference::Any, PythonPreference::OnlySystem, &context.cache, @@ -1767,7 +1767,7 @@ mod tests { context.add_python_interpreters(&[(true, ImplementationName::PyPy, "pypy", "3.10.0")])?; let result = context.run(|| { find_python_installation( - &PythonRequest::Any, + &PythonRequest::Default, EnvironmentPreference::Any, PythonPreference::OnlySystem, &context.cache, @@ -1783,7 +1783,7 @@ mod tests { context.add_python_interpreters(&[(true, ImplementationName::PyPy, "python", "3.10.1")])?; let python = context.run(|| { find_python_installation( - &PythonRequest::Any, + &PythonRequest::Default, EnvironmentPreference::Any, PythonPreference::OnlySystem, &context.cache, @@ -1836,7 +1836,7 @@ mod tests { let python = context.run(|| { find_python_installation( - &PythonRequest::Any, + &PythonRequest::Default, EnvironmentPreference::Any, PythonPreference::OnlySystem, &context.cache, @@ -2110,7 +2110,7 @@ mod tests { )])?; let result = context.run(|| { find_python_installation( - &PythonRequest::Any, + &PythonRequest::Default, EnvironmentPreference::Any, PythonPreference::OnlySystem, &context.cache, @@ -2131,7 +2131,7 @@ mod tests { )])?; let python = context.run(|| { find_python_installation( - &PythonRequest::Any, + &PythonRequest::Default, EnvironmentPreference::Any, PythonPreference::OnlySystem, &context.cache, @@ -2184,7 +2184,7 @@ mod tests { let python = context.run(|| { find_python_installation( - &PythonRequest::Any, + &PythonRequest::Default, EnvironmentPreference::Any, PythonPreference::OnlySystem, &context.cache, diff --git a/crates/uv-python/src/managed.rs b/crates/uv-python/src/managed.rs index 70f28c96c593..1122f6772f0f 100644 --- a/crates/uv-python/src/managed.rs +++ b/crates/uv-python/src/managed.rs @@ -305,7 +305,7 @@ impl ManagedPythonInstallation { pub fn satisfies(&self, request: &PythonRequest) -> bool { match request { PythonRequest::File(path) => self.executable() == *path, - PythonRequest::Any => true, + PythonRequest::Default => true, PythonRequest::Directory(path) => self.path() == *path, PythonRequest::ExecutableName(name) => self .executable() diff --git a/crates/uv/src/commands/project/add.rs b/crates/uv/src/commands/project/add.rs index 12bddaa7161d..3cf9663656e5 100644 --- a/crates/uv/src/commands/project/add.rs +++ b/crates/uv/src/commands/project/add.rs @@ -139,7 +139,7 @@ pub(crate) async fn add( request } else { // (3) Assume any Python version - PythonRequest::Any + PythonRequest::Default }; let interpreter = PythonInstallation::find_or_download( diff --git a/crates/uv/src/commands/python/install.rs b/crates/uv/src/commands/python/install.rs index 4d94975cb38d..55b82476efa8 100644 --- a/crates/uv/src/commands/python/install.rs +++ b/crates/uv/src/commands/python/install.rs @@ -41,7 +41,7 @@ pub(crate) async fn install( PythonVersionFile::discover(&*CWD, no_config, true) .await? .map(uv_python::PythonVersionFile::into_versions) - .unwrap_or_else(|| vec![PythonRequest::Any]) + .unwrap_or_else(|| vec![PythonRequest::Default]) } else { targets .iter() @@ -62,7 +62,7 @@ pub(crate) async fn install( let mut unfilled_requests = Vec::new(); let mut uninstalled = Vec::new(); for (request, download_request) in requests.iter().zip(download_requests) { - if matches!(requests.as_slice(), [PythonRequest::Any]) { + if matches!(requests.as_slice(), [PythonRequest::Default]) { writeln!(printer.stderr(), "Searching for Python installations")?; } else { writeln!( @@ -75,7 +75,7 @@ pub(crate) async fn install( .iter() .find(|installation| download_request.satisfied_by_key(installation.key())) { - if matches!(request, PythonRequest::Any) { + if matches!(request, PythonRequest::Default) { writeln!(printer.stderr(), "Found: {}", installation.key().green())?; } else { writeln!( @@ -96,7 +96,7 @@ pub(crate) async fn install( } if unfilled_requests.is_empty() { - if matches!(requests.as_slice(), [PythonRequest::Any]) { + if matches!(requests.as_slice(), [PythonRequest::Default]) { writeln!( printer.stderr(), "Python is already available. Use `uv python install ` to install a specific version.", diff --git a/crates/uv/src/commands/python/list.rs b/crates/uv/src/commands/python/list.rs index b8f089cf3ef1..65700b3ae9ec 100644 --- a/crates/uv/src/commands/python/list.rs +++ b/crates/uv/src/commands/python/list.rs @@ -66,7 +66,7 @@ pub(crate) async fn list( }; let installed = find_python_installations( - &PythonRequest::Any, + &PythonRequest::Default, EnvironmentPreference::OnlySystem, python_preference, cache, diff --git a/crates/uv/src/commands/python/uninstall.rs b/crates/uv/src/commands/python/uninstall.rs index 9e66b04606d2..11cd6602c520 100644 --- a/crates/uv/src/commands/python/uninstall.rs +++ b/crates/uv/src/commands/python/uninstall.rs @@ -59,7 +59,7 @@ async fn do_uninstall( let start = std::time::Instant::now(); let requests = if all { - vec![PythonRequest::Any] + vec![PythonRequest::Default] } else { let targets = targets.into_iter().collect::>(); targets @@ -82,7 +82,7 @@ async fn do_uninstall( let installed_installations: Vec<_> = installations.find_all()?.collect(); let mut matching_installations = BTreeSet::default(); for (request, download_request) in requests.iter().zip(download_requests) { - if matches!(requests.as_slice(), [PythonRequest::Any]) { + if matches!(requests.as_slice(), [PythonRequest::Default]) { writeln!(printer.stderr(), "Searching for Python installations")?; } else { writeln!( @@ -100,7 +100,7 @@ async fn do_uninstall( matching_installations.insert(installation.clone()); } if !found { - if matches!(requests.as_slice(), [PythonRequest::Any]) { + if matches!(requests.as_slice(), [PythonRequest::Default]) { writeln!(printer.stderr(), "No Python installations found")?; return Ok(ExitStatus::Failure); }