Allow -p to use complex Python version requests in uv pip compile (#11486)

Closes #11285
Closes https://github.com/astral-sh/uv/pull/11437

This changes `-p` from an alias of `--python-version` to `--python`
while retaining backwards compatibility for `--python-version`-like
fallback behavior when the requested version, e.g., `-p 3.12`, cannot be
found.

This was initially implemented with a hidden `--python-legacy` flag
which allows us to special case the short `-p` flag — unlike the
implementation in #11437. However, after further discussion, we decided
the behavior difference between `-p` and `--python` would be confusing
so now `-p` is an alias for `--python` and `--python` is special-cased
when a version is used.

Additionally, we now respect the `UV_PYTHON` environment variable, but
it is ignored when `--python-version` is set. If you want different
`--python-version` and `--python` values, you must do so explicitly. I
considered banning this, but it is valid for e.g. `--python pypy
--python-version 3.12`
This commit is contained in:
Zanie Blue 2025-02-13 15:44:01 -06:00
parent 4b49151c22
commit 61fcdfb2e4
5 changed files with 583 additions and 71 deletions

View file

@ -1079,15 +1079,23 @@ pub struct PipCompileArgs {
/// The Python interpreter to use during resolution.
///
/// A Python interpreter is required for building source distributions to
/// determine package metadata when there are not wheels.
/// A Python interpreter is required for building source distributions to determine package
/// metadata when there are not wheels.
///
/// The interpreter is also used to determine the default minimum Python
/// version, unless `--python-version` is provided.
/// The interpreter is also used to determine the default minimum Python version, unless
/// `--python-version` is provided.
///
/// See `uv help python` for details on Python discovery and supported
/// request formats.
#[arg(long, verbatim_doc_comment, help_heading = "Python options", value_parser = parse_maybe_string)]
/// This option respects `UV_PYTHON`, but when set via environment variable, it is overridden
/// by `--python-version`.
///
/// See `uv help python` for details on Python discovery and supported request formats.
#[arg(
long,
short,
verbatim_doc_comment,
help_heading = "Python options",
value_parser = parse_maybe_string
)]
pub python: Option<Maybe<String>>,
/// Install packages into the system Python environment.
@ -1170,7 +1178,7 @@ pub struct PipCompileArgs {
///
/// If a patch version is omitted, the minimum patch version is assumed. For
/// example, `3.8` is mapped to `3.8.0`.
#[arg(long, short, help_heading = "Python options")]
#[arg(long, help_heading = "Python options")]
pub python_version: Option<PythonVersion>,
/// The platform for which requirements should be resolved.