Skip to content
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
76 changes: 76 additions & 0 deletions crates/uv-python/src/discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2705,12 +2705,15 @@ mod tests {
use std::{path::PathBuf, str::FromStr};

use assert_fs::{prelude::*, TempDir};
use target_lexicon::{Aarch64Architecture, Architecture};
use test_log::test;
use uv_pep440::{Prerelease, PrereleaseKind, VersionSpecifiers};

use crate::{
discovery::{PythonRequest, VersionRequest},
downloads::PythonDownloadRequest,
implementation::ImplementationName,
platform::{Arch, Libc, Os},
};

use super::{Error, PythonVariant};
Expand Down Expand Up @@ -2763,13 +2766,86 @@ mod tests {
PythonRequest::parse("cpython"),
PythonRequest::Implementation(ImplementationName::CPython)
);

assert_eq!(
PythonRequest::parse("cpython3.12.2"),
PythonRequest::ImplementationVersion(
ImplementationName::CPython,
VersionRequest::from_str("3.12.2").unwrap(),
)
);

assert_eq!(
PythonRequest::parse("cpython-3.13.2"),
PythonRequest::Key(PythonDownloadRequest {
version: Some(VersionRequest::MajorMinorPatch(
3,
13,
2,
PythonVariant::Default
)),
implementation: Some(ImplementationName::CPython),
arch: None,
os: None,
libc: None,
prereleases: None
})
);
assert_eq!(
PythonRequest::parse("cpython-3.13.2-macos-aarch64-none"),
PythonRequest::Key(PythonDownloadRequest {
version: Some(VersionRequest::MajorMinorPatch(
3,
13,
2,
PythonVariant::Default
)),
implementation: Some(ImplementationName::CPython),
arch: Some(Arch {
family: Architecture::Aarch64(Aarch64Architecture::Aarch64),
variant: None
}),
os: Some(Os(target_lexicon::OperatingSystem::Darwin(None))),
libc: Some(Libc::None),
prereleases: None
})
);
assert_eq!(
PythonRequest::parse("any-3.13.2"),
PythonRequest::Key(PythonDownloadRequest {
version: Some(VersionRequest::MajorMinorPatch(
3,
13,
2,
PythonVariant::Default
)),
implementation: None,
arch: None,
os: None,
libc: None,
prereleases: None
})
);
assert_eq!(
PythonRequest::parse("any-3.13.2-any-aarch64"),
PythonRequest::Key(PythonDownloadRequest {
version: Some(VersionRequest::MajorMinorPatch(
3,
13,
2,
PythonVariant::Default
)),
implementation: None,
arch: Some(Arch {
family: Architecture::Aarch64(Aarch64Architecture::Aarch64),
variant: None
}),
os: None,
libc: None,
prereleases: None
})
);

assert_eq!(
PythonRequest::parse("pypy"),
PythonRequest::Implementation(ImplementationName::PyPy)
Expand Down
52 changes: 21 additions & 31 deletions crates/uv-python/src/downloads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,15 @@ pub struct ManagedPythonDownload {

#[derive(Debug, Clone, Default, Eq, PartialEq)]
pub struct PythonDownloadRequest {
version: Option<VersionRequest>,
implementation: Option<ImplementationName>,
arch: Option<Arch>,
os: Option<Os>,
libc: Option<Libc>,
pub(crate) version: Option<VersionRequest>,
pub(crate) implementation: Option<ImplementationName>,
pub(crate) arch: Option<Arch>,
pub(crate) os: Option<Os>,
pub(crate) libc: Option<Libc>,

/// Whether to allow pre-releases or not. If not set, defaults to true if [`Self::version`] is
/// not None, and false otherwise.
prereleases: Option<bool>,
pub(crate) prereleases: Option<bool>,
}

impl PythonDownloadRequest {
Expand Down Expand Up @@ -419,39 +419,29 @@ impl FromStr for PythonDownloadRequest {
let mut arch = None;
let mut libc = None;

let mut position = 0;
loop {
// Consume each part
let Some(part) = parts.next() else { break };
position += 1;

if implementation.is_none() {
implementation = Some(ImplementationName::from_str(part)?);
if part.eq_ignore_ascii_case("any") {
continue;
}

if version.is_none() {
version = Some(
VersionRequest::from_str(part)
.map_err(|_| Error::InvalidPythonVersion(part.to_string()))?,
);
continue;
}

if os.is_none() {
os = Some(Os::from_str(part)?);
continue;
}

if arch.is_none() {
arch = Some(Arch::from_str(part)?);
continue;
}

if libc.is_none() {
libc = Some(Libc::from_str(part)?);
continue;
match position {
1 => implementation = Some(ImplementationName::from_str(part)?),
2 => {
version = Some(
VersionRequest::from_str(part)
.map_err(|_| Error::InvalidPythonVersion(part.to_string()))?,
);
}
3 => os = Some(Os::from_str(part)?),
4 => arch = Some(Arch::from_str(part)?),
5 => libc = Some(Libc::from_str(part)?),
_ => return Err(Error::TooManyParts(s.to_string())),
}

return Err(Error::TooManyParts(s.to_string()));
}
Ok(Self::new(version, implementation, arch, os, libc, None))
}
Expand Down
10 changes: 10 additions & 0 deletions crates/uv/tests/it/python_find.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ fn python_find() {
----- stderr -----
"###);

// Request Python 3.12 via partial key syntax with placeholders
uv_snapshot!(context.filters(), context.python_find().arg("any-3.12-any"), @r###"
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously, this would have been treated as an executable name request.

success: true
exit_code: 0
----- stdout -----
[PYTHON-3.12]

----- stderr -----
"###);

// Request CPython 3.12 for the current platform
let os = Os::from_env();
let arch = Arch::from_env();
Expand Down
Loading