Skip to content

Avoid panic for invalid Python versions #13077

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

Merged
merged 1 commit into from
Apr 23, 2025
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
21 changes: 21 additions & 0 deletions crates/uv-python/src/python_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,27 @@ impl FromStr for PythonVersion {
if version.epoch() != 0 {
return Err(format!("Python version `{s}` has a non-zero epoch"));
}
if let Some(major) = version.release().first() {
if u8::try_from(*major).is_err() {
return Err(format!(
"Python version `{s}` has an invalid major version ({major})"
));
}
}
if let Some(minor) = version.release().get(1) {
if u8::try_from(*minor).is_err() {
return Err(format!(
"Python version `{s}` has an invalid minor version ({minor})"
));
}
}
if let Some(patch) = version.release().get(2) {
if u8::try_from(*patch).is_err() {
return Err(format!(
"Python version `{s}` has an invalid patch version ({patch})"
));
}
}

Ok(Self(version))
}
Expand Down
20 changes: 20 additions & 0 deletions crates/uv/tests/it/pip_install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,26 @@ dependencies = ["flask==1.0.x"]
Ok(())
}

#[test]
fn invalid_python_version() {
let context = TestContext::new("3.12");

uv_snapshot!(context.filters(), context.pip_install()
.arg("flask")
.arg("--python-version")
.arg("311"), @r"
success: false
exit_code: 2
----- stdout -----

----- stderr -----
error: invalid value '311' for '--python-version <PYTHON_VERSION>': Python version `311` has an invalid major version (311)

For more information, try '--help'.
"
);
}

#[test]
fn missing_pip() {
uv_snapshot!(Command::new(get_bin()).arg("install"), @r###"
Expand Down
Loading