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
13 changes: 13 additions & 0 deletions crates/uv-resolver/src/lock/export/pylock_toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ use crate::{Installable, LockError, ResolverOutput};

#[derive(Debug, thiserror::Error)]
pub enum PylockTomlErrorKind {
#[error("Package `{0}` requires Python {2}, but the target Python version is {1}")]
IncompatibleRequiresPython(PackageName, Version, RequiresPython),
#[error(
"Package `{0}` includes both a registry (`packages.wheels`) and a directory source (`packages.directory`)"
)]
Expand Down Expand Up @@ -1045,6 +1047,17 @@ impl<'lock> PylockToml {
continue;
}

if let Some(requires_python) = package.requires_python.as_ref()
&& !requires_python.contains(&markers.python_full_version().version)
{
return Err(PylockTomlErrorKind::IncompatibleRequiresPython(
package.name.clone(),
markers.python_full_version().version.clone(),
requires_python.clone(),
)
.into());
}

match (
package.wheels.is_some(),
package.sdist.is_some(),
Expand Down
60 changes: 60 additions & 0 deletions crates/uv/tests/pip_install/pip_install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13393,6 +13393,66 @@ requires_python = "==3.13.*"
Ok(())
}

#[test]
fn pep_751_package_requires_python() -> Result<()> {
let context = uv_test::test_context!("3.12");

let pylock_toml = context.temp_dir.child("pylock.toml");
pylock_toml.write_str(
r#"
lock-version = "1.0"
created-by = "uv"

[[packages]]
name = "example"
marker = "python_version < '3.0'"
requires-python = ">=99"
directory = { path = "." }
"#,
)?;

// Skip the Python requirement for packages excluded by their marker.
uv_snapshot!(context.filters(), context.pip_install()
.arg("--preview")
.arg("-r")
.arg("pylock.toml"), @r#"
success: true
exit_code: 0
----- stdout -----

----- stderr -----
Checked in [TIME]
"#
);

pylock_toml.write_str(
r#"
lock-version = "1.0"
created-by = "uv"

[[packages]]
name = "example"
requires-python = ">=99"
directory = { path = "." }
"#,
)?;

uv_snapshot!(context.filters(), context.pip_install()
.arg("--preview")
.arg("-r")
.arg("pylock.toml"), @r#"
success: false
exit_code: 2
----- stdout -----

----- stderr -----
error: Package `example` requires Python >=99, but the target Python version is 3.12.[X]
"#
);

Ok(())
}

#[test]
fn pep_751_requires_python() -> Result<()> {
let context = uv_test::test_context_with_versions!(&["3.12", "3.13"]);
Expand Down
Loading