mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-28 04:45:01 +00:00
Make requires-python
inference robust to ==
(#12091)
## Summary Instead of using a high patch version, attempt to detect the minimum-supported minor. Closes #12088.
This commit is contained in:
parent
434ce307a7
commit
c326778652
2 changed files with 217 additions and 12 deletions
|
@ -9,7 +9,8 @@ use std::string::ToString;
|
|||
|
||||
use anyhow::{bail, Result};
|
||||
use globset::{Glob, GlobMatcher, GlobSet, GlobSetBuilder};
|
||||
use pep440_rs::{Version as Pep440Version, VersionSpecifier, VersionSpecifiers};
|
||||
use log::debug;
|
||||
use pep440_rs::{Operator, Version as Pep440Version, Version, VersionSpecifier, VersionSpecifiers};
|
||||
use rustc_hash::FxHashMap;
|
||||
use serde::{de, Deserialize, Deserializer, Serialize};
|
||||
use strum::IntoEnumIterator;
|
||||
|
@ -59,7 +60,7 @@ pub enum PythonVersion {
|
|||
impl From<PythonVersion> for Pep440Version {
|
||||
fn from(version: PythonVersion) -> Self {
|
||||
let (major, minor) = version.as_tuple();
|
||||
Self::from_str(&format!("{major}.{minor}.100")).unwrap()
|
||||
Self::new([u64::from(major), u64::from(minor)])
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -89,18 +90,36 @@ impl PythonVersion {
|
|||
self.as_tuple().1
|
||||
}
|
||||
|
||||
/// Infer the minimum supported [`PythonVersion`] from a `requires-python` specifier.
|
||||
pub fn get_minimum_supported_version(requires_version: &VersionSpecifiers) -> Option<Self> {
|
||||
let mut minimum_version = None;
|
||||
for python_version in PythonVersion::iter() {
|
||||
if requires_version
|
||||
.iter()
|
||||
.all(|specifier| specifier.contains(&python_version.into()))
|
||||
{
|
||||
minimum_version = Some(python_version);
|
||||
break;
|
||||
}
|
||||
/// Truncate a version to its major and minor components.
|
||||
fn major_minor(version: &Version) -> Option<Version> {
|
||||
let major = version.release().first()?;
|
||||
let minor = version.release().get(1)?;
|
||||
Some(Version::new([major, minor]))
|
||||
}
|
||||
minimum_version
|
||||
|
||||
// Extract the minimum supported version from the specifiers.
|
||||
let minimum_version = requires_version
|
||||
.iter()
|
||||
.filter(|specifier| {
|
||||
matches!(
|
||||
specifier.operator(),
|
||||
Operator::Equal
|
||||
| Operator::EqualStar
|
||||
| Operator::ExactEqual
|
||||
| Operator::TildeEqual
|
||||
| Operator::GreaterThan
|
||||
| Operator::GreaterThanEqual
|
||||
)
|
||||
})
|
||||
.filter_map(|specifier| major_minor(specifier.version()))
|
||||
.min()?;
|
||||
|
||||
debug!("Detected minimum supported `requires-python` version: {minimum_version}");
|
||||
|
||||
// Find the Python version that matches the minimum supported version.
|
||||
PythonVersion::iter().find(|version| Version::from(*version) == minimum_version)
|
||||
}
|
||||
|
||||
/// Return `true` if the current version supports [PEP 701].
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue