Skip to content

Commit 96d85be

Browse files
authored
Improve error for local version label with unsupported operators (#675)
Co-authored-by: Pradyun Gedam <[email protected]>
1 parent 5d7f020 commit 96d85be

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

src/packaging/_parser.py

+6
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,12 @@ def _parse_version_many(tokenizer: Tokenizer) -> str:
233233
span_start=span_start,
234234
span_end=tokenizer.position + 1,
235235
)
236+
if tokenizer.check("VERSION_LOCAL_LABEL_TRAIL", peek=True):
237+
tokenizer.raise_syntax_error(
238+
"Local version label can only be used with `==` or `!=` operators",
239+
span_start=span_start,
240+
span_end=tokenizer.position,
241+
)
236242
tokenizer.consume("WS")
237243
if not tokenizer.check("COMMA"):
238244
break

src/packaging/_tokenizer.py

+1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ def __str__(self) -> str:
7979
"URL": r"[^ \t]+",
8080
"IDENTIFIER": r"\b[a-zA-Z0-9][a-zA-Z0-9._-]*\b",
8181
"VERSION_PREFIX_TRAIL": r"\.\*",
82+
"VERSION_LOCAL_LABEL_TRAIL": r"\+[a-z0-9]+(?:[-_\.][a-z0-9]+)*",
8283
"WS": r"[ \t]+",
8384
"END": r"$",
8485
}

tests/test_requirements.py

+20
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,26 @@ def test_error_when_prefix_match_is_used_incorrectly(self) -> None:
301301
" ~~~~~^"
302302
)
303303

304+
@pytest.mark.parametrize("operator", [">=", "<=", ">", "<", "~="])
305+
def test_error_when_local_version_label_is_used_incorrectly(
306+
self, operator: str
307+
) -> None:
308+
# GIVEN
309+
to_parse = f"name {operator} 1.0+local.version.label"
310+
op_tilde = len(operator) * "~"
311+
312+
# WHEN
313+
with pytest.raises(InvalidRequirement) as ctx:
314+
Requirement(to_parse)
315+
316+
# THEN
317+
assert ctx.exconly() == (
318+
"packaging.requirements.InvalidRequirement: "
319+
"Local version label can only be used with `==` or `!=` operators\n"
320+
f" name {operator} 1.0+local.version.label\n"
321+
f" {op_tilde}~~~~^"
322+
)
323+
304324
def test_error_when_bracket_not_closed_correctly(self) -> None:
305325
# GIVEN
306326
to_parse = "name[bar, baz >= 1.0"

0 commit comments

Comments
 (0)