Skip to content

Commit

Permalink
Add dedicated error for specifiers with incorrect .* suffix
Browse files Browse the repository at this point in the history
This should make it clearer how and why this is not valid, thus
providing relevant context to readers.
  • Loading branch information
pradyunsg committed Jan 16, 2023
1 parent f4463e2 commit 068a0b5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/packaging/_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,14 @@ def _parse_version_many(tokenizer: Tokenizer) -> str:
"""
parsed_specifiers = ""
while tokenizer.check("SPECIFIER"):
span_start = tokenizer.position
parsed_specifiers += tokenizer.read().text
if tokenizer.check("VERSION_PREFIX_TRAIL", peek=True):
tokenizer.raise_syntax_error(
".* suffix can only be used with `==` or `!=` operators",
span_start=span_start,
span_end=tokenizer.position + 1,
)
tokenizer.consume("WS")
if not tokenizer.check("COMMA"):
break
Expand Down
1 change: 1 addition & 0 deletions src/packaging/_tokenizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ def __str__(self) -> str:
"AT": r"\@",
"URL": r"[^ \t]+",
"IDENTIFIER": r"\b[a-zA-Z0-9][a-zA-Z0-9._-]*\b",
"VERSION_PREFIX_TRAIL": r"\.\*",
"WS": r"[ \t]+",
"END": r"$",
}
Expand Down
16 changes: 16 additions & 0 deletions tests/test_requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,22 @@ def test_error_when_parens_not_closed_correctly(self) -> None:
" ~~~~~~~^"
)

def test_error_when_prefix_match_is_used_incorrectly(self) -> None:
# GIVEN
to_parse = "black (>=20.*) ; extra == 'format'"

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

# THEN
assert ctx.exconly() == (
"packaging.requirements.InvalidRequirement: "
".* suffix can only be used with `==` or `!=` operators\n"
" black (>=20.*) ; extra == 'format'\n"
" ~~~~~^"
)

def test_error_when_bracket_not_closed_correctly(self) -> None:
# GIVEN
to_parse = "name[bar, baz >= 1.0"
Expand Down

0 comments on commit 068a0b5

Please sign in to comment.