Narrow requires-python requirement in resolver forks (#4707)

## Summary

Given:

```text
numpy >=1.26 ; python_version >= '3.9'
numpy <1.26 ; python_version < '3.9'
```

When resolving for Python 3.8, we need to narrow the `requires-python`
requirement in the top branch of the fork, because `numpy >=1.26` all
require Python 3.9 or later -- but we know (in that branch) that we only
need to _solve_ for Python 3.9 or later.

Closes https://github.com/astral-sh/uv/issues/4669.
This commit is contained in:
Charlie Marsh 2024-07-02 08:23:38 -04:00 committed by GitHub
parent 89b3324ae1
commit d9f389a58d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 251 additions and 58 deletions

View file

@ -2,7 +2,7 @@ use pep440_rs::VersionSpecifiers;
use pep508_rs::{MarkerTree, StringVersion};
use uv_toolchain::{Interpreter, PythonVersion};
use crate::RequiresPython;
use crate::{RequiresPython, RequiresPythonBound};
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct PythonRequirement {
@ -49,6 +49,19 @@ impl PythonRequirement {
}
}
/// Narrow the [`PythonRequirement`] to the given version, if it's stricter (i.e., greater)
/// than the current `Requires-Python` minimum.
pub fn narrow(&self, target: &RequiresPythonBound) -> Option<Self> {
let Some(PythonTarget::RequiresPython(requires_python)) = self.target.as_ref() else {
return None;
};
let requires_python = requires_python.narrow(target)?;
Some(Self {
installed: self.installed.clone(),
target: Some(PythonTarget::RequiresPython(requires_python)),
})
}
/// Return the installed version of Python.
pub fn installed(&self) -> &StringVersion {
&self.installed