-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Allow -p
to use complex Python version requests in uv pip compile
#11486
Merged
+583
−71
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e1814ff
Allow `-p` to use complex Python version requests in `uv pip compile`
zanieb 2767613
Windows snaps
zanieb b015bb7
Simplify per feedback
zanieb 24be46c
Feedback
zanieb f412685
More cruft
zanieb 0c0c0c6
Add test coverage for `--python-version` overriding `UV_PYTHON` entirely
zanieb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
use std::collections::BTreeSet; | ||
use std::env; | ||
use std::path::Path; | ||
use std::str::FromStr; | ||
use std::sync::Arc; | ||
|
||
use anyhow::{anyhow, Result}; | ||
|
@@ -38,6 +39,7 @@ use uv_resolver::{ | |
InMemoryIndex, OptionsBuilder, PrereleaseMode, PythonRequirement, RequiresPython, | ||
ResolutionMode, ResolverEnvironment, | ||
}; | ||
use uv_static::EnvVars; | ||
use uv_types::{BuildIsolation, EmptyInstalledPackages, HashStrategy}; | ||
use uv_warnings::warn_user; | ||
|
||
|
@@ -86,14 +88,15 @@ pub(crate) async fn pip_compile( | |
no_build_isolation: bool, | ||
no_build_isolation_package: Vec<PackageName>, | ||
build_options: BuildOptions, | ||
python_version: Option<PythonVersion>, | ||
mut python_version: Option<PythonVersion>, | ||
python_platform: Option<TargetTriple>, | ||
universal: bool, | ||
exclude_newer: Option<ExcludeNewer>, | ||
sources: SourceStrategy, | ||
annotation_style: AnnotationStyle, | ||
link_mode: LinkMode, | ||
python: Option<String>, | ||
mut python: Option<String>, | ||
mut python_legacy: Option<String>, | ||
system: bool, | ||
python_preference: PythonPreference, | ||
concurrency: Concurrency, | ||
|
@@ -103,6 +106,38 @@ pub(crate) async fn pip_compile( | |
printer: Printer, | ||
preview: PreviewMode, | ||
) -> Result<ExitStatus> { | ||
// If the user requests both `-p` and `--python` or `--python-version`, error | ||
if let Some(python_legacy) = python_legacy.as_ref() { | ||
if let Some(python) = python.as_ref() { | ||
return Err(anyhow!( | ||
"Cannot specify both `-p` ({python_legacy}) and `--python` ({python}).", | ||
)); | ||
} | ||
if let Some(python_version) = python_version.as_ref() { | ||
return Err(anyhow!( | ||
"Cannot specify both `-p` ({python_legacy}) and `--python-version` ({python_version}).", | ||
)); | ||
} | ||
} | ||
zanieb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Respect `UV_PYTHON` with legacy behavior | ||
if python_legacy.is_none() && python_version.is_none() && python.is_none() { | ||
if let Ok(python) = std::env::var(EnvVars::UV_PYTHON) { | ||
if !python.is_empty() { | ||
python_legacy = Some(python); | ||
} | ||
} | ||
} | ||
|
||
// Resolve `-p` into `--python-version` or `--python` | ||
if let Some(python_legacy) = python_legacy { | ||
if let Ok(version) = PythonVersion::from_str(&python_legacy) { | ||
python_version = Some(version); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So the net effect here is that we don't error if the version doesn't exist, right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes |
||
} else { | ||
python = Some(python_legacy); | ||
} | ||
} | ||
|
||
// If the user requests `extras` but does not provide a valid source (e.g., a `pyproject.toml`), | ||
// return an error. | ||
if !extras.is_empty() && !requirements.iter().any(RequirementsSource::allows_extras) { | ||
|
@@ -189,8 +224,8 @@ pub(crate) async fn pip_compile( | |
let request = PythonRequest::parse(python); | ||
PythonInstallation::find(&request, environment_preference, python_preference, &cache) | ||
} else { | ||
// TODO(zanieb): The split here hints at a problem with the abstraction; we should be able to use | ||
// `PythonInstallation::find(...)` here. | ||
// TODO(zanieb): The split here hints at a problem with the request abstraction; we should | ||
// be able to use `PythonInstallation::find(...)` here. | ||
let request = if let Some(version) = python_version.as_ref() { | ||
// TODO(zanieb): We should consolidate `VersionRequest` and `PythonVersion` | ||
PythonRequest::Version(VersionRequest::from(version)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why can't this just be done via
-p
? So--python 3.7
and-p 3.7
now behave differently?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should
--python 3.7
error if 3.7 isn't installed or available?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's complicated.
--python 3.7
continues to work as it does today — it errors if it's not found. I don't know if I want to break that behavior because it does matter when you're not just doing version requests, e.g.,--python pypy
needs to fail if we can't find that implementation or the tags will be wrong. Similarly,--python <path>
needs to fail.We could special case
--python <version>
in the future if we want, which (as you said) would let us drop this legacy flag. However, then there would be no way for users to enforce that we actually find that interpreter.