Fix parsing requirement where a variable follows an operator without a space (#2273)

Fix parsing `pytest;'4.0'>=python_version`, where previously the
operator and the variable were incorrectly tokenized as one invalid
operator.

Fixes #2247
This commit is contained in:
konsti 2024-03-07 13:22:16 +01:00 committed by GitHub
parent 85042308a1
commit 953a13f12e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 14 additions and 2 deletions

View file

@ -1710,4 +1710,10 @@ mod tests {
);
}
}
#[test]
fn no_space_after_operator() {
let requirement = Requirement::from_str("pytest;'4.0'>=python_version").unwrap();
assert_eq!(requirement.to_string(), "pytest ; '4.0' >= python_version");
}
}

View file

@ -1150,9 +1150,15 @@ impl Display for MarkerTree {
/// version_cmp = wsp* <'<=' | '<' | '!=' | '==' | '>=' | '>' | '~=' | '==='>
/// marker_op = version_cmp | (wsp* 'in') | (wsp* 'not' wsp+ 'in')
/// ```
/// The `wsp*` has already been consumed by the caller.
fn parse_marker_operator(cursor: &mut Cursor) -> Result<MarkerOperator, Pep508Error> {
let (start, len) =
cursor.take_while(|char| !char.is_whitespace() && char != '\'' && char != '"');
let (start, len) = if cursor.peek_char().is_some_and(|c| c.is_alphabetic()) {
// "in" or "not"
cursor.take_while(|char| !char.is_whitespace() && char != '\'' && char != '"')
} else {
// A mathematical operator
cursor.take_while(|char| matches!(char, '<' | '=' | '>' | '~' | '!'))
};
let operator = cursor.slice(start, len);
if operator == "not" {
// 'not' wsp+ 'in'