Track supported Python range in lockfile (#4065)

## Summary

This PR adds the `Requires-Python` range to the user's lockfile. This
will enable us to validate it when installing.

For now, we repeat the `Requires-Python` back to the user;
alternatively, though, we could detect the supported Python range
automatically.

See: https://github.com/astral-sh/uv/issues/4052
This commit is contained in:
Charlie Marsh 2024-06-05 16:21:59 -04:00 committed by GitHub
parent 8596525d97
commit 642cef0dad
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 58 additions and 9 deletions

View file

@ -60,13 +60,14 @@ impl PythonRequirement {
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum RequiresPython {
/// The `RequiresPython` specifier is a single version specifier, as provided via
/// The [`RequiresPython`] specifier is a single version specifier, as provided via
/// `--python-version` on the command line.
///
/// The use of a separate enum variant allows us to use a verbatim representation when reporting
/// back to the user.
Specifier(StringVersion),
/// The `RequiresPython` specifier is a set of version specifiers.
/// The [`RequiresPython`] specifier is a set of version specifiers, as extracted from the
/// `Requires-Python` field in a `pyproject.toml` or `METADATA` file.
Specifiers(VersionSpecifiers),
}
@ -93,6 +94,14 @@ impl RequiresPython {
}
}
}
/// Returns the [`VersionSpecifiers`] for the [`RequiresPython`] specifier.
pub fn as_specifiers(&self) -> Option<&VersionSpecifiers> {
match self {
RequiresPython::Specifier(_) => None,
RequiresPython::Specifiers(specifiers) => Some(specifiers),
}
}
}
impl std::fmt::Display for RequiresPython {