Skip to content

Commit

Permalink
Coerce empty string values to None for UV_PYTHON env var (#7878)
Browse files Browse the repository at this point in the history
## Summary

Closes #7841. If there are other env vars that would also benefit from
this value parser, please let me know and I can add them to this PR.

## Test Plan

When running the same example from the linked issue, it now works:

```
UV_PYTHON= cargo run -- init x
   Compiling ...
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 29.06s
     Running `target/debug/uv init x`
Initialized project `x` at `/Users/krishnanchandra/Projects/uv/x`
```
  • Loading branch information
krishnan-chandra authored Oct 4, 2024
1 parent c591636 commit d73b253
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 66 deletions.
119 changes: 75 additions & 44 deletions crates/uv-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,16 @@ fn parse_maybe_file_path(input: &str) -> Result<Maybe<PathBuf>, String> {
}
}

// Parse a string, mapping the empty string to `None`.
#[allow(clippy::unnecessary_wraps)]
fn parse_maybe_string(input: &str) -> Result<Maybe<String>, String> {
if input.is_empty() {
Ok(Maybe::None)
} else {
Ok(Maybe::Some(input.to_string()))
}
}

#[derive(Args)]
#[allow(clippy::struct_excessive_bools)]
pub struct PipCompileArgs {
Expand Down Expand Up @@ -967,8 +977,8 @@ pub struct PipCompileArgs {
///
/// See `uv help python` for details on Python discovery and supported
/// request formats.
#[arg(long, verbatim_doc_comment, help_heading = "Python options")]
pub python: Option<String>,
#[arg(long, verbatim_doc_comment, help_heading = "Python options", value_parser = parse_maybe_string)]
pub python: Option<Maybe<String>>,

/// Install packages into the system Python environment.
///
Expand Down Expand Up @@ -1221,9 +1231,10 @@ pub struct PipSyncArgs {
short,
env = "UV_PYTHON",
verbatim_doc_comment,
help_heading = "Python options"
help_heading = "Python options",
value_parser = parse_maybe_string,
)]
pub python: Option<String>,
pub python: Option<Maybe<String>>,

/// Install packages into the system Python environment.
///
Expand Down Expand Up @@ -1503,9 +1514,10 @@ pub struct PipInstallArgs {
short,
env = "UV_PYTHON",
verbatim_doc_comment,
help_heading = "Python options"
help_heading = "Python options",
value_parser = parse_maybe_string,
)]
pub python: Option<String>,
pub python: Option<Maybe<String>>,

/// Install packages into the system Python environment.
///
Expand Down Expand Up @@ -1668,9 +1680,10 @@ pub struct PipUninstallArgs {
short,
env = "UV_PYTHON",
verbatim_doc_comment,
help_heading = "Python options"
help_heading = "Python options",
value_parser = parse_maybe_string,
)]
pub python: Option<String>,
pub python: Option<Maybe<String>>,

/// Attempt to use `keyring` for authentication for remote requirements files.
///
Expand Down Expand Up @@ -1776,9 +1789,10 @@ pub struct PipFreezeArgs {
short,
env = "UV_PYTHON",
verbatim_doc_comment,
help_heading = "Python options"
help_heading = "Python options",
value_parser = parse_maybe_string,
)]
pub python: Option<String>,
pub python: Option<Maybe<String>>,

/// List packages in the system Python environment.
///
Expand Down Expand Up @@ -1840,9 +1854,10 @@ pub struct PipListArgs {
short,
env = "UV_PYTHON",
verbatim_doc_comment,
help_heading = "Python options"
help_heading = "Python options",
value_parser = parse_maybe_string,
)]
pub python: Option<String>,
pub python: Option<Maybe<String>>,

/// List packages in the system Python environment.
///
Expand Down Expand Up @@ -1880,9 +1895,10 @@ pub struct PipCheckArgs {
short,
env = "UV_PYTHON",
verbatim_doc_comment,
help_heading = "Python options"
help_heading = "Python options",
value_parser = parse_maybe_string,
)]
pub python: Option<String>,
pub python: Option<Maybe<String>>,

/// Check packages in the system Python environment.
///
Expand Down Expand Up @@ -1928,9 +1944,10 @@ pub struct PipShowArgs {
short,
env = "UV_PYTHON",
verbatim_doc_comment,
help_heading = "Python options"
help_heading = "Python options",
value_parser = parse_maybe_string,
)]
pub python: Option<String>,
pub python: Option<Maybe<String>>,

/// Show a package in the system Python environment.
///
Expand Down Expand Up @@ -1983,9 +2000,10 @@ pub struct PipTreeArgs {
short,
env = "UV_PYTHON",
verbatim_doc_comment,
help_heading = "Python options"
help_heading = "Python options",
value_parser = parse_maybe_string,
)]
pub python: Option<String>,
pub python: Option<Maybe<String>>,

/// List packages in the system Python environment.
///
Expand Down Expand Up @@ -2118,9 +2136,10 @@ pub struct BuildArgs {
short,
env = "UV_PYTHON",
verbatim_doc_comment,
help_heading = "Python options"
help_heading = "Python options",
value_parser = parse_maybe_string,
)]
pub python: Option<String>,
pub python: Option<Maybe<String>>,

#[command(flatten)]
pub resolver: ResolverArgs,
Expand All @@ -2147,9 +2166,10 @@ pub struct VenvArgs {
short,
env = "UV_PYTHON",
verbatim_doc_comment,
help_heading = "Python options"
help_heading = "Python options",
value_parser = parse_maybe_string,
)]
pub python: Option<String>,
pub python: Option<Maybe<String>>,

/// Ignore virtual environments when searching for the Python interpreter.
///
Expand Down Expand Up @@ -2435,9 +2455,10 @@ pub struct InitArgs {
short,
env = "UV_PYTHON",
verbatim_doc_comment,
help_heading = "Python options"
help_heading = "Python options",
value_parser = parse_maybe_string,
)]
pub python: Option<String>,
pub python: Option<Maybe<String>>,
}

#[derive(Args)]
Expand Down Expand Up @@ -2610,9 +2631,10 @@ pub struct RunArgs {
short,
env = "UV_PYTHON",
verbatim_doc_comment,
help_heading = "Python options"
help_heading = "Python options",
value_parser = parse_maybe_string,
)]
pub python: Option<String>,
pub python: Option<Maybe<String>>,

/// Whether to show resolver and installer output from any environment modifications.
///
Expand Down Expand Up @@ -2752,9 +2774,10 @@ pub struct SyncArgs {
short,
env = "UV_PYTHON",
verbatim_doc_comment,
help_heading = "Python options"
help_heading = "Python options",
value_parser = parse_maybe_string,
)]
pub python: Option<String>,
pub python: Option<Maybe<String>>,
}

#[derive(Args)]
Expand Down Expand Up @@ -2795,9 +2818,10 @@ pub struct LockArgs {
short,
env = "UV_PYTHON",
verbatim_doc_comment,
help_heading = "Python options"
help_heading = "Python options",
value_parser = parse_maybe_string,
)]
pub python: Option<String>,
pub python: Option<Maybe<String>>,
}

#[derive(Args)]
Expand Down Expand Up @@ -2917,9 +2941,10 @@ pub struct AddArgs {
short,
env = "UV_PYTHON",
verbatim_doc_comment,
help_heading = "Python options"
help_heading = "Python options",
value_parser = parse_maybe_string,
)]
pub python: Option<String>,
pub python: Option<Maybe<String>>,
}

#[derive(Args)]
Expand Down Expand Up @@ -2983,9 +3008,10 @@ pub struct RemoveArgs {
short,
env = "UV_PYTHON",
verbatim_doc_comment,
help_heading = "Python options"
help_heading = "Python options",
value_parser = parse_maybe_string,
)]
pub python: Option<String>,
pub python: Option<Maybe<String>>,
}

#[derive(Args)]
Expand Down Expand Up @@ -3057,9 +3083,10 @@ pub struct TreeArgs {
short,
env = "UV_PYTHON",
verbatim_doc_comment,
help_heading = "Python options"
help_heading = "Python options",
value_parser = parse_maybe_string,
)]
pub python: Option<String>,
pub python: Option<Maybe<String>>,
}

#[derive(Args)]
Expand Down Expand Up @@ -3181,9 +3208,10 @@ pub struct ExportArgs {
short,
env = "UV_PYTHON",
verbatim_doc_comment,
help_heading = "Python options"
help_heading = "Python options",
value_parser = parse_maybe_string,
)]
pub python: Option<String>,
pub python: Option<Maybe<String>>,
}

#[derive(Args)]
Expand Down Expand Up @@ -3330,9 +3358,10 @@ pub struct ToolRunArgs {
short,
env = "UV_PYTHON",
verbatim_doc_comment,
help_heading = "Python options"
help_heading = "Python options",
value_parser = parse_maybe_string,
)]
pub python: Option<String>,
pub python: Option<Maybe<String>>,

/// Whether to show resolver and installer output from any environment modifications.
///
Expand Down Expand Up @@ -3391,9 +3420,10 @@ pub struct ToolInstallArgs {
short,
env = "UV_PYTHON",
verbatim_doc_comment,
help_heading = "Python options"
help_heading = "Python options",
value_parser = parse_maybe_string,
)]
pub python: Option<String>,
pub python: Option<Maybe<String>>,
}

#[derive(Args)]
Expand Down Expand Up @@ -3467,9 +3497,10 @@ pub struct ToolUpgradeArgs {
short,
env = "UV_PYTHON",
verbatim_doc_comment,
help_heading = "Python options"
help_heading = "Python options",
value_parser = parse_maybe_string,
)]
pub python: Option<String>,
pub python: Option<Maybe<String>>,

#[command(flatten)]
pub installer: ResolverInstallerArgs,
Expand Down
Loading

0 comments on commit d73b253

Please sign in to comment.