Skip to content

Commit

Permalink
Correctly handle trailing whitespace on URL requirements
Browse files Browse the repository at this point in the history
The input is permitted to end, after trailing whitespace.
  • Loading branch information
pradyunsg committed Dec 14, 2022
1 parent 150c9a6 commit ddf54e1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
6 changes: 5 additions & 1 deletion src/packaging/_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def _parse_requirement_details(
tokenizer: Tokenizer,
) -> Tuple[str, str, Optional[MarkerList]]:
"""
requirement_details = AT URL (WS requirement_marker)?
requirement_details = AT URL (WS requirement_marker?)?
| specifier WS? (requirement_marker)?
"""

Expand All @@ -108,6 +108,10 @@ def _parse_requirement_details(

tokenizer.expect("WS", expected="whitespace after URL")

# The input might end after whitespace.
if tokenizer.check("END", peek=True):
return (url, specifier, marker)

marker = _parse_requirement_marker(
tokenizer, span_start=url_start, after="URL and whitespace"
)
Expand Down
6 changes: 3 additions & 3 deletions tests/test_requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -581,14 +581,14 @@ def test_str_and_repr(
self, extras: str, url_or_specifier: str, marker: str
) -> None:
# GIVEN
to_parse = f"name{extras}{url_or_specifier}{marker}".strip()
to_parse = f"name{extras}{url_or_specifier}{marker}"

# WHEN
req = Requirement(to_parse)

# THEN
assert str(req) == to_parse
assert repr(req) == f"<Requirement({to_parse!r})>"
assert str(req) == to_parse.strip()
assert repr(req) == f"<Requirement({to_parse.strip()!r})>"

@pytest.mark.parametrize("dep1, dep2", EQUAL_DEPENDENCIES)
def test_equal_reqs_equal_hashes(self, dep1: str, dep2: str) -> None:
Expand Down

0 comments on commit ddf54e1

Please sign in to comment.