mirror of
https://github.com/astral-sh/uv.git
synced 2025-07-23 05:05:02 +00:00
Add missing-dot fix to lenient requirements (#416)
Part of https://github.com/astral-sh/puffin/issues/408.
This commit is contained in:
parent
0af2f7e39f
commit
582c94cec3
1 changed files with 30 additions and 4 deletions
|
@ -211,10 +211,14 @@ impl Metadata21 {
|
|||
}
|
||||
}
|
||||
|
||||
/// Ex) `>=7.2.0<8.0.0`
|
||||
static MISSING_COMMA: Lazy<Regex> = Lazy::new(|| Regex::new(r"(\d)([<>=~^!])").unwrap());
|
||||
/// Ex) `!=~5.0`
|
||||
static NOT_EQUAL_TILDE: Lazy<Regex> = Lazy::new(|| Regex::new(r"!=~((?:\d\.)*\d)").unwrap());
|
||||
/// e.g. `>=1.9.*`
|
||||
/// Ex) `>=1.9.*`
|
||||
static GREATER_THAN_STAR: Lazy<Regex> = Lazy::new(|| Regex::new(r">=(\d+\.\d+)\.\*").unwrap());
|
||||
/// Ex) `!=3.0*`
|
||||
static MISSING_DOT: Lazy<Regex> = Lazy::new(|| Regex::new(r"(\d\.\d)+\*").unwrap());
|
||||
|
||||
/// Like [`Requirement`], but attempts to correct some common errors in user-provided requirements.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq)]
|
||||
|
@ -228,7 +232,7 @@ impl FromStr for LenientRequirement {
|
|||
Ok(requirement) => Ok(Self(requirement)),
|
||||
Err(err) => {
|
||||
// Given `elasticsearch-dsl (>=7.2.0<8.0.0)`, rewrite to `elasticsearch-dsl (>=7.2.0,<8.0.0)`.
|
||||
let patched = MISSING_COMMA.replace(s, r"$1,$2");
|
||||
let patched = MISSING_COMMA.replace_all(s, r"$1,$2");
|
||||
if patched != s {
|
||||
if let Ok(requirement) = Requirement::from_str(&patched) {
|
||||
warn!(
|
||||
|
@ -239,7 +243,7 @@ impl FromStr for LenientRequirement {
|
|||
}
|
||||
|
||||
// Given `jupyter-core (!=~5.0,>=4.12)`, rewrite to `jupyter-core (!=5.0.*,>=4.12)`.
|
||||
let patched = NOT_EQUAL_TILDE.replace(s, r"!=${1}.*");
|
||||
let patched = NOT_EQUAL_TILDE.replace_all(s, r"!=${1}.*");
|
||||
if patched != s {
|
||||
if let Ok(requirement) = Requirement::from_str(&patched) {
|
||||
warn!(
|
||||
|
@ -250,7 +254,7 @@ impl FromStr for LenientRequirement {
|
|||
}
|
||||
|
||||
// Given `torch (>=1.9.*)`, rewrite to `torch (>=1.9)`
|
||||
let patched = GREATER_THAN_STAR.replace(s, r">=${1}");
|
||||
let patched = GREATER_THAN_STAR.replace_all(s, r">=${1}");
|
||||
if patched != s {
|
||||
if let Ok(requirement) = Requirement::from_str(&patched) {
|
||||
warn!(
|
||||
|
@ -260,6 +264,17 @@ impl FromStr for LenientRequirement {
|
|||
}
|
||||
}
|
||||
|
||||
// Given `pyzmq (!=3.0*)`, rewrite to `pyzmq (!=3.0.*)`
|
||||
let patched = MISSING_DOT.replace_all(s, r"${1}.*");
|
||||
if patched != s {
|
||||
if let Ok(requirement) = Requirement::from_str(&patched) {
|
||||
warn!(
|
||||
"Inserting missing dot into invalid requirement (before: `{s}`; after: `{patched}`)",
|
||||
);
|
||||
return Ok(Self(requirement));
|
||||
}
|
||||
}
|
||||
|
||||
Err(err)
|
||||
}
|
||||
}
|
||||
|
@ -313,4 +328,15 @@ mod tests {
|
|||
let expected: Requirement = Requirement::from_str("torch (>=1.9)").unwrap();
|
||||
assert_eq!(actual, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn missing_dot() {
|
||||
let actual: Requirement =
|
||||
LenientRequirement::from_str("pyzmq (>=2.7,!=3.0*,!=3.1*,!=3.2*)")
|
||||
.unwrap()
|
||||
.into();
|
||||
let expected: Requirement =
|
||||
Requirement::from_str("pyzmq (>=2.7,!=3.0.*,!=3.1.*,!=3.2.*)").unwrap();
|
||||
assert_eq!(actual, expected);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue