Add requires-python to uv init (#5322)

## Summary

Prefers, in order:

- The major-minor version of an interpreter discovered via `--python`.
- The `requires-python` from the workspace.
- The major-minor version of the default interpreter.

If the `--python` request is a version or a version range, we use that
without fetching an interpreter.

Closes https://github.com/astral-sh/uv/issues/5299.
This commit is contained in:
Charlie Marsh
2024-07-23 12:02:40 -04:00
committed by GitHub
parent c8ac8ee57a
commit 0f8186d9ad
16 changed files with 461 additions and 103 deletions
+15
View File
@@ -49,6 +49,21 @@ impl RequiresPython {
}
}
/// Returns a [`RequiresPython`] from a version specifier.
pub fn from_specifiers(specifiers: &VersionSpecifiers) -> Result<Self, RequiresPythonError> {
let bound = RequiresPythonBound(
crate::pubgrub::PubGrubSpecifier::from_release_specifiers(specifiers)?
.iter()
.next()
.map(|(lower, _)| lower.clone())
.unwrap_or(Bound::Unbounded),
);
Ok(Self {
specifiers: specifiers.clone(),
bound,
})
}
/// Returns a [`RequiresPython`] to express the union of the given version specifiers.
///
/// For example, given `>=3.8` and `>=3.9`, this would return `>=3.8`.