Skip to content

Commit 73c9555

Browse files
authored
Allow whitespace in pip requirement specifiers (#1530)
A bug was discovered recently when attempting to parse a `pip` lockfile. That is, a `requirements*.txt` file containing exact versions (i.e., `==`) in all of it's requirement specifiers. Such a file should parse as a lockfile and not fall back to lockfile generation. However, if whitespace is included around the `==`, the parsing fails. Whitespace is allowed in requirement specifiers. This change accounts for that. It also updates the test fixture to prevent regressions. Ref: https://pip.pypa.io/en/stable/reference/requirement-specifiers/
1 parent dacd07d commit 73c9555

File tree

3 files changed

+7
-2
lines changed

3 files changed

+7
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
88

99
## Unreleased
1010

11+
### Fixed
12+
13+
- `pip` parser failing with whitespace around `==` in requirement specifier
14+
1115
## 7.1.3 - 2024-10-29
1216

1317
### Fixed

lockfile/src/parsers/pypi.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ fn package_version(input: &str) -> IResult<&str, &str> {
187187
let (input, _) = tag("==")(input)?;
188188

189189
// Take all valid semver character.
190-
recognize(many1(alt((alphanumeric1, tag(".")))))(input)
190+
recognize(many1(alt((alphanumeric1, tag(".")))))(input.trim())
191191
}
192192

193193
/// Parse local version specifiers.

tests/fixtures/requirements-locked.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ amqp==5.0.9 --hash=sha256:77fd4e1249d8c9923de34907236b747ced06e5467ecac1a7bb7115
1111
--hash=sha256:8c2f9abd47a9e8df7f0c3f091ce9497d011dc3b31effcf4c85a6e2b50f4114ef
1212
# via kombu
1313

14-
attrs==20.2.0 # This is an inline comment
14+
# Whitespace is allowed in requirement specifiers
15+
attrs == 20.2.0 # This is an inline comment
1516

1617
# The requirement specifier does not have to be at the beginning of the line
1718
flask==2.2.2

0 commit comments

Comments
 (0)