Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update the interface for declaring Python download preferences #5936

Merged
merged 1 commit into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions crates/uv-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use uv_configuration::{
ConfigSettingEntry, IndexStrategy, KeyringProviderType, PackageNameSpecifier, TargetTriple,
};
use uv_normalize::{ExtraName, PackageName};
use uv_python::{PythonFetch, PythonPreference, PythonVersion};
use uv_python::{PythonDownloads, PythonPreference, PythonVersion};
use uv_resolver::{AnnotationStyle, ExcludeNewer, PrereleaseMode, ResolutionMode};

pub mod compat;
Expand Down Expand Up @@ -119,9 +119,17 @@ pub struct GlobalArgs {
)]
pub python_preference: Option<PythonPreference>,

/// Whether to automatically download Python when required.
/// Allow automatically downloading Python when required.
#[arg(global = true, long, help_heading = "Python options", hide = true)]
pub allow_python_downloads: bool,

/// Disable automatic downloads of Python.
#[arg(global = true, long, help_heading = "Python options")]
pub python_fetch: Option<PythonFetch>,
pub no_python_downloads: bool,

/// Deprecated version of [`Self::python_downloads`].
#[arg(global = true, long, hide = true)]
pub python_fetch: Option<PythonDownloads>,

/// Do not print any output.
#[arg(global = true, long, short, conflicts_with = "verbose")]
Expand Down Expand Up @@ -258,7 +266,7 @@ pub enum Commands {
///
/// When preview is enabled, i.e., via `--preview` or by using a preview
/// command, uv will download Python if a version cannot be found. This
/// behavior can be disabled with the `--python-fetch` option.
/// behavior can be disabled with the `--python-downloads` option.
///
/// The `--python` option allows requesting a different interpreter.
///
Expand Down
21 changes: 17 additions & 4 deletions crates/uv-python/src/discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,25 @@ pub enum PythonPreference {
#[serde(deny_unknown_fields, rename_all = "kebab-case")]
#[cfg_attr(feature = "clap", derive(clap::ValueEnum))]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
pub enum PythonFetch {
/// Automatically fetch managed Python installations when needed.
pub enum PythonDownloads {
/// Automatically download managed Python installations when needed.
#[default]
#[serde(alias = "auto")]
Automatic,
/// Do not automatically fetch managed Python installations; require explicit installation.
/// Do not automatically download managed Python installations; require explicit installation.
Manual,
/// Do not ever allow Python downloads.
Never,
}

impl From<bool> for PythonDownloads {
fn from(value: bool) -> Self {
if value {
PythonDownloads::Automatic
} else {
PythonDownloads::Never
}
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
Expand Down Expand Up @@ -1298,7 +1311,7 @@ impl PythonPreference {
}
}

impl PythonFetch {
impl PythonDownloads {
pub fn is_automatic(self) -> bool {
matches!(self, Self::Automatic)
}
Expand Down
8 changes: 4 additions & 4 deletions crates/uv-python/src/installation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::implementation::LenientImplementationName;
use crate::managed::{ManagedPythonInstallation, ManagedPythonInstallations};
use crate::platform::{Arch, Libc, Os};
use crate::{
downloads, Error, Interpreter, PythonFetch, PythonPreference, PythonSource, PythonVersion,
downloads, Error, Interpreter, PythonDownloads, PythonPreference, PythonSource, PythonVersion,
};

/// A Python interpreter and accompanying tools.
Expand Down Expand Up @@ -77,11 +77,11 @@ impl PythonInstallation {
/// Find or fetch a [`PythonInstallation`].
///
/// Unlike [`PythonInstallation::find`], if the required Python is not installed it will be installed automatically.
pub async fn find_or_fetch<'a>(
pub async fn find_or_download<'a>(
request: Option<PythonRequest>,
environments: EnvironmentPreference,
preference: PythonPreference,
python_fetch: PythonFetch,
python_downloads: PythonDownloads,
client_builder: &BaseClientBuilder<'a>,
cache: &Cache,
reporter: Option<&dyn Reporter>,
Expand All @@ -94,7 +94,7 @@ impl PythonInstallation {
// If missing and allowed, perform a fetch
Err(Error::MissingPython(err))
if preference.allows_managed()
&& python_fetch.is_automatic()
&& python_downloads.is_automatic()
&& client_builder.connectivity.is_online() =>
{
if let Some(request) = PythonDownloadRequest::from_request(&request) {
Expand Down
2 changes: 1 addition & 1 deletion crates/uv-python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use thiserror::Error;

pub use crate::discovery::{
find_python_installations, EnvironmentPreference, Error as DiscoveryError, PythonFetch,
find_python_installations, EnvironmentPreference, Error as DiscoveryError, PythonDownloads,
PythonNotFound, PythonPreference, PythonRequest, PythonSource, VersionRequest,
};
pub use crate::environment::PythonEnvironment;
Expand Down
4 changes: 2 additions & 2 deletions crates/uv-settings/src/combine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::path::PathBuf;
use distribution_types::IndexUrl;
use install_wheel_rs::linker::LinkMode;
use uv_configuration::{ConfigSettings, IndexStrategy, KeyringProviderType, TargetTriple};
use uv_python::{PythonFetch, PythonPreference, PythonVersion};
use uv_python::{PythonDownloads, PythonPreference, PythonVersion};
use uv_resolver::{AnnotationStyle, ExcludeNewer, PrereleaseMode, ResolutionMode};

use crate::{FilesystemOptions, PipOptions};
Expand Down Expand Up @@ -70,7 +70,7 @@ impl_combine_or!(ResolutionMode);
impl_combine_or!(String);
impl_combine_or!(TargetTriple);
impl_combine_or!(PythonPreference);
impl_combine_or!(PythonFetch);
impl_combine_or!(PythonDownloads);
impl_combine_or!(bool);

impl<T> Combine for Option<Vec<T>> {
Expand Down
8 changes: 4 additions & 4 deletions crates/uv-settings/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use uv_configuration::{
};
use uv_macros::{CombineOptions, OptionsMetadata};
use uv_normalize::{ExtraName, PackageName};
use uv_python::{PythonFetch, PythonPreference, PythonVersion};
use uv_python::{PythonDownloads, PythonPreference, PythonVersion};
use uv_resolver::{AnnotationStyle, ExcludeNewer, PrereleaseMode, ResolutionMode};

/// A `pyproject.toml` with an (optional) `[tool.uv]` section.
Expand Down Expand Up @@ -143,16 +143,16 @@ pub struct GlobalOptions {
possible_values = true
)]
pub python_preference: Option<PythonPreference>,
/// Whether to automatically download Python when required.
/// Whether to allow Python downloads.
#[option(
default = "\"automatic\"",
value_type = "str",
example = r#"
python-fetch = "manual"
python-downloads = "manual"
"#,
possible_values = true
)]
pub python_fetch: Option<PythonFetch>,
pub python_downloads: Option<PythonDownloads>,
}

/// Settings relevant to all installer operations.
Expand Down
6 changes: 3 additions & 3 deletions crates/uv/src/commands/project/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use uv_dispatch::BuildDispatch;
use uv_distribution::DistributionDatabase;
use uv_fs::CWD;
use uv_normalize::PackageName;
use uv_python::{PythonFetch, PythonPreference, PythonRequest};
use uv_python::{PythonDownloads, PythonPreference, PythonRequest};
use uv_requirements::{NamedRequirementsResolver, RequirementsSource, RequirementsSpecification};
use uv_resolver::FlatIndex;
use uv_types::{BuildIsolation, HashStrategy};
Expand Down Expand Up @@ -51,7 +51,7 @@ pub(crate) async fn add(
python: Option<String>,
settings: ResolverInstallerSettings,
python_preference: PythonPreference,
python_fetch: PythonFetch,
python_downloads: PythonDownloads,
preview: PreviewMode,
connectivity: Connectivity,
concurrency: Concurrency,
Expand Down Expand Up @@ -93,7 +93,7 @@ pub(crate) async fn add(
project.workspace(),
python.as_deref().map(PythonRequest::parse),
python_preference,
python_fetch,
python_downloads,
connectivity,
native_tls,
cache,
Expand Down
16 changes: 8 additions & 8 deletions crates/uv/src/commands/project/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use uv_client::{BaseClientBuilder, Connectivity};
use uv_configuration::PreviewMode;
use uv_fs::{absolutize_path, Simplified, CWD};
use uv_python::{
EnvironmentPreference, PythonFetch, PythonInstallation, PythonPreference, PythonRequest,
EnvironmentPreference, PythonDownloads, PythonInstallation, PythonPreference, PythonRequest,
VersionRequest,
};
use uv_resolver::RequiresPython;
Expand All @@ -35,7 +35,7 @@ pub(crate) async fn init(
no_workspace: bool,
preview: PreviewMode,
python_preference: PythonPreference,
python_fetch: PythonFetch,
python_downloads: PythonDownloads,
connectivity: Connectivity,
native_tls: bool,
cache: &Cache,
Expand Down Expand Up @@ -86,7 +86,7 @@ pub(crate) async fn init(
python,
no_workspace,
python_preference,
python_fetch,
python_downloads,
connectivity,
native_tls,
cache,
Expand Down Expand Up @@ -160,7 +160,7 @@ async fn init_project(
python: Option<String>,
no_workspace: bool,
python_preference: PythonPreference,
python_fetch: PythonFetch,
python_downloads: PythonDownloads,
connectivity: Connectivity,
native_tls: bool,
cache: &Cache,
Expand Down Expand Up @@ -213,11 +213,11 @@ async fn init_project(
let client_builder = BaseClientBuilder::new()
.connectivity(connectivity)
.native_tls(native_tls);
let interpreter = PythonInstallation::find_or_fetch(
let interpreter = PythonInstallation::find_or_download(
Some(request),
EnvironmentPreference::Any,
python_preference,
python_fetch,
python_downloads,
&client_builder,
cache,
Some(&reporter),
Expand All @@ -240,11 +240,11 @@ async fn init_project(
let client_builder = BaseClientBuilder::new()
.connectivity(connectivity)
.native_tls(native_tls);
let interpreter = PythonInstallation::find_or_fetch(
let interpreter = PythonInstallation::find_or_download(
Some(request),
EnvironmentPreference::Any,
python_preference,
python_fetch,
python_downloads,
&client_builder,
cache,
Some(&reporter),
Expand Down
6 changes: 3 additions & 3 deletions crates/uv/src/commands/project/lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use uv_dispatch::BuildDispatch;
use uv_fs::CWD;
use uv_git::ResolvedRepositoryReference;
use uv_normalize::{PackageName, DEV_DEPENDENCIES};
use uv_python::{Interpreter, PythonEnvironment, PythonFetch, PythonPreference, PythonRequest};
use uv_python::{Interpreter, PythonDownloads, PythonEnvironment, PythonPreference, PythonRequest};
use uv_requirements::upgrade::{read_lock_requirements, LockedRequirements};
use uv_resolver::{
FlatIndex, Lock, OptionsBuilder, PythonRequirement, RequiresPython, ResolverMarkers,
Expand Down Expand Up @@ -50,7 +50,7 @@ pub(crate) async fn lock(
settings: ResolverSettings,
preview: PreviewMode,
python_preference: PythonPreference,
python_fetch: PythonFetch,
python_downloads: PythonDownloads,
connectivity: Connectivity,
concurrency: Concurrency,
native_tls: bool,
Expand All @@ -69,7 +69,7 @@ pub(crate) async fn lock(
&workspace,
python.as_deref().map(PythonRequest::parse),
python_preference,
python_fetch,
python_downloads,
connectivity,
native_tls,
cache,
Expand Down
14 changes: 7 additions & 7 deletions crates/uv/src/commands/project/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ use uv_fs::Simplified;
use uv_installer::{SatisfiesResult, SitePackages};
use uv_normalize::PackageName;
use uv_python::{
request_from_version_file, EnvironmentPreference, Interpreter, PythonEnvironment, PythonFetch,
PythonInstallation, PythonPreference, PythonRequest, VersionRequest,
request_from_version_file, EnvironmentPreference, Interpreter, PythonDownloads,
PythonEnvironment, PythonInstallation, PythonPreference, PythonRequest, VersionRequest,
};
use uv_requirements::{NamedRequirementsResolver, RequirementsSpecification};
use uv_resolver::{
Expand Down Expand Up @@ -202,7 +202,7 @@ impl FoundInterpreter {
workspace: &Workspace,
python_request: Option<PythonRequest>,
python_preference: PythonPreference,
python_fetch: PythonFetch,
python_downloads: PythonDownloads,
connectivity: Connectivity,
native_tls: bool,
cache: &Cache,
Expand Down Expand Up @@ -251,11 +251,11 @@ impl FoundInterpreter {
let reporter = PythonDownloadReporter::single(printer);

// Locate the Python interpreter to use in the environment
let python = PythonInstallation::find_or_fetch(
let python = PythonInstallation::find_or_download(
python_request,
EnvironmentPreference::OnlySystem,
python_preference,
python_fetch,
python_downloads,
&client_builder,
cache,
Some(&reporter),
Expand Down Expand Up @@ -329,7 +329,7 @@ pub(crate) async fn get_or_init_environment(
workspace: &Workspace,
python: Option<PythonRequest>,
python_preference: PythonPreference,
python_fetch: PythonFetch,
python_downloads: PythonDownloads,
connectivity: Connectivity,
native_tls: bool,
cache: &Cache,
Expand All @@ -339,7 +339,7 @@ pub(crate) async fn get_or_init_environment(
workspace,
python,
python_preference,
python_fetch,
python_downloads,
connectivity,
native_tls,
cache,
Expand Down
6 changes: 3 additions & 3 deletions crates/uv/src/commands/project/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use uv_cache::Cache;
use uv_client::Connectivity;
use uv_configuration::{Concurrency, ExtrasSpecification, PreviewMode};
use uv_fs::CWD;
use uv_python::{PythonFetch, PythonPreference, PythonRequest};
use uv_python::{PythonDownloads, PythonPreference, PythonRequest};
use uv_warnings::{warn_user, warn_user_once};
use uv_workspace::pyproject::DependencyType;
use uv_workspace::pyproject_mut::PyProjectTomlMut;
Expand All @@ -29,7 +29,7 @@ pub(crate) async fn remove(
python: Option<String>,
settings: ResolverInstallerSettings,
python_preference: PythonPreference,
python_fetch: PythonFetch,
python_downloads: PythonDownloads,
preview: PreviewMode,
connectivity: Connectivity,
concurrency: Concurrency,
Expand Down Expand Up @@ -101,7 +101,7 @@ pub(crate) async fn remove(
project.workspace(),
python.as_deref().map(PythonRequest::parse),
python_preference,
python_fetch,
python_downloads,
connectivity,
native_tls,
cache,
Expand Down
Loading
Loading