Skip to content
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

Default to current Python minor if Requires-Python is absent #4070

Merged
merged 2 commits into from
Jun 5, 2024
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
8 changes: 8 additions & 0 deletions crates/pep440-rs/src/version_specifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,14 @@ impl VersionSpecifier {
}
}

/// `>=<version>`
pub fn greater_than_equal_version(version: Version) -> Self {
Self {
operator: Operator::GreaterThanEqual,
version,
}
}

/// Get the operator, e.g. `>=` in `>= 2.0.0`
pub fn operator(&self) -> &Operator {
&self.operator
Expand Down
18 changes: 9 additions & 9 deletions crates/uv-distribution/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -826,7 +826,7 @@ mod tests {
"root": "[ROOT]/albatross-in-example/examples/bird-feeder",
"project": {
"name": "bird-feeder",
"requires-python": null,
"requires-python": ">=3.12",
"optional-dependencies": null
},
"pyproject_toml": "[PYPROJECT_TOML]"
Expand Down Expand Up @@ -861,7 +861,7 @@ mod tests {
"root": "[ROOT]/albatross-project-in-excluded/excluded/bird-feeder",
"project": {
"name": "bird-feeder",
"requires-python": null,
"requires-python": ">=3.12",
"optional-dependencies": null
},
"pyproject_toml": "[PYPROJECT_TOML]"
Expand Down Expand Up @@ -895,7 +895,7 @@ mod tests {
"root": "[ROOT]/albatross-root-workspace",
"project": {
"name": "albatross",
"requires-python": null,
"requires-python": ">=3.12",
"optional-dependencies": null
},
"pyproject_toml": "[PYPROJECT_TOML]"
Expand All @@ -904,7 +904,7 @@ mod tests {
"root": "[ROOT]/albatross-root-workspace/packages/bird-feeder",
"project": {
"name": "bird-feeder",
"requires-python": null,
"requires-python": ">=3.12",
"optional-dependencies": null
},
"pyproject_toml": "[PYPROJECT_TOML]"
Expand All @@ -913,7 +913,7 @@ mod tests {
"root": "[ROOT]/albatross-root-workspace/packages/seeds",
"project": {
"name": "seeds",
"requires-python": null,
"requires-python": ">=3.12",
"optional-dependencies": null
},
"pyproject_toml": "[PYPROJECT_TOML]"
Expand Down Expand Up @@ -953,7 +953,7 @@ mod tests {
"root": "[ROOT]/albatross-virtual-workspace/packages/albatross",
"project": {
"name": "albatross",
"requires-python": null,
"requires-python": ">=3.12",
"optional-dependencies": null
},
"pyproject_toml": "[PYPROJECT_TOML]"
Expand All @@ -962,7 +962,7 @@ mod tests {
"root": "[ROOT]/albatross-virtual-workspace/packages/bird-feeder",
"project": {
"name": "bird-feeder",
"requires-python": null,
"requires-python": ">=3.12",
"optional-dependencies": null
},
"pyproject_toml": "[PYPROJECT_TOML]"
Expand All @@ -971,7 +971,7 @@ mod tests {
"root": "[ROOT]/albatross-virtual-workspace/packages/seeds",
"project": {
"name": "seeds",
"requires-python": null,
"requires-python": ">=3.12",
"optional-dependencies": null
},
"pyproject_toml": "[PYPROJECT_TOML]"
Expand Down Expand Up @@ -1005,7 +1005,7 @@ mod tests {
"root": "[ROOT]/albatross-just-project",
"project": {
"name": "albatross",
"requires-python": null,
"requires-python": ">=3.12",
"optional-dependencies": null
},
"pyproject_toml": "[PYPROJECT_TOML]"
Expand Down
14 changes: 10 additions & 4 deletions crates/uv-interpreter/src/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,16 +209,22 @@ impl Interpreter {
Some(ExternallyManaged { error })
}

/// Returns the Python version.
/// Returns the `python_full_version` marker corresponding to this Python version.
#[inline]
pub fn python_full_version(&self) -> &StringVersion {
self.markers.python_full_version()
}

/// Returns the full Python version.
#[inline]
pub fn python_version(&self) -> &Version {
&self.markers.python_full_version().version
}

/// Returns the `python_full_version` marker corresponding to this Python version.
/// Returns the full minor Python version.
#[inline]
pub fn python_full_version(&self) -> &StringVersion {
self.markers.python_full_version()
pub fn python_minor_version(&self) -> Version {
Version::new(self.python_version().release().iter().take(2).copied())
}

/// Return the major version of this Python version.
Expand Down
22 changes: 20 additions & 2 deletions crates/uv/src/commands/project/lock.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use anstream::eprint;
use anyhow::Result;
use std::borrow::Cow;

use distribution_types::{IndexLocations, UnresolvedRequirementSpecification};
use install_wheel_rs::linker::LinkMode;
use pep440_rs::{VersionSpecifier, VersionSpecifiers};
use uv_cache::Cache;
use uv_client::RegistryClientBuilder;
use uv_configuration::{
Expand Down Expand Up @@ -91,11 +93,27 @@ pub(super) async fn do_lock(
let source_trees = vec![];
let project_name = project.project_name().clone();

// Determine the supported Python range. If no range is defined, and warn and default to the
// current minor version.
let requires_python = if let Some(requires_python) =
project.current_project().project().requires_python.as_ref()
{
Cow::Borrowed(requires_python)
} else {
let requires_python = VersionSpecifiers::from(
VersionSpecifier::greater_than_equal_version(venv.interpreter().python_minor_version()),
);
warn_user!(
"No `requires-python` field found in `{}`. Defaulting to `{requires_python}`.",
project.current_project().project().name,
);
Cow::Owned(requires_python)
};

// Determine the tags, markers, and interpreter to use for resolution.
let interpreter = venv.interpreter();
let tags = venv.interpreter().tags()?;
let markers = venv.interpreter().markers();
let requires_python = project.current_project().project().requires_python.as_ref();

// Initialize the registry client.
// TODO(zanieb): Support client options e.g. offline, tls, etc.
Expand Down Expand Up @@ -164,7 +182,7 @@ pub(super) async fn do_lock(
interpreter,
tags,
None,
requires_python,
Some(&requires_python),
&client,
&flat_index,
&index,
Expand Down
22 changes: 22 additions & 0 deletions crates/uv/tests/lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ fn lock_wheel_registry() -> Result<()> {
[project]
name = "project"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = ["anyio==3.7.0"]
"#,
)?;
Expand All @@ -41,6 +42,7 @@ fn lock_wheel_registry() -> Result<()> {
assert_snapshot!(
lock, @r###"
version = 1
requires-python = ">=3.12"

[[distribution]]
name = "anyio"
Expand Down Expand Up @@ -143,6 +145,7 @@ fn lock_sdist_registry() -> Result<()> {
[project]
name = "project"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = ["source-distribution==0.0.1"]
"#,
)?;
Expand All @@ -165,6 +168,7 @@ fn lock_sdist_registry() -> Result<()> {
assert_snapshot!(
lock, @r###"
version = 1
requires-python = ">=3.12"

[[distribution]]
name = "project"
Expand Down Expand Up @@ -200,6 +204,7 @@ fn lock_sdist_git() -> Result<()> {
[project]
name = "project"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = ["uv-public-pypackage @ git+https://github.com/astral-test/uv-public-pypackage@0.0.1"]
"#,
)?;
Expand All @@ -222,6 +227,7 @@ fn lock_sdist_git() -> Result<()> {
assert_snapshot!(
lock, @r###"
version = 1
requires-python = ">=3.12"

[[distribution]]
name = "project"
Expand Down Expand Up @@ -271,6 +277,7 @@ fn lock_wheel_url() -> Result<()> {
[project]
name = "project"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = ["anyio @ https://files.pythonhosted.org/packages/14/fd/2f20c40b45e4fb4324834aea24bd4afdf1143390242c0b33774da0e2e34f/anyio-4.3.0-py3-none-any.whl"]
"#,
)?;
Expand All @@ -293,6 +300,7 @@ fn lock_wheel_url() -> Result<()> {
assert_snapshot!(
lock, @r###"
version = 1
requires-python = ">=3.12"

[[distribution]]
name = "anyio"
Expand Down Expand Up @@ -394,6 +402,7 @@ fn lock_sdist_url() -> Result<()> {
[project]
name = "project"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = ["anyio @ https://files.pythonhosted.org/packages/db/4d/3970183622f0330d3c23d9b8a5f52e365e50381fd484d08e3285104333d3/anyio-4.3.0.tar.gz"]
"#,
)?;
Expand All @@ -416,6 +425,7 @@ fn lock_sdist_url() -> Result<()> {
assert_snapshot!(
lock, @r###"
version = 1
requires-python = ">=3.12"

[[distribution]]
name = "anyio"
Expand Down Expand Up @@ -517,6 +527,7 @@ fn lock_extra() -> Result<()> {
[project]
name = "project"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = ["anyio==3.7.0"]

[project.optional-dependencies]
Expand All @@ -542,6 +553,7 @@ fn lock_extra() -> Result<()> {
assert_snapshot!(
lock, @r###"
version = 1
requires-python = ">=3.12"

[[distribution]]
name = "anyio"
Expand Down Expand Up @@ -671,6 +683,7 @@ fn lock_preference() -> Result<()> {
[project]
name = "project"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = ["iniconfig<2"]
"#,
)?;
Expand All @@ -693,6 +706,7 @@ fn lock_preference() -> Result<()> {
assert_snapshot!(
lock, @r###"
version = 1
requires-python = ">=3.12"

[[distribution]]
name = "iniconfig"
Expand Down Expand Up @@ -721,6 +735,7 @@ fn lock_preference() -> Result<()> {
[project]
name = "project"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = ["iniconfig"]
"#,
)?;
Expand All @@ -744,6 +759,7 @@ fn lock_preference() -> Result<()> {
assert_snapshot!(
lock, @r###"
version = 1
requires-python = ">=3.12"

[[distribution]]
name = "iniconfig"
Expand Down Expand Up @@ -785,6 +801,7 @@ fn lock_preference() -> Result<()> {
assert_snapshot!(
lock, @r###"
version = 1
requires-python = ">=3.12"

[[distribution]]
name = "iniconfig"
Expand Down Expand Up @@ -822,6 +839,7 @@ fn lock_git_sha() -> Result<()> {
[project]
name = "project"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = ["uv-public-pypackage @ git+https://github.com/astral-test/uv-public-pypackage@0dacfd662c64cb4ceb16e6cf65a157a8b715b979"]
"#,
)?;
Expand All @@ -844,6 +862,7 @@ fn lock_git_sha() -> Result<()> {
assert_snapshot!(
lock, @r###"
version = 1
requires-python = ">=3.12"

[[distribution]]
name = "project"
Expand Down Expand Up @@ -876,6 +895,7 @@ fn lock_git_sha() -> Result<()> {
[project]
name = "project"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = ["uv-public-pypackage @ git+https://github.com/astral-test/uv-public-pypackage@main"]
"#,
)?;
Expand All @@ -900,6 +920,7 @@ fn lock_git_sha() -> Result<()> {
assert_snapshot!(
lock, @r###"
version = 1
requires-python = ">=3.12"

[[distribution]]
name = "project"
Expand Down Expand Up @@ -942,6 +963,7 @@ fn lock_git_sha() -> Result<()> {
assert_snapshot!(
lock, @r###"
version = 1
requires-python = ">=3.12"

[[distribution]]
name = "project"
Expand Down
Loading
Loading