Skip to content

Commit

Permalink
Improve error message for bad version specifiers in Requirement
Browse files Browse the repository at this point in the history
This makes it easier to understand what the state of the parser is and
what is expected at that point.
  • Loading branch information
pradyunsg committed Dec 7, 2022
1 parent 258d252 commit 7930b73
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
8 changes: 7 additions & 1 deletion packaging/_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,13 @@ def _parse_requirement_details(
return (url, specifier, marker)

marker = _parse_requirement_marker(
tokenizer, span_start=specifier_start, after="version specifier"
tokenizer,
span_start=specifier_start,
after=(
"version specifier"
if specifier
else "name and no valid version specifier"
),
)

return (url, specifier, marker)
Expand Down
18 changes: 17 additions & 1 deletion tests/test_requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,11 +489,27 @@ def test_error_on_missing_op_after_name(self) -> None:
# THEN
assert ctx.exconly() == (
"packaging.requirements.InvalidRequirement: "
"Expected end or semicolon (after version specifier)\n"
"Expected end or semicolon (after name and no valid version specifier)\n"
" name 1.0\n"
" ^"
)

def test_error_on_random_char_after_specifier(self) -> None:
# GIVEN
to_parse = "name >= 1.0 #"

# WHEN
with pytest.raises(InvalidRequirement) as ctx:
Requirement(to_parse)

# THEN
assert ctx.exconly() == (
"packaging.requirements.InvalidRequirement: "
"Expected end or semicolon (after version specifier)\n"
" name >= 1.0 #\n"
" ~~~~~~~^"
)


class TestRequirementBehaviour:
def test_types_with_nothing(self) -> None:
Expand Down

0 comments on commit 7930b73

Please sign in to comment.