Improve major-minor bounds on requires-python (#8145)

This commit is contained in:
Charlie Marsh 2024-10-12 16:48:03 +02:00 committed by GitHub
parent b12d5b619b
commit 346050bf99
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 22 additions and 18 deletions

View file

@ -553,13 +553,11 @@ impl LowerBound {
/// Return the [`LowerBound`] truncated to the major and minor version.
fn major_minor(&self) -> Self {
match &self.0 {
// Ex) `>=3.10.1` -> `>=3.10` (and `>=3.10.0` is `>=3.10`)
// Ex) `>=3.10.1` -> `>=3.10`
Bound::Included(version) => Self(Bound::Included(Version::new(
version.release().iter().take(2),
))),
// Ex) `>3.10.1` -> `>=3.10`.
// This is unintuitive, but `>3.10.1` does indicate that _some_ version of Python 3.10
// is supported.
Bound::Excluded(version) => Self(Bound::Included(Version::new(
version.release().iter().take(2),
))),
@ -668,24 +666,20 @@ impl UpperBound {
/// Return the [`UpperBound`] truncated to the major and minor version.
fn major_minor(&self) -> Self {
match &self.0 {
// Ex) `<=3.10.1` -> `<3.11` (but `<=3.10.0` is `<=3.10`)
Bound::Included(version) => {
let major = version.release().first().copied().unwrap_or(3);
let minor = version.release().get(1).copied().unwrap_or(0);
if version.release().get(2).is_some_and(|patch| *patch > 0) {
Self(Bound::Excluded(Version::new([major, minor + 1])))
} else {
Self(Bound::Included(Version::new([major, minor])))
}
}
// Ex) `<3.10.1` -> `<3.11` (but `<3.10.0` is `<3.10`)
// Ex) `<=3.10.1` -> `<=3.10`
Bound::Included(version) => Self(Bound::Included(Version::new(
version.release().iter().take(2),
))),
// Ex) `<3.10.1` -> `<=3.10` (but `<3.10.0` is `<3.10`)
Bound::Excluded(version) => {
let major = version.release().first().copied().unwrap_or(3);
let minor = version.release().get(1).copied().unwrap_or(0);
if version.release().get(2).is_some_and(|patch| *patch > 0) {
Self(Bound::Excluded(Version::new([major, minor + 1])))
Self(Bound::Included(Version::new(
version.release().iter().take(2),
)))
} else {
Self(Bound::Excluded(Version::new([major, minor])))
Self(Bound::Excluded(Version::new(
version.release().iter().take(2),
)))
}
}
Bound::Unbounded => Self(Bound::Unbounded),

View file

@ -39,6 +39,16 @@ fn requires_python_included() {
);
}
let version_specifiers = VersionSpecifiers::from_str("==3.12.6").unwrap();
let requires_python = RequiresPython::from_specifiers(&version_specifiers).unwrap();
let wheel_names = &["lxml-5.3.0-cp312-cp312-musllinux_1_2_aarch64.whl"];
for wheel_name in wheel_names {
assert!(
requires_python.matches_wheel_tag(&WheelFilename::from_str(wheel_name).unwrap()),
"{wheel_name}"
);
}
let version_specifiers = VersionSpecifiers::from_str("==3.12").unwrap();
let requires_python = RequiresPython::from_specifiers(&version_specifiers).unwrap();
let wheel_names = &["lxml-5.3.0-cp312-cp312-musllinux_1_2_x86_64.whl"];